+import User from './User';
+
+export const compareFinished = round => (a, b) => {
+ const a_result = findResult(a, round);
+ const b_result = findResult(b, round);
+ const a_finished = a_result && a_result.has_finished;
+ const b_finished = b_result && b_result.has_finished;
+ if (a_finished) {
+ if (b_finished) {
+ return compareUsername(a, b);
+ }
+ return -1;
+ }
+ if (b_finished) {
+ return 1;
+ }
+ return compareUsername(a, b);
+};
+
export const comparePlacement = (a, b) => {
if (a.placement < b.placement) return -1;
if (b.placement < a.placement) return 1;
};
export const compareUsername = (a, b) => {
- const a_name = a && a.user && a.user.username ? a.user.username : '';
- const b_name = b && b.user && b.user.username ? b.user.username : '';
+ const a_name = getUserName(a);
+ const b_name = getUserName(b);
return a_name.localeCompare(b_name);
};
return round.results.find(result => result.user_id === participant.user_id);
};
+export const getUserName = participant =>
+ participant.user ? User.getUserName(participant.user) : '';
+
export const isRunner = participant =>
participant && participant.roles && participant.roles.includes('runner');
};
};
+export const sortByFinished = (participants, round) => {
+ if (!participants || !participants.length) return participants;
+ if (!round || !round.results || !round.results.length) return participants;
+ return participants.sort(compareFinished(round));
+};
+
export const sortByResult = (participants, round) => {
if (!participants || !participants.length) return participants;
if (!round || !round.results || !round.results.length) return participants;
return participants.sort(compareResult(round));
};
+export const sortByUsername = (participants, round) => {
+ if (!participants || !participants.length) return participants;
+ if (!round || !round.results || !round.results.length) return participants;
+ return participants.sort(compareUsername);
+};
+
export default {
+ compareFinished,
compareResult,
compareUsername,
findResult,
+ getUserName,
isRunner,
isTournamentAdmin,
isTournamentCrew,
isTournamentMonitor,
patchUser,
+ sortByFinished,
sortByResult,
+ sortByUsername,
};