1 import Participant from './Participant';
2 import Round from './Round';
4 export const calculateScores = tournament => {
5 const scores = getRunners(tournament).map(participant => ({ participant, score: 0 }));
6 if (!tournament.rounds || !tournament.rounds.length) return scores;
7 tournament.rounds.forEach(round => {
8 const filtered = Participant
9 .sortByResult(tournament.participants, round)
10 .map(p => ({ participant: p, result: Participant.findResult(p, round) }))
11 .filter(r => r.result && (r.result.time || r.result.forfeit))
15 let lastResult = null;
16 for (let i = 0; i < filtered.length; ++i) {
17 const score = scores.find(s => s.participant.id === filtered[i].participant.id);
19 const result = filtered[i].result;
20 const betterThanLast = lastResult === null || result.time < lastResult;
21 if (!result.forfeit && betterThanLast) {
23 lastResult = result.time;
28 if (!result.forfeit) {
29 score.score += running;
33 return scores.sort(compareScore).reverse();
36 export const compareScore = (a, b) => {
37 const a_score = a && a.score ? a.score : 0;
38 const b_score = b && b.score ? b.score : 0;
39 if (a_score < b_score) return -1;
40 if (b_score < a_score) return 1;
41 return Participant.compareUsername(a.participant, b.participant) * -1;
44 export const findParticipant = (tournament, user) => {
45 if (!tournament || !tournament.participants || !tournament.participants.length) return null;
46 if (!user || !user.id) return null;
47 return tournament.participants.find(p => p.user_id == user.id);
50 export const getRunners = tournament => {
51 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
52 return tournament.participants
53 .filter(Participant.isRunner)
54 .sort(Participant.compareUsername);
57 export const getTournamentAdmins = tournament => {
58 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
59 return tournament.participants
60 .filter(Participant.isTournamentAdmin)
61 .sort(Participant.compareUsername);
64 export const hasRunners = tournament => {
65 return getRunners(tournament).length > 0;
68 export const hasTournamentAdmins = tournament => {
69 return getTournamentAdmins(tournament).length > 0;
72 export const patchResult = (tournament, result) => {
73 if (!tournament || !tournament.rounds) return tournament;
76 rounds: tournament.rounds.map(round =>
77 round.id === result.round_id
78 ? Round.patchResult(round, result)
84 export const patchRound = (tournament, round) => {
85 if (!tournament) return tournament;
88 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
92 export const patchUser = (tournament, user) => {
93 if (!tournament || !tournament.participants || !user) return tournament;
94 if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
97 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
101 export const sortParticipants = tournament => {
102 if (!tournament || !tournament.participants || !tournament.participants.length) {
107 participants: tournament.participants.sort(Participant.compareUsername),