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 = getUserName(a);
47 const b_name = getUserName(b);
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 getUserName = participant =>
58 participant.user ? User.getUserName(participant.user) : '';
60 export const isRunner = participant =>
61 participant && participant.roles && participant.roles.includes('runner');
63 export const isTournamentAdmin = participant =>
64 participant && participant.roles && participant.roles.includes('admin');
66 export const isTournamentCrew = participant =>
67 isTournamentAdmin(participant) || isTournamentMonitor(participant);
69 export const isTournamentMonitor = participant =>
70 participant && participant.roles && participant.roles.includes('monitor');
72 export const patchUser = (participant, user) => {
73 if (!participant || !user) return participant;
74 if (participant.user_id != user.id) return participant;
84 export const sortByFinished = (participants, round) => {
85 if (!participants || !participants.length) return participants;
86 if (!round || !round.results || !round.results.length) return participants;
87 return participants.sort(compareFinished(round));
90 export const sortByResult = (participants, round) => {
91 if (!participants || !participants.length) return participants;
92 if (!round || !round.results || !round.results.length) return participants;
93 return participants.sort(compareResult(round));
96 export const sortByUsername = (participants, round) => {
97 if (!participants || !participants.length) return participants;
98 if (!round || !round.results || !round.results.length) return participants;
99 return participants.sort(compareUsername);