]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
server calculated scoring
[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 hasRunners = tournament => {
33         return getRunners(tournament).length > 0;
34 };
35
36 export const hasTournamentAdmins = tournament => {
37         return getTournamentAdmins(tournament).length > 0;
38 };
39
40 export const patchParticipant = (tournament, participant) => {
41         if (!tournament) return tournament;
42         if (!tournament.participants || !tournament.participants.length) {
43                 return {
44                         ...tournament,
45                         participants: [participant],
46                 };
47         }
48         if (!tournament.participants.find(p => p.id === participant.id)) {
49                 return {
50                         ...tournament,
51                         participants: [...tournament.participants, participant],
52                 };
53         }
54         return {
55                 ...tournament,
56                 participants: tournament.participants.map(
57                         p => p.id === participant.id ? participant : p,
58                 ),
59         };
60 };
61
62 export const patchResult = (tournament, result) => {
63         if (!tournament || !tournament.rounds) return tournament;
64         return {
65                 ...tournament,
66                 rounds: tournament.rounds.map(round =>
67                         round.id === result.round_id
68                                 ? Round.patchResult(round, result)
69                                 : round
70                 ),
71         };
72 };
73
74 export const patchRound = (tournament, round) => {
75         if (!tournament) return tournament;
76         return {
77                 ...tournament,
78                 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
79         };
80 };
81
82 export const patchUser = (tournament, user) => {
83         if (!tournament || !tournament.participants || !user) return tournament;
84         if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
85         return {
86                 ...tournament,
87                 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
88         };
89 };
90
91 export const sortParticipants = tournament => {
92         if (!tournament || !tournament.participants || !tournament.participants.length) {
93                 return tournament;
94         }
95         return {
96                 ...tournament,
97                 participants: tournament.participants.sort(Participant.compareUsername),
98         };
99 };
100
101 export default {
102         compareScore,
103         findParticipant,
104         getRunners,
105         getTournamentAdmins,
106         patchResult,
107         patchRound,
108         patchUser,
109         sortParticipants,
110 };