Skip to content

Commit

Permalink
Merge pull request #768 from frank-weinberg/feature/764
Browse files Browse the repository at this point in the history
support exporting to statsbooks with extra rows
  • Loading branch information
frank-weinberg authored Sep 17, 2024
2 parents 7a2897d + 329ad01 commit 49a0fe3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected void itemAdded(Child<?> prop, ValueWithId item, Source source) {
boolean found = Paths.get(item.getValue()).toFile().canRead();
if (found && scoreBoard.isInitialLoadDone()) {
StatsbookExporter.preload(item.getValue(), getScoreBoard());
for (Game g : scoreBoard.getAll(ScoreBoard.GAME)) { g.clearStatsbookError(); }
} else if (!found) {
getScoreBoard().set(ScoreBoard.BLANK_STATSBOOK_FOUND, "none");
}
Expand Down
12 changes: 10 additions & 2 deletions src/com/carolinarollergirls/scoreboard/core/game/GameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ protected Object computeValue(Value<?> prop, Object value, Object last, Source s
!getTeam(Team.ID_2).getAll(Team.SCORE_ADJUSTMENT).isEmpty()) {
return "unapplied score adjustments";
} else {
return "";
return exportFailureText;
}
}
return value;
Expand Down Expand Up @@ -1112,14 +1112,21 @@ public boolean filenameIsUsed(String filename) {
}

@Override
public void exportDone(boolean success) {
public void exportDone(boolean success, String failureText) {
exportFailureText = failureText;
set(EXPORT_BLOCKED_BY, failureText);
if (success) {
set(LAST_FILE_UPDATE,
LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
}
set(UPDATE_IN_PROGRESS, false);
statsbookExporter = null;
}
@Override
public void clearStatsbookError() {
exportFailureText = "";
set(EXPORT_BLOCKED_BY, "");
}

public static void setQuickClockThreshold(long threshold) { quickClockThreshold = threshold; } // for unit tests

Expand All @@ -1138,6 +1145,7 @@ public void exportDone(boolean success) {

protected StatsbookExporter statsbookExporter;
protected JSONStateSnapshotter jsonSnapshotter;
protected String exportFailureText = "";

public static class GameSnapshot {
private GameSnapshot(GameImpl g, String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public interface Game extends ScoreBoardEventProvider {

public String getFilename();

public void exportDone(boolean success);
public void exportDone(boolean success, String failureText);
public void clearStatsbookError();

public enum State {
PREPARED("Prepared"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
Expand Down Expand Up @@ -108,6 +109,8 @@ public void run() {
wb = WorkbookFactory.create(in);
in.close();

countJamRows();

synchronized (coreLock) { fillIgrfAndPenalties(); }
synchronized (coreLock) {
fillScoreLineupsAndClock();
Expand All @@ -124,7 +127,18 @@ public void run() {
}
success = true;
} catch (Exception e) { Logger.printStackTrace(e); } finally {
game.exportDone(success);
game.exportDone(success, failureText);
}
}

private void countJamRows() {
Sheet score = wb.getSheet("Score");
int row = 3;
for (int period = 0; period < 2; period++) {
int startRow = row;
while (score.getRow(row).getCell(0).getCellType() != CellType.FORMULA) { row++; }
jamRows[period] = row - startRow;
row += 4;
}
}

Expand Down Expand Up @@ -390,11 +404,11 @@ private void fillScoreLineupsAndClock() {
Sheet clock = wb.getSheet("Game Clock");
Sheet reviews = wb.getSheet("Official Reviews");
int[] toCols = {3, 3};
int[] startRowIndex = {0, jamRows[0] + 4};

for (int pn = 0; pn < game.getCurrentPeriodNumber(); pn++) {
int rowIndex = pn * 42;
fillScoreHead(score.getRow(rowIndex), pn);
fillLineupsHead(lineups.getRow(rowIndex), pn);
for (int pn = 0; pn < game.getCurrentPeriodNumber() && pn < 2; pn++) {
fillScoreHead(score.getRow(startRowIndex[pn]), pn);
fillLineupsHead(lineups.getRow(startRowIndex[pn]), pn);

Period p = game.get(Game.PERIOD, pn + 1);
toCols = fillTimeouts(clock, reviews, toCols, p);
Expand All @@ -404,13 +418,21 @@ private void fillScoreLineupsAndClock() {
continue;
}

rowIndex += 3;
int rowIndex = startRowIndex[pn] + 3;
for (int jn = 1; jn <= p.getCurrentJamNumber(); jn++) {
int numRows = p.getJam(jn).get(Jam.STAR_PASS) ? 2 : 1;
if (rowIndex + numRows - startRowIndex[pn] - 3 >= jamRows[pn]) {
// sheet too short
int rowsNeeded = 0;
for (Jam j : p.getAll(Period.JAM)) { rowsNeeded += j.get(Jam.STAR_PASS) ? 2 : 1; }
failureText =
"Statsbook needs " + (rowsNeeded - jamRows[pn]) + " more row(s) in Period " + (pn + 1);
throw new RuntimeException(failureText);
}
fillJam(score.getRow(rowIndex), score.getRow(rowIndex + 1), osOffset.getRow(rowIndex),
lineups.getRow(rowIndex), lineups.getRow(rowIndex + 1), clock.getRow(pn * 51 + jn + 9),
p.getJam(jn));
rowIndex += p.getJam(jn).get(Jam.STAR_PASS) ? 2 : 1;
if (rowIndex > 82 || (rowIndex > 40 && rowIndex < 45)) { break; } // end of sheet
rowIndex += numRows;
}
}
}
Expand Down Expand Up @@ -660,6 +682,10 @@ private void fillClockJam(Row row, Jam j) {
boolean bAddTimeToDetails = false;
String endTime = ClockConversion.toHumanReadable(j.get(Jam.PERIOD_CLOCK_DISPLAY_END));

if (jamRows[j.get(Jam.PERIOD_NUMBER) - 1] != 38) {
// extending has broken jam numbers - fix them
setCell(row, 0, j.getNumber());
}
setCell(row, 1, j.getDuration() / 1000);
for (Penalty pen : j.getAll(Jam.PENALTY)) {
if (Skater.FO_EXP_ID.equals(pen.getProviderId()) && !"FO".equals(pen.getCode())) { // expulsion
Expand Down Expand Up @@ -766,6 +792,7 @@ private void createCellStyles(Sheet igrf) {
private CellStyle strikedNum;
private CellStyle strikedName;
private Object coreLock;
private String failureText = "";

// Officials names for filling sheet headers
private String pt = "";
Expand All @@ -778,4 +805,6 @@ private void createCellStyles(Sheet igrf) {

private Boolean hadOsOffset = false;
private List<String> osOffsetReasons = new ArrayList<>();

private int[] jamRows = {38, 38};
}

0 comments on commit 49a0fe3

Please sign in to comment.