Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Nov 26, 2024
1 parent b1159c9 commit e3d590b
Show file tree
Hide file tree
Showing 40 changed files with 1,641 additions and 1,595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@

public enum CompletionStatus {

// TODO enum for completion status - normal, DNS, DNF, recorded but DNF

COMPLETED, DNS, DNF, RECORDED;

// TODO tidy logic around number of races completed, possible series completion etc
// TODO rationalise with IndividualRace with respect to treatment of non-starts or non finishers.
// TODO unify compareCompletionSoFar and compareCompletion into compareCanComplete

int numberOfRacesCompleted() {
return 0;
}

boolean completedSeries() {
return false;
}

boolean canCompleteSeries() {
return false;
}
COMPLETED, // Completed race or series.
DNS, // Did Not Start: runner appears in entry but no finish(es) recorded.
DNF // Did Not Finish: did not complete all legs or sufficient component races.
}
13 changes: 4 additions & 9 deletions src/main/java/org/grahamkirby/race_timing/common/Race.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;
import java.util.function.Predicate;

import static org.grahamkirby.race_timing.common.Normalisation.parseTime;

public abstract class Race {

public static final String DUMMY_DURATION_STRING = "00:00:00";
public static final Duration DUMMY_DURATION = parseTime(DUMMY_DURATION_STRING);
public static final String COMMENT_SYMBOL = "#";

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -252,9 +247,9 @@ public StringBuilder getNotes() {
public EntryCategory lookupEntryCategory(final String short_name) {

return entry_categories.stream().
filter(category -> category.getShortName().equals(short_name)).
findFirst().
orElseThrow(() -> new RuntimeException("Category not found: " + short_name));
filter(category -> category.getShortName().equals(short_name)).
findFirst().
orElseThrow(() -> new RuntimeException("Category not found: " + short_name));
}

public String mapCategory(final String category) {
Expand All @@ -272,7 +267,7 @@ public static Path getTestResourcesRootPath(final String individual_test_resourc

protected int compareCompletion(final RaceResult r1, final RaceResult r2) {

return Boolean.compare(!r1.completed(), !r2.completed());
return Boolean.compare(r1.getCompletionStatus() != CompletionStatus.COMPLETED, r2.getCompletionStatus() != CompletionStatus.COMPLETED);
}

protected int comparePerformance(final RaceResult r1, final RaceResult r2) {
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/grahamkirby/race_timing/common/RacePrizes.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void allocatePrizes() {

protected boolean prizeWinner(final RaceResult result, final PrizeCategory category) {

return !alreadyWonPrize(result) && result.completed() && race.entryCategoryIsEligibleForPrizeCategory(result.getCategory(), category);
return !alreadyWonPrize(result) &&
result.getCompletionStatus() == CompletionStatus.COMPLETED &&
race.entryCategoryIsEligibleForPrizeCategory(result.getCategory(), category);
}

protected boolean alreadyWonPrize(RaceResult result) {
Expand Down Expand Up @@ -71,9 +73,12 @@ public void setPrizeWinners(final PrizeCategory category) {
final AtomicInteger position = new AtomicInteger(1);

race.getOverallResults().stream().
filter(result -> position.get() <= category.numberOfPrizes() && prizeWinner(result, category)).
peek(result -> setPrizeWinner(result, category)).
forEach(_ -> position.getAndIncrement());
filter(_ -> position.get() <= category.numberOfPrizes()).
filter(result -> prizeWinner(result, category)).
forEach(result -> {
setPrizeWinner(result, category);
position.getAndIncrement();
});
}

public boolean prizesInThisOrLaterCategory(final PrizeCategory category) {
Expand All @@ -88,9 +93,9 @@ public boolean prizesInThisOrLaterCategory(final PrizeCategory category) {

public boolean prizesInOtherCategorySameAge(final PrizeCategory category) {

for (final PrizeCategory c : race.getPrizeCategories())
if (!c.equals(category) && c.getMinimumAge() == category.getMinimumAge() && !race.prizes.getPrizeWinners(c).isEmpty()) return true;

return false;
return race.getPrizeCategories().stream().
filter(c -> !c.equals(category)).
filter(c -> c.getMinimumAge() == category.getMinimumAge()).
anyMatch(c -> !race.prizes.getPrizeWinners(c).isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public abstract class RaceResult {

public final Race race;
public String position_string;
public PrizeCategory category_of_prize_awarded = null;
public CompletionStatus completion_status;
public PrizeCategory category_of_prize_awarded;

protected RaceResult(Race race) {
this.race = race;
}

protected abstract String getIndividualRunnerName();
public abstract int comparePerformanceTo(final RaceResult other);
public abstract boolean completed();
public abstract CompletionStatus getCompletionStatus();
public abstract boolean shouldDisplayPosition();
public abstract boolean shouldBeDisplayedInResults();
public abstract EntryCategory getCategory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.grahamkirby.race_timing.individual_race;

import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.RaceInput;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.categories.EntryCategory;
Expand Down Expand Up @@ -144,6 +145,7 @@ protected boolean entryCategoryIsEligibleForPrizeCategoryByGender(final EntryCat

@Override
protected EntryCategory getEntryCategory(RaceResult result) {

return ((IndividualRaceResult) result).entry.runner.category;
}

Expand All @@ -154,7 +156,7 @@ protected void fillDNF(final String dnf_string) {
final int bib_number = Integer.parseInt(dnf_string);
final IndividualRaceResult result = getResultWithBibNumber(bib_number);

result.DNF = true;
result.completion_status = CompletionStatus.DNF;
}
catch (Exception e) {
throw new RuntimeException("illegal DNF string: ", e);
Expand All @@ -172,7 +174,7 @@ private void fillFinishTimes() {

// Provisionally this result is not DNF since a finish time was recorded.
// However, it might still be set to DNF in fillDNF() if the runner didn't complete the course.
result.DNF = false;
result.completion_status = CompletionStatus.COMPLETED;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.grahamkirby.race_timing.individual_race;

import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.Race;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.output.ResultPrinterCSV;
Expand Down Expand Up @@ -63,10 +64,10 @@ public void printResult(final RaceResult r) throws IOException {
final IndividualRaceResult result = (IndividualRaceResult) r;

// Check for case where no time recorded for runner, as opposed to finished but didn't complete course.
if (!result.duration().equals(Race.DUMMY_DURATION)) {
if (result.duration() != null) {

writer.append(STR."\{result.shouldDisplayPosition() ? result.position_string : ""},\{result.entry.bib_number},\{encode(result.entry.runner.name)},").
append(STR."\{encode(result.entry.runner.club)},\{result.entry.runner.category.getShortName()},\{!result.completed() ? "DNF" : format(result.duration())}\n");
append(STR."\{encode(result.entry.runner.club)},\{result.entry.runner.category.getShortName()},\{result.getCompletionStatus() == CompletionStatus.COMPLETED ? format(result.duration()) : "DNF"}\n");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.grahamkirby.race_timing.individual_race;

import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.Race;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.output.ResultPrinterHTML;
Expand Down Expand Up @@ -92,16 +93,15 @@ public void printResult(final RaceResult r) throws IOException {
final IndividualRaceResult result = ((IndividualRaceResult)r);

// Check for case where no time recorded for runner, as opposed to finished but didn't complete course.
if (!result.duration().equals(Race.DUMMY_DURATION)) {

if (result.shouldBeDisplayedInResults()) {
writer.append(STR."""
<tr>
<td>\{result.DNF ? "" : result.position_string}</td>
<td>\{result.shouldDisplayPosition() ? result.position_string : ""}</td>
<td>\{result.entry.bib_number}</td>
<td>\{race.normalisation.htmlEncode(result.entry.runner.name)}</td>
<td>\{result.entry.runner.club}</td>
<td>\{result.entry.runner.category.getShortName()}</td>
<td>\{result.DNF ? DNF_STRING : format(result.duration())}</td>
<td>\{result.getCompletionStatus() != CompletionStatus.COMPLETED ? DNF_STRING : format(result.duration())}</td>
</tr>
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
*/
package org.grahamkirby.race_timing.individual_race;

import org.grahamkirby.race_timing.common.Race;
import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.categories.EntryCategory;

import java.time.Duration;
import java.util.Comparator;

public class IndividualRaceResult extends RaceResult {

public IndividualRaceEntry entry;
public boolean DNF;
public Duration finish_time;
CompletionStatus completion_status;

//////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -36,17 +37,17 @@ public IndividualRaceResult(final IndividualRace race, final IndividualRaceEntry
this.entry = entry;

// Initialised in IndividualRace.fillFinishTimes().
finish_time = Race.DUMMY_DURATION;
finish_time = null;

// Will be set to false later if a time is processed for this runner.
DNF = true;
// Will be changed later if a time is processed for this runner.
completion_status = CompletionStatus.DNS;
}

//////////////////////////////////////////////////////////////////////////////////////////////////

@Override
public boolean completed() {
return !DNF;
public CompletionStatus getCompletionStatus () {
return completion_status;
}

@Override
Expand All @@ -61,12 +62,21 @@ protected String getIndividualRunnerName() {

@Override
public int comparePerformanceTo(final RaceResult other) {
return duration().compareTo(((IndividualRaceResult) other).duration());

final Duration duration = duration();
final Duration other_duration = ((IndividualRaceResult) other).duration();

return Comparator.nullsLast(Duration::compareTo).compare(duration, other_duration);
}

@Override
public boolean shouldDisplayPosition() {
return completed();
return completion_status == CompletionStatus.COMPLETED;
}

@Override
public boolean shouldBeDisplayedInResults() {
return completion_status == CompletionStatus.COMPLETED || completion_status == CompletionStatus.DNF;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
*/
package org.grahamkirby.race_timing.relay_race;

import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.Race;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.categories.EntryCategory;

import java.time.Duration;
import java.util.Comparator;

public class LegResult extends RaceResult {

final RelayRaceEntry entry;
int leg_number;
boolean DNF;
CompletionStatus completion_status;
boolean in_mass_start;

Duration start_time; // Relative to start of leg 1.
Expand All @@ -36,17 +38,17 @@ public LegResult(final RelayRaceEntry entry, final Race race) {

super(race);
this.entry = entry;
this.DNF = true;
this.completion_status = CompletionStatus.DNF;
this.in_mass_start = false;
}

public Duration duration() {
return DNF ? Race.DUMMY_DURATION : finish_time.minus(start_time);
return completion_status == CompletionStatus.COMPLETED ? finish_time.minus(start_time) : null;
}

@Override
public boolean completed() {
return !DNF;
public CompletionStatus getCompletionStatus() {
return completion_status;
}

@Override
Expand All @@ -60,13 +62,21 @@ protected String getIndividualRunnerName() {
}

@Override
public int comparePerformanceTo(final RaceResult result) {
public int comparePerformanceTo(final RaceResult other) {

return duration().compareTo(((LegResult) result).duration());
final Duration duration = duration();
final Duration other_duration = ((LegResult) other).duration();

return Comparator.nullsLast(Duration::compareTo).compare(duration, other_duration);
}

@Override
public boolean shouldDisplayPosition() {
return true;
}

@Override
public boolean shouldBeDisplayedInResults() {
return getCompletionStatus() == CompletionStatus.COMPLETED;
}
}
Loading

0 comments on commit e3d590b

Please sign in to comment.