]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Participant.js
fix big key size
[alttp.git] / resources / js / helpers / Participant.js
1 export const compareResult = round => (a, b) => {
2         const a_result = findResult(a, round);
3         const b_result = findResult(b, round);
4         const a_time = a_result && !a_result.forfeit ? a_result.time : 0;
5         const b_time = b_result && !b_result.forfeit ? b_result.time : 0;
6         if (a_time) {
7                 if (b_time) {
8                         if (a_time < b_time) return -1;
9                         if (b_time < a_time) return 1;
10                         return 0;
11                 }
12                 return -1;
13         }
14         if (b_time) {
15                 return 1;
16         }
17         const a_forfeit = a_result && a_result.forfeit;
18         const b_forfeit = b_result && b_result.forfeit;
19         if (a_forfeit) {
20                 if (b_forfeit) {
21                         return 0;
22                 }
23                 return -1;
24         }
25         if (b_forfeit) {
26                 return 1;
27         }
28         return compareUsername(a, b);
29 };
30
31 export const compareUsername = (a, b) => {
32         const a_name = a && a.user && a.user.username ? a.user.username : '';
33         const b_name = b && b.user && b.user.username ? b.user.username : '';
34         return a_name.localeCompare(b_name);
35 };
36
37 export const findResult = (participant, round) => {
38         if (!participant || !participant.user_id) return null;
39         if (!round || !round.results || !round.results.length) return null;
40         return round.results.find(result => result.user_id === participant.user_id);
41 };
42
43 export const isRunner = participant =>
44         participant && participant.roles && participant.roles.includes('runner');
45
46 export const isTournamentAdmin = participant =>
47         participant && participant.roles && participant.roles.includes('admin');
48
49 export const patchUser = (participant, user) => {
50         if (!participant || !user) return participant;
51         if (participant.user_id != user.id) return participant;
52         return {
53                 ...participant,
54                 user: {
55                         ...participant.user,
56                         ...user,
57                 },
58         };
59 };
60
61 export const sortByResult = (participants, round) => {
62         if (!participants || !participants.length) return participants;
63         if (!round || !round.results || !round.results.length) return participants;
64         return participants.sort(compareResult(round));
65 };
66
67 export default {
68         compareResult,
69         compareUsername,
70         findResult,
71         isRunner,
72         isTournamentAdmin,
73         patchUser,
74         sortByResult,
75 };