]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
25981d87fe0c8e11d58a82a342218d47a2dd2864
[alttp.git] / resources / js / helpers / Tournament.js
1 import Participant from './Participant';
2 import Round from './Round';
3
4 export const compareScore = (a, b) => {
5         const a_score = a && a.score ? a.score : 0;
6         const b_score = b && b.score ? b.score : 0;
7         if (a_score < b_score) return -1;
8         if (b_score < a_score) return 1;
9         return Participant.compareUsername(a.participant, b.participant) * -1;
10 };
11
12 export const findParticipant = (tournament, user) => {
13         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
14         if (!user || !user.id) return null;
15         return tournament.participants.find(p => p.user_id == user.id);
16 };
17
18 export const getRunners = tournament => {
19         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
20         return tournament.participants
21                 .filter(Participant.isRunner)
22                 .sort(Participant.compareUsername);
23 };
24
25 export const getTournamentAdmins = tournament => {
26         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
27         return tournament.participants
28                 .filter(Participant.isTournamentAdmin)
29                 .sort(Participant.compareUsername);
30 };
31
32 export const getTournamentCrew = tournament => {
33         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
34         return tournament.participants
35                 .filter(Participant.isTournamentCrew)
36                 .sort(Participant.compareUsername);
37 };
38
39 export const getTournamentMonitors = tournament => {
40         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
41         return tournament.participants
42                 .filter(Participant.isTournamentMonitor)
43                 .sort(Participant.compareUsername);
44 };
45
46 export const hasRunners = tournament => {
47         return getRunners(tournament).length > 0;
48 };
49
50 export const hasTournamentAdmins = tournament => {
51         return getTournamentAdmins(tournament).length > 0;
52 };
53
54 export const hasTournamentCrew = tournament => {
55         return getTournamentCrew(tournament).length > 0;
56 };
57
58 export const hasTournamentMonitors = tournament => {
59         return getTournamentMonitors(tournament).length > 0;
60 };
61
62 export const patchApplication = (tournament, application) => {
63         if (!tournament) return tournament;
64         if (!tournament.applications || !tournament.applications.length) {
65                 return {
66                         ...tournament,
67                         applications: [application],
68                 };
69         }
70         if (!tournament.applications.find(a => a.user_id == application.user_id)) {
71                 return {
72                         ...tournament,
73                         applications: [...tournament.applications, application],
74                 };
75         }
76         return {
77                 ...tournament,
78                 applications: tournament.applications.map(
79                         a => a.user_id === application.user_id ? application : a,
80                 ),
81         };
82 };
83
84 export const patchParticipant = (tournament, participant) => {
85         if (!tournament) return tournament;
86         if (!tournament.participants || !tournament.participants.length) {
87                 return {
88                         ...tournament,
89                         participants: [participant],
90                 };
91         }
92         if (!tournament.participants.find(p => p.id === participant.id)) {
93                 return {
94                         ...tournament,
95                         participants: [...tournament.participants, participant],
96                 };
97         }
98         return {
99                 ...tournament,
100                 participants: tournament.participants.map(
101                         p => p.id === participant.id ? participant : p,
102                 ),
103         };
104 };
105
106 export const patchResult = (tournament, result) => {
107         if (!tournament || !tournament.rounds) return tournament;
108         return {
109                 ...tournament,
110                 rounds: tournament.rounds.map(round =>
111                         round.id === result.round_id
112                                 ? Round.patchResult(round, result)
113                                 : round
114                 ),
115         };
116 };
117
118 export const patchRound = (tournament, round) => {
119         if (!tournament) return tournament;
120         return {
121                 ...tournament,
122                 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
123         };
124 };
125
126 export const patchUser = (tournament, user) => {
127         if (!tournament || !tournament.participants || !user) return tournament;
128         if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
129         return {
130                 ...tournament,
131                 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
132         };
133 };
134
135 export const sortParticipants = tournament => {
136         if (!tournament || !tournament.participants || !tournament.participants.length) {
137                 return tournament;
138         }
139         return {
140                 ...tournament,
141                 participants: tournament.participants.sort(Participant.compareUsername),
142         };
143 };
144
145 export default {
146         compareScore,
147         findParticipant,
148         getRunners,
149         getTournamentAdmins,
150         getTournamentCrew,
151         getTournamentMonitors,
152         hasRunners,
153         hasTournamentAdmins,
154         hasTournamentCrew,
155         hasTournamentMonitors,
156         patchResult,
157         patchRound,
158         patchUser,
159         sortParticipants,
160 };