Skip to content

Commit

Permalink
Progress on Grand Prix, not working.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Dec 2, 2024
1 parent 3ec1b99 commit 6c7fdda
Show file tree
Hide file tree
Showing 54 changed files with 667 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,23 @@ private static String replaceAllMapEntries(final String s, final Map<String, Str

public static Duration parseTime(String element) {

try {
return parseTime(element, ":");
}
catch (Exception _) {
return parseTime(element, "\\.");
}
}


private static Duration parseTime(String element, String separator) {

element = element.strip();
if (element.startsWith(":")) element = "0" + element;
if (element.endsWith(":")) element = element + "0";
if (element.startsWith(separator)) element = "0" + element;
if (element.endsWith(separator)) element = element + "0";

try {
final String[] parts = element.split(":");
final String[] parts = element.split(separator);
final String time_as_ISO = STR."PT\{hours(parts)}\{minutes(parts)}\{seconds(parts)}";

return Duration.parse(time_as_ISO);
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/grahamkirby/race_timing/common/Race.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ public abstract class Race {
// Single race.
public static final String KEY_ENTRIES_PATH = "ENTRIES_PATH";
public static final String KEY_RAW_RESULTS_PATH = "RAW_RESULTS_PATH";
public static final String KEY_RESULTS_PATH = "RESULTS_PATH";
public static final String KEY_CATEGORIES_ENTRY_PATH = "CATEGORIES_ENTRY_PATH";
public static final String KEY_CATEGORIES_PRIZE_PATH = "CATEGORIES_PRIZE_PATH";
public static final String KEY_DNF_FINISHERS = "DNF_FINISHERS";

// Individual race.
public static final String KEY_MEDIAN_TIME = "MEDIAN_TIME";

// Relay race.
public static final String KEY_GENDER_ELIGIBILITY_MAP_PATH = "GENDER_ELIGIBILITY_MAP_PATH";
public static final String KEY_ANNOTATIONS_PATH = "ANNOTATIONS_PATH";
Expand Down Expand Up @@ -202,6 +206,9 @@ protected void printCombined() throws IOException {

public Path getPath(final String path) {

if (path == null) {
int x = 3;
}
return path.startsWith("/") ?
getPathRelativeToProjectRoot(path) :
getPathRelativeToRaceConfigFile(path);
Expand Down Expand Up @@ -229,7 +236,8 @@ public List<PrizeCategory> getPrizeCategories() {

public List<RaceResult> getOverallResults() {

return getOverallResults(getPrizeCategories());
return overall_results;
// return getOverallResults(getPrizeCategories());
}

public List<RaceResult> getOverallResults(final List<PrizeCategory> prize_categories) {
Expand All @@ -240,7 +248,7 @@ public List<RaceResult> getOverallResults(final List<PrizeCategory> prize_catego

public List<RaceResult> getOverallResults(final Predicate<RaceResult> inclusion_filter) {

final List<RaceResult> results = overall_results.stream().filter(inclusion_filter).toList();
final List<RaceResult> results = getOverallResults().stream().filter(inclusion_filter).toList();
setPositionStrings(results, allowEqualPositions());
return results;
}
Expand Down Expand Up @@ -282,6 +290,9 @@ public static Path getTestResourcesRootPath(final String individual_test_resourc

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

if (r1 == null) {
int x = 3;
}
return Boolean.compare(r1.getCompletionStatus() != CompletionStatus.COMPLETED, r2.getCompletionStatus() != CompletionStatus.COMPLETED);
}

Expand Down Expand Up @@ -416,12 +427,12 @@ protected void sortResults() {
sortDNFResults();
}

private void sortAllResults() {
protected void sortAllResults() {

overall_results.sort(combineComparators(getComparators()));
}

private void sortDNFResults() {
protected void sortDNFResults() {

overall_results.sort(dnfOnly(combineComparators(getDNFComparators())));
}
Expand Down Expand Up @@ -471,6 +482,7 @@ private PrizeCategoryGroup getGroupWithName(final List<PrizeCategoryGroup> group

private boolean entryCategoryIsEligibleForPrizeCategoryByAge(final EntryCategory entry_category, final PrizeCategory prize_category) {

if (entry_category == null) return true;
return entry_category.getMinimumAge() >= prize_category.getMinimumAge() &&
entry_category.getMaximumAge() <= prize_category.getMaximumAge();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public abstract class RaceInput {

protected final Race race;
protected String entries_path, raw_results_path, categories_entry_path, categories_prize_path;
protected String entries_path, raw_results_path, results_path, categories_entry_path, categories_prize_path;

public RaceInput(Race race) {
this.race = race;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.grahamkirby.race_timing.individual_race;

import org.grahamkirby.race_timing.common.CompletionStatus;
import org.grahamkirby.race_timing.common.Normalisation;
import org.grahamkirby.race_timing.common.RaceInput;
import org.grahamkirby.race_timing.common.RaceResult;
import org.grahamkirby.race_timing.common.categories.EntryCategory;
Expand All @@ -30,11 +31,14 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Comparator;
import java.util.List;

public class IndividualRace extends SingleRace {

private String median_time_string;

public IndividualRace(final Path config_file_path) throws IOException {
super(config_file_path);
}
Expand All @@ -57,11 +61,13 @@ public void calculateResults() {

initialiseResults();

fillFinishTimes();
fillDNFs();
if (raw_results != null) {
fillFinishTimes();
fillDNFs();

sortResults();
allocatePrizes();
sortResults();
allocatePrizes();
}
}

public EntryCategory findCategory(final int bib_number) {
Expand All @@ -80,6 +86,26 @@ private int compareRecordedPosition(final RaceResult r1, final RaceResult r2) {

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

@Override
protected void sortAllResults() {
if (raw_results != null) super.sortAllResults();
}

@Override
protected void sortDNFResults() {
if (raw_results != null) super.sortDNFResults();
}

@Override
protected void readProperties() throws IOException {

super.readProperties();

// Specifies all the bib numbers for runners who did have a finish
// time recorded but were declared DNF.
median_time_string = getProperty(KEY_MEDIAN_TIME);
}

@Override
protected RaceInput getInput() {
return new IndividualRaceInput(this);
Expand Down Expand Up @@ -108,10 +134,15 @@ protected RaceOutputPDF getOutputPDF() {
@Override
protected void initialiseResults() {

entries.stream().
map(entry -> (IndividualRaceEntry)entry).
map(entry -> new IndividualRaceResult(this, entry)).
forEachOrdered(overall_results::add);
if (entries != null) {
entries.stream().
map(entry -> (IndividualRaceEntry) entry).
map(entry -> new IndividualRaceResult(this, entry)).
forEachOrdered(overall_results::add);
}
else {

}
}

@Override
Expand Down Expand Up @@ -139,6 +170,7 @@ public List<Comparator<RaceResult>> getDNFComparators() {
@Override
protected boolean entryCategoryIsEligibleForPrizeCategoryByGender(final EntryCategory entry_category, final PrizeCategory prize_category) {

if (entry_category == null) return true;
return entry_category.getGender().equals(prize_category.getGender());
}

Expand All @@ -164,17 +196,34 @@ protected void fillDNF(final String dnf_string) {

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

public Duration getMedianTime() {

if (median_time_string != null) return Normalisation.parseTime(median_time_string);

List<RaceResult> results = getOverallResults();
if (results.size() % 2 == 0) {
RaceResult result1 = results.get(results.size() / 2);
RaceResult result2 = results.get(results.size() / 2 + 1);
return ((IndividualRaceResult) result1).finish_time.plus(((IndividualRaceResult) result2).finish_time).dividedBy(2);
}
else {
RaceResult result = results.get(results.size() / 2);
return ((IndividualRaceResult) result).finish_time;
}
}

private void fillFinishTimes() {

raw_results.forEach(raw_result -> {
if (raw_results != null)
raw_results.forEach(raw_result -> {

final IndividualRaceResult result = getResultWithBibNumber(raw_result.getBibNumber());
result.finish_time = raw_result.getRecordedFinishTime();
final IndividualRaceResult result = getResultWithBibNumber(raw_result.getBibNumber());
result.finish_time = raw_result.getRecordedFinishTime();

// 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.completion_status = CompletionStatus.COMPLETED;
});
// 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.completion_status = CompletionStatus.COMPLETED;
});
}

private IndividualRaceResult getResultWithBibNumber(final int bib_number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public IndividualRaceEntry(final List<String> elements, final Race race) {

final String name = race.normalisation.cleanRunnerName(mapped_elements.get(NAME_INDEX));
final String club = race.normalisation.cleanClubOrTeamName(mapped_elements.get(CLUB_INDEX));
final EntryCategory category = race.lookupEntryCategory(race.mapCategory(mapped_elements.get(CATEGORY_INDEX)));

final String category_name = race.mapCategory(mapped_elements.get(CATEGORY_INDEX));
final EntryCategory category = category_name.isEmpty() ? null : race.lookupEntryCategory(category_name);

runner = new Runner(name, club, category);
}
catch (RuntimeException _) {
catch (RuntimeException e) {
throw new RuntimeException("illegal category for runner: " + bib_number);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected RaceEntry makeRaceEntry(final List<String> elements) {

@Override
public List<RawResult> loadRawResults() throws IOException {

if (raw_results_path == null) return null;
return loadRawResults(race.getPath(raw_results_path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class IndividualRaceResult extends RaceResult {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

public abstract class SeriesRace extends Race {

Expand Down Expand Up @@ -67,16 +68,20 @@ protected void configureInputData() throws IOException {
@Override
protected void initialiseResults() {

final Predicate<RaceResult> inclusion_predicate = getResultInclusionPredicate();

races.stream().
filter(Objects::nonNull).
flatMap(race -> race.getOverallResults().stream()).
filter(inclusion_predicate).
map(result -> (IndividualRaceResult) result).
map(result -> result.entry.runner).
distinct().
map(this::getOverallResult).
forEachOrdered(overall_results::add);
}


@Override
protected void outputResults() throws IOException {

Expand Down Expand Up @@ -108,4 +113,5 @@ public int getNumberOfRacesTakenPlace() {
//////////////////////////////////////////////////////////////////////////////////////////////////

protected abstract RaceResult getOverallResult(final Runner runner);
protected abstract Predicate<RaceResult> getResultInclusionPredicate();
}
Loading

0 comments on commit 6c7fdda

Please sign in to comment.