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