]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
switch participant list to scoreboard
[alttp.git] / resources / js / helpers / Tournament.js
1 import Participant from './Participant';
2 import Round from './Round';
3
4 export const calculateScores = tournament => {
5         if (!tournament || !tournament.participants || !tournament.participants.length) return [];
6         const scores = tournament.participants.map(participant => ({ participant, score: 0 }));
7         if (!tournament.rounds || !tournament.rounds.length) return scores;
8         tournament.rounds.forEach(round => {
9                 const filtered = Participant
10                         .sortByResult(tournament.participants, round)
11                         .map(p => ({ participant: p, result: Participant.findResult(p, round) }))
12                         .filter(r => r.result && (r.result.time || r.result.forfeit))
13                         .reverse();
14                 let running = 0;
15                 let bonus = 1;
16                 let lastResult = null;
17                 for (let i = 0; i < filtered.length; ++i) {
18                         const score = scores.find(s => s.participant.id === filtered[i].participant.id);
19                         if (!score) return;
20                         const result = filtered[i].result;
21                         const betterThanLast = lastResult === null || result.time < lastResult;
22                         if (!result.forfeit && betterThanLast) {
23                                 running += bonus;
24                                 lastResult = result.time;
25                                 bonus = 1;
26                         } else {
27                                 ++bonus;
28                         }
29                         if (!result.forfeit) {
30                                 score.score += running;
31                         }
32                 }
33         });
34         return scores.sort(compareScore).reverse();
35 };
36
37 export const compareScore = (a, b) => {
38         const a_score = a && a.score ? a.score : 0;
39         const b_score = b && b.score ? b.score : 0;
40         if (a_score < b_score) return -1;
41         if (b_score < a_score) return 1;
42         return 0;
43 };
44
45 export const findParticipant = (tournament, user) => {
46         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
47         if (!user || !user.id) return null;
48         return tournament.participants.find(p => p.user_id == user.id);
49 };
50
51 export const patchResult = (tournament, result) => {
52         if (!tournament || !tournament.rounds) return tournament;
53         return {
54                 ...tournament,
55                 rounds: tournament.rounds.map(round =>
56                         round.id === result.round_id
57                                 ? Round.patchResult(round, result)
58                                 : round
59                 ),
60         };
61 };
62
63 export const patchRound = (tournament, round) => {
64         if (!tournament) return tournament;
65         return {
66                 ...tournament,
67                 rounds: tournament.rounds.map(r => r.id === round.id ? round : r),
68         };
69 };
70
71 export const sortParticipants = tournament => {
72         if (!tournament || !tournament.participants || !tournament.participants.length) {
73                 return tournament;
74         }
75         return {
76                 ...tournament,
77                 participants: tournament.participants.sort(Participant.compareUsername),
78         };
79 };
80
81 export default {
82         calculateScores,
83         compareScore,
84         findParticipant,
85         patchResult,
86         sortParticipants,
87 };