]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/User.js
discord discriminator stuff round 2
[alttp.git] / resources / js / helpers / User.js
1 export const compareFinished = round => (a, b) => {
2         const a_result = findResult(a, round);
3         const b_result = findResult(b, round);
4         const a_finished = a_result && a_result.has_finished;
5         const b_finished = b_result && b_result.has_finished;
6         if (a_finished) {
7                 if (b_finished) {
8                         return compareUsername(a, b);
9                 }
10                 return -1;
11         }
12         if (b_finished) {
13                 return 1;
14         }
15         return compareUsername(a, b);
16 };
17
18 export const compareResult = round => (a, b) => {
19         const a_result = findResult(a, round);
20         const b_result = findResult(b, round);
21         const a_placement = a_result && a_result.placement ? a_result.placement : 0;
22         const b_placement = b_result && b_result.placement ? b_result.placement : 0;
23         if (a_placement) {
24                 if (b_placement) {
25                         if (a_placement < b_placement) return -1;
26                         if (b_placement < a_placement) return 1;
27                         return compareUsername(a, b);
28                 }
29                 return -1;
30         }
31         if (b_placement) {
32                 return 1;
33         }
34         return compareUsername(a, b);
35 };
36
37 export const compareUsername = (a, b) => {
38         const a_name = getUserName(a);
39         const b_name = getUserName(b);
40         return a_name.localeCompare(b_name);
41 };
42
43 export const findResult = (user, round) => {
44         if (!user || !user.id) return null;
45         if (!round || !round.results || !round.results.length) return null;
46         return round.results.find(result => result.user_id == user.id);
47 };
48
49 export const getAvatarUrl = user => {
50         if (user && user.avatar) {
51                 if (user.avatar_cached) {
52                         return `/media/avatar/${user.id}/${user.avatar}.png`;
53                 }
54                 return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
55         }
56         return '/default-avatar.png';
57 };
58
59 export const getUserName = user => (user &&
60         (user.nickname || user.discord_nickname || user.username)) || '';
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 };