1 import moment from 'moment';
3 export const compareFinished = round => (a, b) => {
4 const a_result = findResult(a, round);
5 const b_result = findResult(b, round);
6 const a_finished = a_result && a_result.has_finished;
7 const b_finished = b_result && b_result.has_finished;
10 return compareUsername(a, b);
17 return compareUsername(a, b);
20 export const compareResult = round => (a, b) => {
21 const a_result = findResult(a, round);
22 const b_result = findResult(b, round);
23 const a_placement = a_result && a_result.placement ? a_result.placement : 0;
24 const b_placement = b_result && b_result.placement ? b_result.placement : 0;
27 if (a_placement < b_placement) return -1;
28 if (b_placement < a_placement) return 1;
29 return compareUsername(a, b);
36 return compareUsername(a, b);
39 export const compareUsername = (a, b) => {
40 const a_name = getUserName(a);
41 const b_name = getUserName(b);
42 return a_name.localeCompare(b_name);
45 export const findResult = (user, round) => {
46 if (!user || !user.id) return null;
47 if (!round || !round.results || !round.results.length) return null;
48 return round.results.find(result => result.user_id == user.id);
51 export const getAvatarUrl = user => {
52 if (user && user.avatar) {
53 if (user.avatar_cached) {
54 return `/media/avatar/${user.id}/${user.avatar}.png`;
56 return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
58 return '/default-avatar.png';
61 export const getUserName = user => (user && (user.nickname || user.username)) || '';
63 export const hasFinishedRound = (user, round) => {
64 const result = findResult(user, round);
65 return result && result.has_finished;