From d2d1231d5cea49224df581aed9d9e77f9deb832b Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 27 Oct 2022 14:06:55 +0200 Subject: [PATCH] sort open-async results, if allowed --- resources/js/components/results/List.js | 5 ++- resources/js/helpers/Participant.js | 2 ++ resources/js/helpers/Result.js | 25 ++++++++++++++ resources/js/helpers/User.js | 45 +++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/resources/js/components/results/List.js b/resources/js/components/results/List.js index 3d1bdfa..fb26fa5 100644 --- a/resources/js/components/results/List.js +++ b/resources/js/components/results/List.js @@ -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
{results.map(result => { + 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, diff --git a/resources/js/helpers/User.js b/resources/js/helpers/User.js index f13e119..b94b777 100644 --- a/resources/js/helpers/User.js +++ b/resources/js/helpers/User.js @@ -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, -- 2.39.2