]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
allow admins to lock/unlock rounds
[alttp.git] / resources / js / helpers / Tournament.js
1 import Participant from './Participant';
2 import Round from './Round';
3
4 export const calculateScores = tournament => {
5         const scores = getRunners(tournament).map(participant => ({ participant, score: 0 }));
6         if (!tournament.rounds || !tournament.rounds.length) return scores;
7         tournament.rounds.forEach(round => {
8                 const filtered = Participant
9                         .sortByResult(tournament.participants, round)
10                         .map(p => ({ participant: p, result: Participant.findResult(p, round) }))
11                         .filter(r => r.result && (r.result.time || r.result.forfeit))
12                         .reverse();
13                 let running = 0;
14                 let bonus = 1;
15                 let lastResult = null;
16                 for (let i = 0; i < filtered.length; ++i) {
17                         const score = scores.find(s => s.participant.id === filtered[i].participant.id);
18                         if (!score) return;
19                         const result = filtered[i].result;
20                         const betterThanLast = lastResult === null || result.time < lastResult;
21                         if (!result.forfeit && betterThanLast) {
22                                 running += bonus;
23                                 lastResult = result.time;
24                                 bonus = 1;
25                         } else {
26                                 ++bonus;
27                         }
28                         if (!result.forfeit) {
29                                 score.score += running;
30                         }
31                 }
32         });
33         return scores.sort(compareScore).reverse();
34 };
35
36 export const compareScore = (a, b) => {
37         const a_score = a && a.score ? a.score : 0;
38         const b_score = b && b.score ? b.score : 0;
39         if (a_score < b_score) return -1;
40         if (b_score < a_score) return 1;
41         return Participant.compareUsername(a.participant, b.participant) * -1;
42 };
43
44 export const findParticipant = (tournament, user) => {
45         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
46         if (!user || !user.id) return null;
47         return tournament.participants.find(p => p.user_id == user.id);
48 };
49
50 export const getRunners = tournament => {
51         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
52         return tournament.participants
53                 .filter(Participant.isRunner)
54                 .sort(Participant.compareUsername);
55 };
56
57 export const getTournamentAdmins = tournament => {
58         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
59         return tournament.participants
60                 .filter(Participant.isTournamentAdmin)
61                 .sort(Participant.compareUsername);
62 };
63
64 export const hasRunners = tournament => {
65         return getRunners(tournament).length > 0;
66 };
67
68 export const hasTournamentAdmins = tournament => {
69         return getTournamentAdmins(tournament).length > 0;
70 };
71
72 export const patchResult = (tournament, result) => {
73         if (!tournament || !tournament.rounds) return tournament;
74         return {
75                 ...tournament,
76                 rounds: tournament.rounds.map(round =>
77                         round.id === result.round_id
78                                 ? Round.patchResult(round, result)
79                                 : round
80                 ),
81         };
82 };
83
84 export const patchRound = (tournament, round) => {
85         if (!tournament) return tournament;
86         return {
87                 ...tournament,
88                 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
89         };
90 };
91
92 export const patchUser = (tournament, user) => {
93         if (!tournament || !tournament.participants || !user) return tournament;
94         if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
95         return {
96                 ...tournament,
97                 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
98         };
99 };
100
101 export const sortParticipants = tournament => {
102         if (!tournament || !tournament.participants || !tournament.participants.length) {
103                 return tournament;
104         }
105         return {
106                 ...tournament,
107                 participants: tournament.participants.sort(Participant.compareUsername),
108         };
109 };
110
111 export default {
112         calculateScores,
113         compareScore,
114         findParticipant,
115         getRunners,
116         getTournamentAdmins,
117         patchResult,
118         patchRound,
119         patchUser,
120         sortParticipants,
121 };