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