1
1
package individual_race ;
2
2
3
+ import common .Category ;
3
4
import common .Race ;
5
+ import common .RawResult ;
4
6
5
7
import java .io .IOException ;
6
- import java .util .Properties ;
8
+ import java .util .* ;
7
9
8
10
public class IndividualRace extends Race {
9
11
10
- // TODO variable number of prizes per category; open prizes in addition to gender categories; non-binary category; optional team prizes
12
+ // TODO variable number of prizes per category; open prizes in addition to gender categories; non-binary category; optional team prizes, by cumulative positions and times
13
+
14
+ IndividualRaceInput input ;
15
+ IndividualRaceOutput output_CSV , output_HTML , output_text , output_PDF ;
16
+ IndividualRacePrizes prizes ;
17
+
18
+ Runner [] entries ;
19
+ Result [] overall_results ;
20
+ Map <Category , List <Runner >> prize_winners = new HashMap <>();
11
21
12
22
public IndividualRace (final String config_file_path ) throws IOException {
13
23
super (config_file_path );
@@ -19,9 +29,146 @@ public IndividualRace(final Properties properties) throws IOException {
19
29
20
30
@ Override
21
31
protected void configure () throws IOException {
32
+
33
+ readProperties ();
34
+
35
+ configureHelpers ();
36
+ configureInputData ();
22
37
}
23
38
24
39
@ Override
25
40
public void processResults () throws IOException {
41
+
42
+ initialiseResults ();
43
+
44
+ fillFinishTimes ();
45
+ fillDNFs ();
46
+ calculateResults ();
47
+ allocatePrizes ();
48
+
49
+ printOverallResults ();
50
+ printPrizes ();
51
+ printCombined ();
52
+ }
53
+
54
+ private void configureHelpers () {
55
+
56
+ input = new IndividualRaceInput (this );
57
+
58
+ output_CSV = new IndividualRaceOutputCSV (this );
59
+ output_HTML = new IndividualRaceOutputHTML (this );
60
+ output_text = new IndividualRaceOutputText (this );
61
+ output_PDF = new IndividualRaceOutputPDF (this );
62
+
63
+ prizes = new IndividualRacePrizes (this );
64
+ }
65
+
66
+ private void configureInputData () throws IOException {
67
+
68
+ raw_results = input .loadRawResults ();
69
+ entries = input .loadEntries ();
70
+ }
71
+
72
+ private void initialiseResults () {
73
+
74
+ overall_results = new Result [entries .length ];
75
+
76
+ for (int i = 0 ; i < overall_results .length ; i ++)
77
+ overall_results [i ] = new Result (entries [i ], this );
78
+ }
79
+
80
+ private void fillFinishTimes () {
81
+
82
+ for (final RawResult raw_result : raw_results ) {
83
+
84
+ final int runner_index = findIndexOfRunnerWithBibNumber (raw_result .getBibNumber ());
85
+ final Result result = overall_results [runner_index ];
86
+
87
+ result .finish_time = raw_result .getRecordedFinishTime ();
88
+
89
+ // Provisionally this leg is not DNF since a finish time was recorded.
90
+ // However, it might still be set to DNF in fillDNFs() if the runner didn't complete the course.
91
+ result .DNF = false ;
92
+ }
93
+ }
94
+
95
+ private void fillDNFs () {
96
+
97
+ // This fills in the DNF results that were specified explicitly in the config
98
+ // file, corresponding to cases where the runner reported not completing the course.
99
+
100
+ // DNF cases where there is no recorded leg result are captured by the
101
+ // default value of Result.DNF being true.
102
+
103
+ if (dnf_string != null && !dnf_string .isBlank ()) {
104
+
105
+ for (final String dnf_string : dnf_string .split ("," )) {
106
+
107
+ try {
108
+ final Result result = getResultWithIndex (dnf_string );
109
+ result .DNF = true ;
110
+ }
111
+ catch (Exception e ) {
112
+ throw new RuntimeException ("illegal DNF time" );
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ private Result getResultWithIndex (final String bib ) {
119
+
120
+ final int bib_number = Integer .parseInt (bib );
121
+
122
+ return overall_results [findIndexOfRunnerWithBibNumber (bib_number )];
123
+ }
124
+
125
+ int findIndexOfRunnerWithBibNumber (final int bib_number ) {
126
+
127
+ for (int i = 0 ; i < overall_results .length ; i ++)
128
+ if (overall_results [i ].runner .bib_number == bib_number ) return i ;
129
+
130
+ throw new RuntimeException ("unregistered team: " + bib_number );
131
+ }
132
+
133
+ private void calculateResults () {
134
+
135
+ // Sort in order of recorded time.
136
+ // DNF results are sorted in increasing order of bib number.
137
+ // Where two runners have the same recorded time, the order in which they were recorded is preserved.
138
+ Arrays .sort (overall_results );
139
+ }
140
+
141
+ int getRecordedPosition (final int bib_number ) {
142
+
143
+ for (int i = 0 ; i < raw_results .length ; i ++) {
144
+ if (raw_results [i ].getBibNumber () == bib_number ) {
145
+ return i + 1 ;
146
+ }
147
+ }
148
+
149
+ return Integer .MAX_VALUE ;
150
+ }
151
+
152
+ private void allocatePrizes () {
153
+
154
+ prizes .allocatePrizes ();
155
+ }
156
+
157
+ private void printOverallResults () throws IOException {
158
+
159
+ output_CSV .printOverallResults ();
160
+ output_HTML .printOverallResults ();
161
+ }
162
+
163
+ private void printPrizes () throws IOException {
164
+
165
+ output_text .printPrizes ();
166
+ output_PDF .printPrizes ();
167
+ output_HTML .printPrizes ();
168
+ }
169
+
170
+ private void printCombined () throws IOException {
171
+
172
+ ((IndividualRaceOutputHTML )output_HTML ).printCombined ();
26
173
}
27
174
}
0 commit comments