]> git.localhorst.tv Git - alttp.git/commitdiff
sort open-async results, if allowed
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Oct 2022 12:06:55 +0000 (14:06 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Oct 2022 12:06:55 +0000 (14:06 +0200)
resources/js/components/results/List.js
resources/js/helpers/Participant.js
resources/js/helpers/Result.js
resources/js/helpers/User.js

index 3d1bdfa8f27c9ff0147832934ce81975a8bee2c0..fb26fa56f264d94c2c393a6dc70af0e23dea91f3 100644 (file)
@@ -5,11 +5,14 @@ import Item from './Item';
 import { sortByFinished, sortByResult } from '../../helpers/Participant';
 import { maySeeResults } from '../../helpers/permissions';
 import { getRunners } from '../../helpers/Tournament';
+import { compareResult } from '../../helpers/Result';
 import { withUser } from '../../helpers/UserContext';
 
 const List = ({ round, tournament, user }) => {
        if (tournament.type === 'open-async') {
-               const results = round.results || [];
+               const results = maySeeResults(user, tournament, round)
+                       ? (round.results || []).sort(compareResult)
+                       : round.results || [];
                return <div className="results d-flex flex-wrap">
                        {results.map(result =>
                                <Item
index 7d649526924a35c0812938a07b85aa4bd978f901..1a24f8efef9ee242be68ec7e0af9a271fda6010a 100644 (file)
@@ -110,5 +110,7 @@ export default {
        isTournamentCrew,
        isTournamentMonitor,
        patchUser,
+       sortByFinished,
        sortByResult,
+       sortByUsername,
 };
index 205a42b27325050800c4fe55f728f43de1d0c8c0..b1fb3f90845e914e335113d3cd57b416b4ac1f54 100644 (file)
@@ -1,6 +1,29 @@
 import React from 'react';
 import Icon from '../components/common/Icon';
 
+export const compareResult = (a, b) => {
+       const a_placement = a && a.placement ? a.placement : 0;
+       const b_placement = b && b.placement ? b.placement : 0;
+       if (a_placement) {
+               if (b_placement) {
+                       if (a_placement < b_placement) return -1;
+                       if (b_placement < a_placement) return 1;
+                       return compareUsername(a, b);
+               }
+               return -1;
+       }
+       if (b_placement) {
+               return 1;
+       }
+       return compareUsername(a, b);
+};
+
+export const compareUsername = (a, b) => {
+       const a_name = (a && a.user && a.user.username) || '';
+       const b_name = (b && b.user && b.user.username) || '';
+       return a_name.localeCompare(b_name);
+};
+
 export const formatTime = result => {
        const hours = `${Math.floor(result.time / 60 / 60)}`;
        let minutes = `${Math.floor((result.time / 60) % 60)}`;
@@ -52,6 +75,8 @@ export const parseTime = str => {
 };
 
 export default {
+       compareResult,
+       compareUsername,
        formatTime,
        getIcon,
        getTime,
index f13e11977f652529284872d447c9454b2a7e53c8..b94b777091601790abdaf2440027206b8f5c7ea4 100644 (file)
@@ -1,3 +1,45 @@
+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 compareResult = round => (a, b) => {
+       const a_result = findResult(a, round);
+       const b_result = findResult(b, round);
+       const a_placement = a_result && a_result.placement ? a_result.placement : 0;
+       const b_placement = b_result && b_result.placement ? b_result.placement : 0;
+       if (a_placement) {
+               if (b_placement) {
+                       if (a_placement < b_placement) return -1;
+                       if (b_placement < a_placement) return 1;
+                       return compareUsername(a, b);
+               }
+               return -1;
+       }
+       if (b_placement) {
+               return 1;
+       }
+       return compareUsername(a, b);
+};
+
+export const compareUsername = (a, b) => {
+       const a_name = getUserName(a);
+       const b_name = getUserName(b);
+       return a_name.localeCompare(b_name);
+};
+
 export const findResult = (user, round) => {
        if (!user || !user.id) return null;
        if (!round || !round.results || !round.results.length) return null;
@@ -16,6 +58,9 @@ export const hasFinishedRound = (user, round) => {
 };
 
 export default {
+       compareFinished,
+       compareResult,
+       compareUsername,
        findResult,
        getAvatarUrl,
        getUserName,