-export const calculateScores = tournament => {
- const runners = getRunners(tournament);
- const scores = runners.map(participant => ({ participant, score: 0 }));
- if (!scores.length) return scores;
- if (!tournament.rounds || !tournament.rounds.length) return scores;
- tournament.rounds.forEach(round => {
- const filtered = Participant
- .sortByResult(runners, round)
- .map(p => ({ participant: p, result: Participant.findResult(p, round) }))
- .filter(r => r.result && (r.result.time || r.result.forfeit))
- .reverse();
- let running = 0;
- let bonus = 1;
- let lastResult = null;
- for (let i = 0; i < filtered.length; ++i) {
- const score = scores.find(s => s.participant.id === filtered[i].participant.id);
- if (!score) return;
- const result = filtered[i].result;
- const betterThanLast = lastResult === null || result.time < lastResult;
- if (!result.forfeit && betterThanLast) {
- running += bonus;
- lastResult = result.time;
- bonus = 1;
- } else {
- ++bonus;
- }
- if (!result.forfeit) {
- score.score += running;
- }
- }
- });
- return scores.sort(compareScore).reverse();
-};
-