]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Participant.js
limits for huge tournaments
[alttp.git] / resources / js / helpers / Participant.js
1 import User from './User';
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 comparePlacement = (a, b) => {
21         if (a.placement < b.placement) return -1;
22         if (b.placement < a.placement) return 1;
23         return compareUsername(a, b);
24 };
25
26 export const compareResult = round => (a, b) => {
27         const a_result = findResult(a, round);
28         const b_result = findResult(b, round);
29         const a_placement = a_result && a_result.placement ? a_result.placement : 0;
30         const b_placement = b_result && b_result.placement ? b_result.placement : 0;
31         if (a_placement) {
32                 if (b_placement) {
33                         if (a_placement < b_placement) return -1;
34                         if (b_placement < a_placement) return 1;
35                         return compareUsername(a, b);
36                 }
37                 return -1;
38         }
39         if (b_placement) {
40                 return 1;
41         }
42         return compareUsername(a, b);
43 };
44
45 export const compareUsername = (a, b) => {
46         const a_name = getUserName(a);
47         const b_name = getUserName(b);
48         return a_name.localeCompare(b_name);
49 };
50
51 export const findResult = (participant, round) => {
52         if (!participant || !participant.user_id) return null;
53         if (!round || !round.results || !round.results.length) return null;
54         return round.results.find(result => result.user_id === participant.user_id);
55 };
56
57 export const getUserName = participant =>
58         participant.user ? User.getUserName(participant.user) : '';
59
60 export const isRunner = participant =>
61         participant && participant.roles && participant.roles.includes('runner');
62
63 export const isTournamentAdmin = participant =>
64         participant && participant.roles && participant.roles.includes('admin');
65
66 export const isTournamentCrew = participant =>
67         isTournamentAdmin(participant) || isTournamentMonitor(participant);
68
69 export const isTournamentMonitor = participant =>
70         participant && participant.roles && participant.roles.includes('monitor');
71
72 export const patchUser = (participant, user) => {
73         if (!participant || !user) return participant;
74         if (participant.user_id != user.id) return participant;
75         return {
76                 ...participant,
77                 user: {
78                         ...participant.user,
79                         ...user,
80                 },
81         };
82 };
83
84 export const sortByFinished = (participants, round) => {
85         if (!participants || !participants.length) return participants;
86         if (!round || !round.results || !round.results.length) return participants;
87         return participants.sort(compareFinished(round));
88 };
89
90 export const sortByResult = (participants, round) => {
91         if (!participants || !participants.length) return participants;
92         if (!round || !round.results || !round.results.length) return participants;
93         return participants.sort(compareResult(round));
94 };
95
96 export const sortByUsername = (participants, round) => {
97         if (!participants || !participants.length) return participants;
98         if (!round || !round.results || !round.results.length) return participants;
99         return participants.sort(compareUsername);
100 };
101
102 export default {
103         compareFinished,
104         compareResult,
105         compareUsername,
106         findResult,
107         getUserName,
108         isRunner,
109         isTournamentAdmin,
110         isTournamentCrew,
111         isTournamentMonitor,
112         patchUser,
113         sortByFinished,
114         sortByResult,
115         sortByUsername,
116 };