]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/Participant.js
application admin UI
[alttp.git] / resources / js / helpers / Participant.js
index 4b4b6ef51c5ee0cb72a73b9ddaca50e2d2321e1a..d7bdd7c4a41055a8f7d067c2f44c1f02ffc1a5b2 100644 (file)
@@ -1,3 +1,22 @@
+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;
@@ -24,8 +43,8 @@ export const compareResult = round => (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 : '';
+       const a_name = a && a.user ? User.getUserName(a.user) : '';
+       const b_name = b && b.user ? User.getUserName(b.user) : '';
        return a_name.localeCompare(b_name);
 };
 
@@ -59,13 +78,26 @@ export const patchUser = (participant, user) => {
        };
 };
 
+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,