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