]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/User.js
b94b777091601790abdaf2440027206b8f5c7ea4
[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 => user && user.avatar
50         ? `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`
51         : '/default-avatar.png';
52
53 export const getUserName = user => (user && (user.nickname || user.username)) || '';
54
55 export const hasFinishedRound = (user, round) => {
56         const result = findResult(user, round);
57         return result && result.has_finished;
58 };
59
60 export default {
61         compareFinished,
62         compareResult,
63         compareUsername,
64         findResult,
65         getAvatarUrl,
66         getUserName,
67         hasFinishedRound,
68 };