X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=resources%2Fjs%2Fhelpers%2FParticipant.js;h=8545e0335c2f37c9fb854138e1a930211d18648e;hb=d32516335ea2534e15256c948e9c38d3de40794b;hp=8ae225c8570e59845c55891a90ecd2033e4e85b3;hpb=ccaa2cf99468fea81efdf28aaa3fc72a278a95f6;p=alttp.git diff --git a/resources/js/helpers/Participant.js b/resources/js/helpers/Participant.js index 8ae225c..8545e03 100644 --- a/resources/js/helpers/Participant.js +++ b/resources/js/helpers/Participant.js @@ -1,3 +1,28 @@ +export const comparePlacement = (a, b) => { + if (a.placement < b.placement) return -1; + if (b.placement < a.placement) 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 = a && a.user && a.user.username ? a.user.username : ''; const b_name = b && b.user && b.user.username ? b.user.username : ''; @@ -10,7 +35,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, };