]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/Participant.js
show placement on scoreboard
[alttp.git] / resources / js / helpers / Participant.js
index 8ae225c8570e59845c55891a90ecd2033e4e85b3..d79349bca8776a65ae853943df671f8ffb8f114f 100644 (file)
@@ -1,3 +1,33 @@
+export const compareResult = round => (a, b) => {
+       const a_result = findResult(a, round);
+       const b_result = findResult(b, round);
+       const a_time = a_result && !a_result.forfeit ? a_result.time : 0;
+       const b_time = b_result && !b_result.forfeit ? b_result.time : 0;
+       if (a_time) {
+               if (b_time) {
+                       if (a_time < b_time) return -1;
+                       if (b_time < a_time) return 1;
+                       return 0;
+               }
+               return -1;
+       }
+       if (b_time) {
+               return 1;
+       }
+       const a_forfeit = a_result && a_result.forfeit;
+       const b_forfeit = b_result && b_result.forfeit;
+       if (a_forfeit) {
+               if (b_forfeit) {
+                       return 0;
+               }
+               return -1;
+       }
+       if (b_forfeit) {
+               return 1;
+       }
+       return compareUsername(a, b);
+};
+
 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 : '';
@@ -10,7 +40,36 @@ export const findResult = (participant, round) => {
        return round.results.find(result => result.user_id === participant.user_id);
 };
 
+export const isRunner = participant =>
+       participant && participant.roles && participant.roles.includes('runner');
+
+export const isTournamentAdmin = participant =>
+       participant && participant.roles && participant.roles.includes('admin');
+
+export const patchUser = (participant, user) => {
+       if (!participant || !user) return participant;
+       if (participant.user_id != user.id) return participant;
+       return {
+               ...participant,
+               user: {
+                       ...participant.user,
+                       ...user,
+               },
+       };
+};
+
+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 default {
+       compareResult,
        compareUsername,
        findResult,
+       isRunner,
+       isTournamentAdmin,
+       patchUser,
+       sortByResult,
 };