1 import User from './User';
3 export const compareFinished = round => (a, b) => {
4 const a_result = findResult(a, round);
5 const b_result = findResult(b, round);
6 const a_finished = a_result && a_result.has_finished;
7 const b_finished = b_result && b_result.has_finished;
10 return compareUsername(a, b);
17 return compareUsername(a, b);
20 export const comparePlacement = (a, b) => {
21 if (a.placement < b.placement) return -1;
22 if (b.placement < a.placement) return 1;
23 return compareUsername(a, b);
26 export const compareResult = round => (a, b) => {
27 const a_result = findResult(a, round);
28 const b_result = findResult(b, round);
29 const a_placement = a_result && a_result.placement ? a_result.placement : 0;
30 const b_placement = b_result && b_result.placement ? b_result.placement : 0;
33 if (a_placement < b_placement) return -1;
34 if (b_placement < a_placement) return 1;
35 return compareUsername(a, b);
42 return compareUsername(a, b);
45 export const compareUsername = (a, b) => {
46 const a_name = a && a.user ? User.getUserName(a.user) : '';
47 const b_name = b && b.user ? User.getUserName(b.user) : '';
48 return a_name.localeCompare(b_name);
51 export const findResult = (participant, round) => {
52 if (!participant || !participant.user_id) return null;
53 if (!round || !round.results || !round.results.length) return null;
54 return round.results.find(result => result.user_id === participant.user_id);
57 export const isRunner = participant =>
58 participant && participant.roles && participant.roles.includes('runner');
60 export const isTournamentAdmin = participant =>
61 participant && participant.roles && participant.roles.includes('admin');
63 export const isTournamentCrew = participant =>
64 isTournamentAdmin(participant) || isTournamentMonitor(participant);
66 export const isTournamentMonitor = participant =>
67 participant && participant.roles && participant.roles.includes('monitor');
69 export const patchUser = (participant, user) => {
70 if (!participant || !user) return participant;
71 if (participant.user_id != user.id) return participant;
81 export const sortByFinished = (participants, round) => {
82 if (!participants || !participants.length) return participants;
83 if (!round || !round.results || !round.results.length) return participants;
84 return participants.sort(compareFinished(round));
87 export const sortByResult = (participants, round) => {
88 if (!participants || !participants.length) return participants;
89 if (!round || !round.results || !round.results.length) return participants;
90 return participants.sort(compareResult(round));
93 export const sortByUsername = (participants, round) => {
94 if (!participants || !participants.length) return participants;
95 if (!round || !round.results || !round.results.length) return participants;
96 return participants.sort(compareUsername);