X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhelpers%2FUser.js;h=b94b777091601790abdaf2440027206b8f5c7ea4;hb=d2d1231d5cea49224df581aed9d9e77f9deb832b;hp=f13e11977f652529284872d447c9454b2a7e53c8;hpb=278c9b9d68b2a5e57068a12b3f7d9611ac42222b;p=alttp.git 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,