1 export const compareFinished = round => (a, b) => {
2 const a_result = findResult(a, round);
3 const b_result = findResult(b, round);
4 const a_finished = a_result && a_result.has_finished;
5 const b_finished = b_result && b_result.has_finished;
8 return compareUsername(a, b);
15 return compareUsername(a, b);
18 export const comparePlacement = (a, b) => {
19 if (a.placement < b.placement) return -1;
20 if (b.placement < a.placement) return 1;
21 return compareUsername(a, b);
24 export const compareResult = round => (a, b) => {
25 const a_result = findResult(a, round);
26 const b_result = findResult(b, round);
27 const a_placement = a_result && a_result.placement ? a_result.placement : 0;
28 const b_placement = b_result && b_result.placement ? b_result.placement : 0;
31 if (a_placement < b_placement) return -1;
32 if (b_placement < a_placement) return 1;
33 return compareUsername(a, b);
40 return compareUsername(a, b);
43 export const compareUsername = (a, b) => {
44 const a_name = a && a.user && a.user.username ? a.user.username : '';
45 const b_name = b && b.user && b.user.username ? b.user.username : '';
46 return a_name.localeCompare(b_name);
49 export const findResult = (participant, round) => {
50 if (!participant || !participant.user_id) return null;
51 if (!round || !round.results || !round.results.length) return null;
52 return round.results.find(result => result.user_id === participant.user_id);
55 export const isRunner = participant =>
56 participant && participant.roles && participant.roles.includes('runner');
58 export const isTournamentAdmin = participant =>
59 participant && participant.roles && participant.roles.includes('admin');
61 export const isTournamentCrew = participant =>
62 isTournamentAdmin(participant) || isTournamentMonitor(participant);
64 export const isTournamentMonitor = participant =>
65 participant && participant.roles && participant.roles.includes('monitor');
67 export const patchUser = (participant, user) => {
68 if (!participant || !user) return participant;
69 if (participant.user_id != user.id) return participant;
79 export const sortByFinished = (participants, round) => {
80 if (!participants || !participants.length) return participants;
81 if (!round || !round.results || !round.results.length) return participants;
82 return participants.sort(compareFinished(round));
85 export const sortByResult = (participants, round) => {
86 if (!participants || !participants.length) return participants;
87 if (!round || !round.results || !round.results.length) return participants;
88 return participants.sort(compareResult(round));
91 export const sortByUsername = (participants, round) => {
92 if (!participants || !participants.length) return participants;
93 if (!round || !round.results || !round.results.length) return participants;
94 return participants.sort(compareUsername);