1 export const getUserName = user => (user &&
2 (user.nickname || user.discord_nickname || user.username)) || '';
4 export const compareUsername = (a, b) => {
5 const a_name = getUserName(a);
6 const b_name = getUserName(b);
7 return a_name.localeCompare(b_name);
10 export const findResult = (user, round) => {
11 if (!user || !user.id) return null;
12 if (!round || !round.results || !round.results.length) return null;
13 return round.results.find(result => result.user_id == user.id);
16 export const compareFinished = round => (a, b) => {
17 const a_result = findResult(a, round);
18 const b_result = findResult(b, round);
19 const a_finished = a_result && a_result.has_finished;
20 const b_finished = b_result && b_result.has_finished;
23 return compareUsername(a, b);
30 return compareUsername(a, b);
33 export const compareResult = round => (a, b) => {
34 const a_result = findResult(a, round);
35 const b_result = findResult(b, round);
36 const a_placement = a_result && a_result.placement ? a_result.placement : 0;
37 const b_placement = b_result && b_result.placement ? b_result.placement : 0;
40 if (a_placement < b_placement) return -1;
41 if (b_placement < a_placement) return 1;
42 return compareUsername(a, b);
49 return compareUsername(a, b);
52 export const getAvatarUrl = user => {
53 if (user && user.avatar) {
54 if (user.avatar_cached) {
55 return `/media/avatar/${user.id}/${user.avatar}.png`;
57 return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
59 return '/default-avatar.png';
62 export const hasFinishedRound = (user, round) => {
63 const result = findResult(user, round);
64 return result && result.has_finished;