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