]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Tournament.js
6b4ca28fb93c2c185ec096ed4579707a93f52ead
[alttp.git] / resources / js / helpers / Tournament.js
1 import Participant from './Participant';
2 import Round from './Round';
3
4 export const findParticipant = (tournament, user) => {
5         if (!tournament || !tournament.participants || !tournament.participants.length) return null;
6         if (!user || !user.id) return null;
7         return tournament.participants.find(p => p.user_id == user.id);
8 };
9
10 export const patchResult = (tournament, result) => {
11         if (!tournament || !tournament.rounds) return tournament;
12         return {
13                 ...tournament,
14                 rounds: tournament.rounds.map(round =>
15                         round.id === result.round_id
16                                 ? Round.patchResult(round, result)
17                                 : round
18                 ),
19         };
20 };
21
22 export const patchRound = (tournament, round) => {
23         if (!tournament) return tournament;
24         return {
25                 ...tournament,
26                 rounds: tournament.rounds.map(r => r.id === round.id ? round : r),
27         };
28 };
29
30 export const sortParticipants = tournament => {
31         if (!tournament || !tournament.participants || !tournament.participants.length) {
32                 return tournament;
33         }
34         return {
35                 ...tournament,
36                 participants: tournament.participants.sort(Participant.compareUsername),
37         };
38 };
39
40 export default {
41         findParticipant,
42         patchResult,
43         sortParticipants,
44 };