]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/User.js
adlib chat
[alttp.git] / resources / js / helpers / User.js
1 export const getUserName = user => (user &&
2         (user.nickname || user.discord_nickname || user.username)) || '';
3
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);
8 };
9
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);
14 };
15
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;
21         if (a_finished) {
22                 if (b_finished) {
23                         return compareUsername(a, b);
24                 }
25                 return -1;
26         }
27         if (b_finished) {
28                 return 1;
29         }
30         return compareUsername(a, b);
31 };
32
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;
38         if (a_placement) {
39                 if (b_placement) {
40                         if (a_placement < b_placement) return -1;
41                         if (b_placement < a_placement) return 1;
42                         return compareUsername(a, b);
43                 }
44                 return -1;
45         }
46         if (b_placement) {
47                 return 1;
48         }
49         return compareUsername(a, b);
50 };
51
52 export const getAvatarUrl = user => {
53         if (user && user.avatar) {
54                 if (user.avatar_cached) {
55                         return `/media/avatar/${user.id}/${user.avatar}.png`;
56                 }
57                 return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
58         }
59         return '/default-avatar.png';
60 };
61
62 export const hasFinishedRound = (user, round) => {
63         const result = findResult(user, round);
64         return result && result.has_finished;
65 };
66
67 export default {
68         compareFinished,
69         compareResult,
70         compareUsername,
71         findResult,
72         getAvatarUrl,
73         getUserName,
74         hasFinishedRound,
75 };