X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhelpers%2FUser.js;h=b94b777091601790abdaf2440027206b8f5c7ea4;hb=d2d1231d5cea49224df581aed9d9e77f9deb832b;hp=c6a99c9dfe4d60e5918ef93a345615111ecc6316;hpb=33cc4da565ffb07ee4f8d9cbafb248629b85e65a;p=alttp.git diff --git a/resources/js/helpers/User.js b/resources/js/helpers/User.js index c6a99c9..b94b777 100644 --- a/resources/js/helpers/User.js +++ b/resources/js/helpers/User.js @@ -1,20 +1,68 @@ +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; return round.results.find(result => result.user_id == user.id); }; -export const getAvatarUrl = user => user.avatar +export const getAvatarUrl = user => user && user.avatar ? `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png` : '/default-avatar.png'; +export const getUserName = user => (user && (user.nickname || user.username)) || ''; + export const hasFinishedRound = (user, round) => { const result = findResult(user, round); return result && result.has_finished; }; export default { + compareFinished, + compareResult, + compareUsername, findResult, getAvatarUrl, + getUserName, hasFinishedRound, };