1 import Participant from './Participant';
2 import Round from './Round';
4 export const calculateScores = tournament => {
5 const runners = getRunners(tournament);
6 const scores = runners.map(participant => ({ participant, score: 0 }));
7 if (!scores.length) return scores;
8 if (!tournament.rounds || !tournament.rounds.length) return scores;
9 tournament.rounds.forEach(round => {
10 const filtered = Participant
11 .sortByResult(runners, round)
12 .map(p => ({ participant: p, result: Participant.findResult(p, round) }))
13 .filter(r => r.result && (r.result.time || r.result.forfeit))
17 let lastResult = null;
18 for (let i = 0; i < filtered.length; ++i) {
19 const score = scores.find(s => s.participant.id === filtered[i].participant.id);
21 const result = filtered[i].result;
22 const betterThanLast = lastResult === null || result.time < lastResult;
23 if (!result.forfeit && betterThanLast) {
25 lastResult = result.time;
30 if (!result.forfeit) {
31 score.score += running;
35 const sorted = scores.sort(compareScore);
36 let placement = scores.length;
38 let lastScore = sorted[0].score;
39 for (let i = 0; i < sorted.length; ++i) {
40 if (sorted[i].score > lastScore) {
43 lastScore = sorted[i].score;
47 sorted[i].placement = placement;
49 return sorted.reverse();
52 export const compareScore = (a, b) => {
53 const a_score = a && a.score ? a.score : 0;
54 const b_score = b && b.score ? b.score : 0;
55 if (a_score < b_score) return -1;
56 if (b_score < a_score) return 1;
57 return Participant.compareUsername(a.participant, b.participant) * -1;
60 export const findParticipant = (tournament, user) => {
61 if (!tournament || !tournament.participants || !tournament.participants.length) return null;
62 if (!user || !user.id) return null;
63 return tournament.participants.find(p => p.user_id == user.id);
66 export const getRunners = tournament => {
67 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
68 return tournament.participants
69 .filter(Participant.isRunner)
70 .sort(Participant.compareUsername);
73 export const getTournamentAdmins = tournament => {
74 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
75 return tournament.participants
76 .filter(Participant.isTournamentAdmin)
77 .sort(Participant.compareUsername);
80 export const hasRunners = tournament => {
81 return getRunners(tournament).length > 0;
84 export const hasTournamentAdmins = tournament => {
85 return getTournamentAdmins(tournament).length > 0;
88 export const patchResult = (tournament, result) => {
89 if (!tournament || !tournament.rounds) return tournament;
92 rounds: tournament.rounds.map(round =>
93 round.id === result.round_id
94 ? Round.patchResult(round, result)
100 export const patchRound = (tournament, round) => {
101 if (!tournament) return tournament;
104 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
108 export const patchUser = (tournament, user) => {
109 if (!tournament || !tournament.participants || !user) return tournament;
110 if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
113 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
117 export const sortParticipants = tournament => {
118 if (!tournament || !tournament.participants || !tournament.participants.length) {
123 participants: tournament.participants.sort(Participant.compareUsername),