]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/User.js
periodically sync avatars
[alttp.git] / resources / js / helpers / User.js
1 import moment from 'moment';
2
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;
8         if (a_finished) {
9                 if (b_finished) {
10                         return compareUsername(a, b);
11                 }
12                 return -1;
13         }
14         if (b_finished) {
15                 return 1;
16         }
17         return compareUsername(a, b);
18 };
19
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;
25         if (a_placement) {
26                 if (b_placement) {
27                         if (a_placement < b_placement) return -1;
28                         if (b_placement < a_placement) return 1;
29                         return compareUsername(a, b);
30                 }
31                 return -1;
32         }
33         if (b_placement) {
34                 return 1;
35         }
36         return compareUsername(a, b);
37 };
38
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);
43 };
44
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);
49 };
50
51 export const getAvatarUrl = user => {
52         if (user && user.avatar) {
53                 if (user.avatar_cached) {
54                         return `/media/avatar/${user.id}/${user.avatar}.png`;
55                 }
56                 return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
57         }
58         return '/default-avatar.png';
59 };
60
61 export const getUserName = user => (user && (user.nickname || user.username)) || '';
62
63 export const hasFinishedRound = (user, round) => {
64         const result = findResult(user, round);
65         return result && result.has_finished;
66 };
67
68 export default {
69         compareFinished,
70         compareResult,
71         compareUsername,
72         findResult,
73         getAvatarUrl,
74         getUserName,
75         hasFinishedRound,
76 };