-
Notifications
You must be signed in to change notification settings - Fork 6
/
BarnesPointsCalc.ts
468 lines (399 loc) · 14.3 KB
/
BarnesPointsCalc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import { Entry, Event, Results, genPlaces } from 'crewtimer-common';
import { isAFinal } from '../common/CrewTimerUtils';
export type BarnesPointsTeamResults = {
combined: number;
mensScull: number;
womensScull: number;
mensSweep: number;
womensSweep: number;
};
export type PointsPlace = {
points: number;
place: number;
};
export type TeamPoints = {
team: string;
points: number;
place: number;
};
export type BarnesFullCategoryResults = {
combined: TeamPoints[];
mensScull: TeamPoints[];
womensScull: TeamPoints[];
mensSweep: TeamPoints[];
womensSweep: TeamPoints[];
};
export type BarnesSimpleCategoryResults = {
combined: TeamPoints[];
combinedSweep: TeamPoints[];
combinedScull: TeamPoints[];
mens: TeamPoints[];
womens: TeamPoints[];
};
const PLACEHOLD_TEAM_NAME = 'Empty';
const EXHIB_PENALTY_CODE = 'Exhib';
const WOMENS_EVENT_REGEX_MATCHERS = [/WOMEN/, /GIRL/];
/**
* The max possible number of points for this event given the boat class and event type
*
* @param eventName
* @returns Number of points
*/
export const maxPointsFromName = (eventName: string, useScaledEvents: boolean) => {
eventName = eventName.toUpperCase();
const points = maxPointsByBoatClass(eventName);
return points * scaleByEventType(eventName, useScaledEvents);
};
/**
* The max possible number of points for this event given the boat class and event type
*
* @param eventName
* @returns Number of points
*/
export const maxPointsByBoatClass = (eventName: string) => {
const boatClassCaptureExpression = /.* ([1248])[xX\-+]/;
const match = eventName.match(boatClassCaptureExpression);
if (match && match.length > 1) {
const boatClass = Number.parseInt(match[1]);
if (boatClass == 1) {
return 10;
}
if (boatClass == 2) {
return 15;
}
if (boatClass == 4) {
return 20;
}
if (boatClass == 8) {
return 30;
}
}
// assume there is at least one person per boat
return 10;
};
/**
* Number of points for each place in a final,
* by the number of entries in the final.
*/
const percentageOfPoints: Map<number, number[]> = new Map([
[2, [1, 0.2]],
[3, [1, 0.4, 0.2]],
[4, [1, 0.6, 0.3, 0.05]],
[5, [1, 0.8, 0.4, 0.1]],
[6, [1, 0.8, 0.4, 0.2, 0.1, 0.05]],
]);
/**
* Number of points for each place in a final,
* by the number of entries in the final.
*/
const percentageOfPoints8Lane: Map<number, number[]> = new Map([
[2, [1, 0.8]],
[3, [1, 0.8, 0.6]],
[4, [1, 0.8, 0.6, 0.4]],
[5, [1, 0.8, 0.6, 0.4, 0.3]],
[6, [1, 0.8, 0.6, 0.4, 0.3, 0.2]],
[7, [1, 0.8, 0.6, 0.4, 0.3, 0.2, 0.1]],
[8, [1, 0.8, 0.6, 0.4, 0.3, 0.2, 0.1, 0.05]],
]);
/**
* Given the number of entries in an event and a boats placement,
* determine the percentage of points for the given place
*
* @param numberOfEntries
* @param place
* @returns (float) A pergentage between [0,1]
*/
const scalePoints = (numberOfEntries: number, place: number, useEightLanePoints?: boolean) => {
if (numberOfEntries < 2) {
return 0;
}
// snap to max number of lanes with points, even if there were more lanes
const maxLanes = useEightLanePoints ? 8 : 6;
if (!useEightLanePoints && numberOfEntries > maxLanes) {
numberOfEntries = maxLanes;
}
const scalars = (useEightLanePoints ? percentageOfPoints8Lane : percentageOfPoints).get(numberOfEntries);
if (!scalars || place > scalars.length) {
return 0;
}
return scalars[place - 1];
};
const NOVICE_MATCHERS = [/NOV/, /FRESHMAN/, /FROSH/, /3V/, /3RD/];
const JUNIOR_MATCHERS = [/JUNIOR/, /JNR/, /JR/, /2V/, /2ND/];
/**
* Based on whether this is a varsity, junior, or novice event
* return the percentage of max points to use
*
* 1st Varsity: 100%
* 2nd Varsity/Junior/JV: 80%
* 3rd Varsity/Novice/Freshman/Frosh: 60%
*
* @param eventName
*/
const scaleByEventType = (eventName: string, useScaledEvents: boolean) => {
if (!useScaledEvents) {
return 1;
}
if (NOVICE_MATCHERS.some((noviceMatcher) => eventName.match(noviceMatcher))) {
return 0.6;
}
if (JUNIOR_MATCHERS.some((juniorMatcher) => eventName.match(juniorMatcher))) {
return 0.8;
}
// assume this is a varsity event
return 1;
};
/**
* Extract the root team name. This will trim any trailing single characters
* which were used as A or B boat designations
*
* For example:
* "Green Lake Crew A" -> "Green Lake Crew"
* "Green Lake Crew B" -> "Green Lake Crew"
* "Green Lake Crew" -> "Green Lake Crew"
*
* @param crewName
* @returns (string) The team name
*/
const trimCrewName = (crewName: string) => {
crewName = crewName.trim();
const suffixExpression = / .$/;
return crewName.replace(suffixExpression, '');
};
/**
* Assign places to an array of TeamPoints
*
* @param TeamPoints[]
*/
const assignPlaces = (teamPoints: TeamPoints[]) => {
// round to two decimals to account for float precision
const places = genPlaces(
teamPoints.map((teamEntry) => Math.round(teamEntry.points * 100) / 100),
'desc',
);
places.forEach((place, i) => (teamPoints[i].place = place));
};
/**
* Sort teams in each category by number of points, including sweep/sculling split out
*
* @param results
* @returns
*/
const finalizeFullResults = (results: Map<string, BarnesPointsTeamResults>): BarnesFullCategoryResults => {
const sortedPoints = {
combined: Array.from(results.entries())
.sort((a, b) => b[1].combined - a[1].combined)
.map((value) => ({ team: value[0], points: value[1].combined, place: 0 })),
mensScull: Array.from(results.entries())
.sort((a, b) => b[1].mensScull - a[1].mensScull)
.map((value) => ({ team: value[0], points: value[1].mensScull, place: 0 })),
womensScull: Array.from(results.entries())
.sort((a, b) => b[1].womensScull - a[1].womensScull)
.map((value) => ({ team: value[0], points: value[1].womensScull, place: 0 })),
mensSweep: Array.from(results.entries())
.sort((a, b) => b[1].mensSweep - a[1].mensSweep)
.map((value) => ({ team: value[0], points: value[1].mensSweep, place: 0 })),
womensSweep: Array.from(results.entries())
.sort((a, b) => b[1].womensSweep - a[1].womensSweep)
.map((value) => ({ team: value[0], points: value[1].womensSweep, place: 0 })),
};
assignPlaces(sortedPoints.combined);
assignPlaces(sortedPoints.mensScull);
assignPlaces(sortedPoints.womensScull);
assignPlaces(sortedPoints.mensSweep);
assignPlaces(sortedPoints.womensSweep);
return sortedPoints;
};
/**
* Sort teams in each category by number of points
*
* @param results
* @returns
*/
const finalizeResults = (results: Map<string, BarnesPointsTeamResults>) => {
const sortedPoints = {
combined: Array.from(results.entries())
.sort((a, b) => b[1].combined - a[1].combined)
.map((value) => ({ team: value[0], points: value[1].combined, place: 0 })),
combinedSweep: Array.from(results.entries())
.sort((a, b) => b[1].womensSweep + b[1].mensSweep - (a[1].womensSweep + a[1].mensSweep))
.map((value) => ({ team: value[0], points: value[1].womensSweep + value[1].mensSweep, place: 0 })),
combinedScull: Array.from(results.entries())
.sort((a, b) => b[1].womensScull + b[1].mensSweep - (a[1].womensScull + a[1].mensScull))
.map((value) => ({ team: value[0], points: value[1].womensScull + value[1].mensScull, place: 0 })),
mens: Array.from(results.entries())
.sort((a, b) => b[1].mensScull + b[1].mensSweep - (a[1].mensScull + a[1].mensSweep))
.map((value) => ({ team: value[0], points: value[1].mensScull + value[1].mensSweep, place: 0 })),
womens: Array.from(results.entries())
.sort((a, b) => b[1].womensScull + b[1].womensSweep - (a[1].womensScull + a[1].womensSweep))
.map((value) => ({ team: value[0], points: value[1].womensScull + value[1].womensSweep, place: 0 })),
};
assignPlaces(sortedPoints.combined);
assignPlaces(sortedPoints.combinedScull);
assignPlaces(sortedPoints.combinedSweep);
assignPlaces(sortedPoints.mens);
assignPlaces(sortedPoints.womens);
return sortedPoints;
};
/**
* Returns the number of entries, excluding any exhibition crews
*
* @param entries
* @returns number of elligible entries
*/
export const calculateNumberOfEntries = (entries: Entry[]) => {
return entries.filter((entry) => entry.PenaltyCode != 'Exhib').length;
};
export const calculateEventTeamPoints = (
eventResult: Event,
useScaledEvents: boolean,
useEightLanePoints?: boolean,
): Map<string, number> => {
const eventTeamPoints = new Map<string, number>();
if (!isAFinal(eventResult.Event, eventResult.EventNum)) {
return eventTeamPoints; // not a final (e.g. Heat, TT)
}
const maxPoints = maxPointsFromName(eventResult.Event, useScaledEvents);
const numberOfEntries = calculateNumberOfEntries(eventResult.entries || []);
// track the team's we've seen in this event to exclude anything that is not
// a primary entry (ex: B entries, second entries)
const placingTeams = new Set<string>();
const sortedEntries = (eventResult.entries || []).sort(
(lhs, rhs) => (lhs.Place || Number.MAX_VALUE) - (rhs.Place || Number.MAX_VALUE),
);
sortedEntries.forEach((entry) => {
const teamName = trimCrewName(entry.Crew);
if (!entry.Place) {
return; // DNF, DNS, DQ, Exhib etc
}
// if a team has already placed in this event, skip subsequent entries
if (placingTeams.has(teamName)) {
return;
}
placingTeams.add(teamName);
const points = maxPoints * scalePoints(numberOfEntries, entry.Place, useEightLanePoints);
eventTeamPoints.set(teamName, points);
});
return eventTeamPoints;
};
/**
* Identify all teams which are entered in either only Men's or only Women's events and
* which could be elligible for exclusion from the Combined points table
*/
const getCoedTeams = (eventResults: Event[]): Set<string> => {
const womensTeams = new Set<string>();
const mensTeams = new Set<string>();
eventResults.forEach((eventResult) => {
if (womensEvent(eventResult.Event)) {
eventResult.entries.forEach((entry) => {
womensTeams.add(trimCrewName(entry.Crew));
});
} else {
eventResult.entries.forEach((entry) => {
mensTeams.add(trimCrewName(entry.Crew));
});
}
});
const coedTeams = new Set<string>();
womensTeams.forEach((team) => {
if (mensTeams.has(team)) {
coedTeams.add(team);
}
});
return coedTeams;
};
/**
* Return true if the event name indicated that this is a women's event
*/
export const womensEvent = (eventName: string): boolean => {
return WOMENS_EVENT_REGEX_MATCHERS.some((candidate) => eventName.toUpperCase().search(candidate) != -1);
};
/**
* Calculate points based on the Barnes Scoring System, as described by MSRA:
* https://sites.google.com/site/msrahome/regatta-rules/home
*/
export const barnesPointsImpl = (
resultData: Results,
useScaledEvents: boolean,
coedTeamsOnlyInCombined?: boolean,
useEightLanePoints?: boolean,
): Map<string, BarnesPointsTeamResults> => {
const teamPoints = new Map<string, BarnesPointsTeamResults>();
const coedTeams = getCoedTeams(resultData.results);
resultData.results.forEach((eventResult) => {
const isWomensEvent = womensEvent(eventResult.Event);
const isScullingEvent = eventResult.Event.match(/[1234]x/) != null;
const eventTeamPoints = calculateEventTeamPoints(eventResult, useScaledEvents, useEightLanePoints);
// aggregate the points from this event into the whole team points table
eventTeamPoints.forEach((points: number, teamName: string) => {
const teamEntry = teamPoints.get(teamName);
if (!teamEntry) {
teamPoints.set(teamName, {
// if we only allow coed teams to get points towards the combined trophy, and this is not a coed team,
// give them 0 points towards the combined trophy
combined: coedTeamsOnlyInCombined && !coedTeams.has(teamName) ? 0 : points,
womensScull: isWomensEvent && isScullingEvent ? points : 0,
mensScull: !isWomensEvent && isScullingEvent ? points : 0,
womensSweep: isWomensEvent && !isScullingEvent ? points : 0,
mensSweep: !isWomensEvent && !isScullingEvent ? points : 0,
});
} else {
teamPoints.set(teamName, {
combined: coedTeamsOnlyInCombined && !coedTeams.has(teamName) ? 0 : teamEntry.combined + points,
womensScull: teamEntry.womensScull + (isWomensEvent && isScullingEvent ? points : 0),
mensScull: teamEntry.mensScull + (!isWomensEvent && isScullingEvent ? points : 0),
womensSweep: teamEntry.womensSweep + (isWomensEvent && !isScullingEvent ? points : 0),
mensSweep: teamEntry.mensSweep + (!isWomensEvent && !isScullingEvent ? points : 0),
});
}
});
});
// check if there were teams entered which scored 0 points,
// but which still need to be represented on the results page
// exhibition crews are ignored
const scoringTeams = new Set<string>(teamPoints.keys());
const missingTeams = new Set<string>();
resultData.results.forEach((event) =>
event.entries?.forEach((entry) => {
const teamName = trimCrewName(entry.Crew);
if (!scoringTeams.has(teamName) && teamName != PLACEHOLD_TEAM_NAME && entry.PenaltyCode != EXHIB_PENALTY_CODE) {
missingTeams.add(teamName);
}
}),
);
missingTeams.forEach((missingTeam) =>
teamPoints.set(missingTeam, {
combined: 0,
womensScull: 0,
mensScull: 0,
womensSweep: 0,
mensSweep: 0,
}),
);
return teamPoints;
};
/**
* Calculate points based on the Barnes Scoring System, as described by MSRA:
* https://sites.google.com/site/msrahome/regatta-rules/home
*/
export const barnesFullPointsCalc = (
resultData: Results,
useScaledEvents: boolean,
coedTeamsOnlyInCombined?: boolean,
useEightLanePoints?: boolean,
): BarnesFullCategoryResults => {
const teamPoints = barnesPointsImpl(resultData, useScaledEvents, coedTeamsOnlyInCombined, useEightLanePoints);
return finalizeFullResults(teamPoints);
};
/**
* Calculate points based on the Barnes Scoring System, as described by MSRA:
* https://sites.google.com/site/msrahome/regatta-rules/home
*/
export const barnesPointsCalc = (
resultData: Results,
useScaledEvents: boolean,
useEightLanePoints?: boolean,
): BarnesSimpleCategoryResults => {
const teamPoints = barnesPointsImpl(resultData, useScaledEvents, useEightLanePoints);
return finalizeResults(teamPoints);
};