X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhelpers%2FUser.js;h=0705bed2d35013f25bcd14154a2361a861d8f56f;hb=816690cc4fadff954f4407999550f7feec2884da;hp=c6a99c9dfe4d60e5918ef93a345615111ecc6316;hpb=33cc4da565ffb07ee4f8d9cbafb248629b85e65a;p=alttp.git diff --git a/resources/js/helpers/User.js b/resources/js/helpers/User.js index c6a99c9..0705bed 100644 --- a/resources/js/helpers/User.js +++ b/resources/js/helpers/User.js @@ -1,12 +1,63 @@ +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 - ? `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png` - : '/default-avatar.png'; +export const getAvatarUrl = user => { + if (user && user.avatar) { + if (user.avatar_cached) { + return `/media/avatar/${user.id}/${user.avatar}.png`; + } + return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`; + } + return '/default-avatar.png'; +}; + +export const getUserName = user => (user && + (user.nickname || user.discord_nickname || user.username)) || ''; export const hasFinishedRound = (user, round) => { const result = findResult(user, round); @@ -14,7 +65,11 @@ export const hasFinishedRound = (user, round) => { }; export default { + compareFinished, + compareResult, + compareUsername, findResult, getAvatarUrl, + getUserName, hasFinishedRound, };