]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
some discord bot stuff
[alttp.git] / resources / js / helpers / Tournament.js
1 import Application from './Application';
2 import Participant from './Participant';
3 import Round from './Round';
4
5 export const compareScore = (a, b) => {
6         const a_score = a && a.score ? a.score : 0;
7         const b_score = b && b.score ? b.score : 0;
8         if (a_score < b_score) return -1;
9         if (b_score < a_score) return 1;
10         return Participant.compareUsername(a.participant, b.participant) * -1;
11 };
12
13 export const findParticipant = (tournament, user) => {
14         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
15         if (!user || !user.id) return null;
16         return tournament.participants.find(p => p.user_id == user.id);
17 };
18
19 export const getPendingApplications = tournament => {
20         if (!tournament || !tournament.applications || !tournament.applications.length) return [];
21         return tournament.applications
22                 .filter(Application.isPending)
23                 .sort(Application.compareUsername);
24 };
25
26 export const getRunners = tournament => {
27         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
28         return tournament.participants
29                 .filter(Participant.isRunner)
30                 .sort(Participant.compareUsername);
31 };
32
33 export const hasScoreboard = tournament => !!(tournament && tournament.type === 'signup-async');
34
35 export const hasSignup = tournament => !!(tournament && tournament.type === 'signup-async');
36
37 export const getScoreTable = tournament => {
38         if (!tournament || !tournament.rounds || !tournament.rounds.length) return [];
39         const runners = getRunners(tournament);
40         if (!runners.length) return [];
41         const running = {};
42         runners.forEach(participant => {
43                 running[participant.id] = 0;
44         });
45         const data = [...tournament.rounds, {}].reverse().map(round => {
46                 const entry = { number: round.number ? `#${round.number}` : '' };
47                 runners.forEach(participant => {
48                         const result = Participant.findResult(participant, round);
49                         if (result && result.score) {
50                                 running[participant.id] += result.score;
51                         }
52                         entry[Participant.getUserName(participant)] = running[participant.id];
53                 });
54                 return entry;
55         });
56         return data;
57 };
58
59 export const getTournamentAdmins = tournament => {
60         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
61         return tournament.participants
62                 .filter(Participant.isTournamentAdmin)
63                 .sort(Participant.compareUsername);
64 };
65
66 export const getTournamentCrew = tournament => {
67         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
68         return tournament.participants
69                 .filter(Participant.isTournamentCrew)
70                 .sort(Participant.compareUsername);
71 };
72
73 export const getTournamentMonitors = tournament => {
74         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
75         return tournament.participants
76                 .filter(Participant.isTournamentMonitor)
77                 .sort(Participant.compareUsername);
78 };
79
80 export const hasRunners = tournament => {
81         return getRunners(tournament).length > 0;
82 };
83
84 export const hasTournamentAdmins = tournament => {
85         return getTournamentAdmins(tournament).length > 0;
86 };
87
88 export const hasTournamentCrew = tournament => {
89         return getTournamentCrew(tournament).length > 0;
90 };
91
92 export const hasTournamentMonitors = tournament => {
93         return getTournamentMonitors(tournament).length > 0;
94 };
95
96 export const patchApplication = (tournament, application) => {
97         if (!tournament) return tournament;
98         if (!tournament.applications || !tournament.applications.length) {
99                 return {
100                         ...tournament,
101                         applications: [application],
102                 };
103         }
104         if (!tournament.applications.find(a => a.user_id == application.user_id)) {
105                 return {
106                         ...tournament,
107                         applications: [...tournament.applications, application],
108                 };
109         }
110         return {
111                 ...tournament,
112                 applications: tournament.applications.map(
113                         a => a.user_id === application.user_id ? application : a,
114                 ),
115         };
116 };
117
118 export const patchParticipant = (tournament, participant) => {
119         if (!tournament) return tournament;
120         if (!tournament.participants || !tournament.participants.length) {
121                 return {
122                         ...tournament,
123                         participants: [participant],
124                 };
125         }
126         if (!tournament.participants.find(p => p.id === participant.id)) {
127                 return {
128                         ...tournament,
129                         participants: [...tournament.participants, participant],
130                 };
131         }
132         return {
133                 ...tournament,
134                 participants: tournament.participants.map(
135                         p => p.id === participant.id ? participant : p,
136                 ),
137         };
138 };
139
140 export const patchResult = (tournament, result) => {
141         if (!tournament || !tournament.rounds) return tournament;
142         return {
143                 ...tournament,
144                 rounds: tournament.rounds.map(round =>
145                         round.id === result.round_id
146                                 ? Round.patchResult(round, result)
147                                 : round
148                 ),
149         };
150 };
151
152 export const patchRound = (tournament, round) => {
153         if (!tournament) return tournament;
154         return {
155                 ...tournament,
156                 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
157         };
158 };
159
160 export const patchUser = (tournament, user) => {
161         if (!tournament || !tournament.participants || !user) return tournament;
162         if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
163         return {
164                 ...tournament,
165                 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
166         };
167 };
168
169 export const removeApplication = (tournament, id) => {
170         if (!tournament || !tournament.applications || !tournament.applications.find(a => a.id == id)) {
171                 return tournament;
172         }
173         return {
174                 ...tournament,
175                 applications: tournament.applications.filter(a => a.id != id),
176         };
177 };
178
179 export const sortParticipants = tournament => {
180         if (!tournament || !tournament.participants || !tournament.participants.length) {
181                 return tournament;
182         }
183         return {
184                 ...tournament,
185                 participants: tournament.participants.sort(Participant.compareUsername),
186         };
187 };
188
189 export default {
190         compareScore,
191         findParticipant,
192         getRunners,
193         getTournamentAdmins,
194         getTournamentCrew,
195         getTournamentMonitors,
196         hasRunners,
197         hasScoreboard,
198         hasSignup,
199         hasTournamentAdmins,
200         hasTournamentCrew,
201         hasTournamentMonitors,
202         patchResult,
203         patchRound,
204         patchUser,
205         sortParticipants,
206 };