]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Round.js
dark mode
[alttp.git] / resources / js / helpers / Round.js
1 import Participant from './Participant';
2 import Tournament from './Tournament';
3
4 export const isComplete = (tournament, round) => {
5         if (!tournament || !tournament.participants) return false;
6         if (!round || !round.results) return false;
7         const runners = Tournament.getRunners(tournament);
8         for (let i = 0; i < runners.length; ++i) {
9                 const result = Participant.findResult(runners[i], round);
10                 if (!result || !result.has_finished) return false;
11         }
12         return true;
13 };
14
15 export const patchResult = (round, result) => {
16         if (!round) return round;
17         if (!round.results || !round.results.length) {
18                 return { ...round, results: [result] };
19         }
20         if (!round.results.find(r => r.id === result.id)) {
21                 return { ...round, results: [...round.results, result] };
22         }
23         return {
24                 ...round,
25                 results: round.results.map(r => r.id === result.id ? result : r),
26         };
27 };
28
29 export default {
30         isComplete,
31         patchResult,
32 };