]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
fix big key size
[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         const sorted = scores.sort(compareScore);
36         let placement = scores.length;
37         let skipped = 0;
38         let lastScore = sorted[0].score;
39         for (let i = 0; i < sorted.length; ++i) {
40                 if (sorted[i].score > lastScore) {
41                         placement -= skipped;
42                         skipped = 1;
43                         lastScore = sorted[i].score;
44                 } else {
45                         ++skipped;
46                 }
47                 sorted[i].placement = placement;
48         }
49         return sorted.reverse();
50 };
51
52 export const compareScore = (a, b) => {
53         const a_score = a && a.score ? a.score : 0;
54         const b_score = b && b.score ? b.score : 0;
55         if (a_score < b_score) return -1;
56         if (b_score < a_score) return 1;
57         return Participant.compareUsername(a.participant, b.participant) * -1;
58 };
59
60 export const findParticipant = (tournament, user) => {
61         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
62         if (!user || !user.id) return null;
63         return tournament.participants.find(p => p.user_id == user.id);
64 };
65
66 export const getRunners = tournament => {
67         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
68         return tournament.participants
69                 .filter(Participant.isRunner)
70                 .sort(Participant.compareUsername);
71 };
72
73 export const getTournamentAdmins = tournament => {
74         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
75         return tournament.participants
76                 .filter(Participant.isTournamentAdmin)
77                 .sort(Participant.compareUsername);
78 };
79
80 export const hasRunners = tournament => {
81         return getRunners(tournament).length > 0;
82 };
83
84 export const hasTournamentAdmins = tournament => {
85         return getTournamentAdmins(tournament).length > 0;
86 };
87
88 export const patchResult = (tournament, result) => {
89         if (!tournament || !tournament.rounds) return tournament;
90         return {
91                 ...tournament,
92                 rounds: tournament.rounds.map(round =>
93                         round.id === result.round_id
94                                 ? Round.patchResult(round, result)
95                                 : round
96                 ),
97         };
98 };
99
100 export const patchRound = (tournament, round) => {
101         if (!tournament) return tournament;
102         return {
103                 ...tournament,
104                 rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
105         };
106 };
107
108 export const patchUser = (tournament, user) => {
109         if (!tournament || !tournament.participants || !user) return tournament;
110         if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
111         return {
112                 ...tournament,
113                 participants: tournament.participants.map(p => Participant.patchUser(p, user)),
114         };
115 };
116
117 export const sortParticipants = tournament => {
118         if (!tournament || !tournament.participants || !tournament.participants.length) {
119                 return tournament;
120         }
121         return {
122                 ...tournament,
123                 participants: tournament.participants.sort(Participant.compareUsername),
124         };
125 };
126
127 export default {
128         calculateScores,
129         compareScore,
130         findParticipant,
131         getRunners,
132         getTournamentAdmins,
133         patchResult,
134         patchRound,
135         patchUser,
136         sortParticipants,
137 };