1 import Participant from './Participant';
2 import Round from './Round';
4 export const compareScore = (a, b) => {
5 const a_score = a && a.score ? a.score : 0;
6 const b_score = b && b.score ? b.score : 0;
7 if (a_score < b_score) return -1;
8 if (b_score < a_score) return 1;
9 return Participant.compareUsername(a.participant, b.participant) * -1;
12 export const findParticipant = (tournament, user) => {
13 if (!tournament || !tournament.participants || !tournament.participants.length) return null;
14 if (!user || !user.id) return null;
15 return tournament.participants.find(p => p.user_id == user.id);
18 export const getRunners = tournament => {
19 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
20 return tournament.participants
21 .filter(Participant.isRunner)
22 .sort(Participant.compareUsername);
25 export const getTournamentAdmins = tournament => {
26 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
27 return tournament.participants
28 .filter(Participant.isTournamentAdmin)
29 .sort(Participant.compareUsername);
32 export const getTournamentCrew = tournament => {
33 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
34 return tournament.participants
35 .filter(Participant.isTournamentCrew)
36 .sort(Participant.compareUsername);
39 export const getTournamentMonitors = tournament => {
40 if (!tournament || !tournament.participants || !tournament.participants.length) return [];
41 return tournament.participants
42 .filter(Participant.isTournamentMonitor)
43 .sort(Participant.compareUsername);
46 export const hasRunners = tournament => {
47 return getRunners(tournament).length > 0;
50 export const hasTournamentAdmins = tournament => {
51 return getTournamentAdmins(tournament).length > 0;
54 export const hasTournamentCrew = tournament => {
55 return getTournamentCrew(tournament).length > 0;
58 export const hasTournamentMonitors = tournament => {
59 return getTournamentMonitors(tournament).length > 0;
62 export const patchParticipant = (tournament, participant) => {
63 if (!tournament) return tournament;
64 if (!tournament.participants || !tournament.participants.length) {
67 participants: [participant],
70 if (!tournament.participants.find(p => p.id === participant.id)) {
73 participants: [...tournament.participants, participant],
78 participants: tournament.participants.map(
79 p => p.id === participant.id ? participant : p,
84 export const patchResult = (tournament, result) => {
85 if (!tournament || !tournament.rounds) return tournament;
88 rounds: tournament.rounds.map(round =>
89 round.id === result.round_id
90 ? Round.patchResult(round, result)
96 export const patchRound = (tournament, round) => {
97 if (!tournament) return tournament;
100 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
104 export const patchUser = (tournament, user) => {
105 if (!tournament || !tournament.participants || !user) return tournament;
106 if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
109 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
113 export const sortParticipants = tournament => {
114 if (!tournament || !tournament.participants || !tournament.participants.length) {
119 participants: tournament.participants.sort(Participant.compareUsername),
129 getTournamentMonitors,
133 hasTournamentMonitors,