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 return scores.sort(compareScore).reverse();
38 export const compareScore = (a, b) => {
39 const a_score = a && a.score ? a.score : 0;
40 const b_score = b && b.score ? b.score : 0;
41 if (a_score < b_score) return -1;
42 if (b_score < a_score) return 1;
43 return Participant.compareUsername(a.participant, b.participant) * -1;
46 export const findParticipant = (tournament, user) => {
47 if (!tournament || !tournament.participants || !tournament.participants.length) return null;
48 if (!user || !user.id) return null;
49 return tournament.participants.find(p => p.user_id == user.id);
52 export const getRunners = tournament => {
53 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
54 return tournament.participants
55 .filter(Participant.isRunner)
56 .sort(Participant.compareUsername);
59 export const getTournamentAdmins = tournament => {
60 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
61 return tournament.participants
62 .filter(Participant.isTournamentAdmin)
63 .sort(Participant.compareUsername);
66 export const hasRunners = tournament => {
67 return getRunners(tournament).length > 0;
70 export const hasTournamentAdmins = tournament => {
71 return getTournamentAdmins(tournament).length > 0;
74 export const patchResult = (tournament, result) => {
75 if (!tournament || !tournament.rounds) return tournament;
78 rounds: tournament.rounds.map(round =>
79 round.id === result.round_id
80 ? Round.patchResult(round, result)
86 export const patchRound = (tournament, round) => {
87 if (!tournament) return tournament;
90 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
94 export const patchUser = (tournament, user) => {
95 if (!tournament || !tournament.participants || !user) return tournament;
96 if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
99 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
103 export const sortParticipants = tournament => {
104 if (!tournament || !tournament.participants || !tournament.participants.length) {
109 participants: tournament.participants.sort(Participant.compareUsername),