]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/Tournament.js
alternative dungeon counting
[alttp.git] / resources / js / helpers / Tournament.js
index 1b808fdf3211f8eede21ee009defb1043db3d4d6..7270351512938a2f3481244d9dca91bf0c8679dd 100644 (file)
+import Application from './Application';
+import Participant from './Participant';
 import Round from './Round';
 
+export const compareScore = (a, b) => {
+       const a_score = a && a.score ? a.score : 0;
+       const b_score = b && b.score ? b.score : 0;
+       if (a_score < b_score) return -1;
+       if (b_score < a_score) return 1;
+       return Participant.compareUsername(a.participant, b.participant) * -1;
+};
+
 export const findParticipant = (tournament, user) => {
        if (!tournament || !tournament.participants || !tournament.participants.length) return null;
        if (!user || !user.id) return null;
        return tournament.participants.find(p => p.user_id == user.id);
 };
 
+export const getPendingApplications = tournament => {
+       if (!tournament || !tournament.applications || !tournament.applications.length) return [];
+       return tournament.applications
+               .filter(Application.isPending)
+               .sort(Application.compareUsername);
+};
+
+export const getRunners = tournament => {
+       if (!tournament || !tournament.participants || !tournament.participants.length) return [];
+       return tournament.participants
+               .filter(Participant.isRunner)
+               .sort(Participant.compareUsername);
+};
+
+export const getLastRound = tournament => {
+       if (!tournament || !tournament.rounds || !tournament.rounds.length) return null;
+       return tournament.rounds.slice(-1)[0];
+};
+
+export const canLoadMoreRounds = tournament => {
+       const last_round = getLastRound(tournament);
+       return last_round && last_round.number > 1;
+};
+
+export const hasScoreboard = tournament => !!(tournament && tournament.type === 'signup-async');
+
+export const hasSignup = tournament => !!(tournament && tournament.type === 'signup-async');
+
+export const getScoreTable = tournament => {
+       if (!tournament || !tournament.rounds || !tournament.rounds.length) return [];
+       const runners = getRunners(tournament);
+       if (!runners.length) return [];
+       const running = {};
+       runners.forEach(participant => {
+               running[participant.id] = 0;
+       });
+       const data = [...tournament.rounds, {}].reverse().map(round => {
+               const entry = { number: round.number ? `#${round.number}` : '' };
+               runners.forEach(participant => {
+                       const result = Participant.findResult(participant, round);
+                       if (result && result.score) {
+                               running[participant.id] += result.score;
+                       }
+                       entry[Participant.getUserName(participant)] = running[participant.id];
+               });
+               return entry;
+       });
+       return data;
+};
+
+export const getTournamentAdmins = tournament => {
+       if (!tournament || !tournament.participants || !tournament.participants.length) return [];
+       return tournament.participants
+               .filter(Participant.isTournamentAdmin)
+               .sort(Participant.compareUsername);
+};
+
+export const getTournamentCrew = tournament => {
+       if (!tournament || !tournament.participants || !tournament.participants.length) return [];
+       return tournament.participants
+               .filter(Participant.isTournamentCrew)
+               .sort(Participant.compareUsername);
+};
+
+export const getTournamentMonitors = tournament => {
+       if (!tournament || !tournament.participants || !tournament.participants.length) return [];
+       return tournament.participants
+               .filter(Participant.isTournamentMonitor)
+               .sort(Participant.compareUsername);
+};
+
+export const hasRunners = tournament => {
+       return getRunners(tournament).length > 0;
+};
+
+export const hasTournamentAdmins = tournament => {
+       return getTournamentAdmins(tournament).length > 0;
+};
+
+export const hasTournamentCrew = tournament => {
+       return getTournamentCrew(tournament).length > 0;
+};
+
+export const hasTournamentMonitors = tournament => {
+       return getTournamentMonitors(tournament).length > 0;
+};
+
+export const patchApplication = (tournament, application) => {
+       if (!tournament) return tournament;
+       if (!tournament.applications || !tournament.applications.length) {
+               return {
+                       ...tournament,
+                       applications: [application],
+               };
+       }
+       if (!tournament.applications.find(a => a.user_id == application.user_id)) {
+               return {
+                       ...tournament,
+                       applications: [...tournament.applications, application],
+               };
+       }
+       return {
+               ...tournament,
+               applications: tournament.applications.map(
+                       a => a.user_id === application.user_id ? application : a,
+               ),
+       };
+};
+
+export const patchParticipant = (tournament, participant) => {
+       if (!tournament) return tournament;
+       if (!tournament.participants || !tournament.participants.length) {
+               return {
+                       ...tournament,
+                       participants: [participant],
+               };
+       }
+       if (!tournament.participants.find(p => p.id === participant.id)) {
+               return {
+                       ...tournament,
+                       participants: [...tournament.participants, participant],
+               };
+       }
+       return {
+               ...tournament,
+               participants: tournament.participants.map(
+                       p => p.id === participant.id ? participant : p,
+               ),
+       };
+};
+
 export const patchResult = (tournament, result) => {
        if (!tournament || !tournament.rounds) return tournament;
        return {
@@ -18,7 +159,58 @@ export const patchResult = (tournament, result) => {
        };
 };
 
+export const patchRound = (tournament, round) => {
+       if (!tournament) return tournament;
+       return {
+               ...tournament,
+               rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
+       };
+};
+
+export const patchUser = (tournament, user) => {
+       if (!tournament || !tournament.participants || !user) return tournament;
+       if (!tournament.participants.find(p => p.user_id == user.id)) return tournament;
+       return {
+               ...tournament,
+               participants: tournament.participants.map(p => Participant.patchUser(p, user)),
+       };
+};
+
+export const removeApplication = (tournament, id) => {
+       if (!tournament || !tournament.applications || !tournament.applications.find(a => a.id == id)) {
+               return tournament;
+       }
+       return {
+               ...tournament,
+               applications: tournament.applications.filter(a => a.id != id),
+       };
+};
+
+export const sortParticipants = tournament => {
+       if (!tournament || !tournament.participants || !tournament.participants.length) {
+               return tournament;
+       }
+       return {
+               ...tournament,
+               participants: tournament.participants.sort(Participant.compareUsername),
+       };
+};
+
 export default {
+       compareScore,
        findParticipant,
+       getRunners,
+       getTournamentAdmins,
+       getTournamentCrew,
+       getTournamentMonitors,
+       hasRunners,
+       hasScoreboard,
+       hasSignup,
+       hasTournamentAdmins,
+       hasTournamentCrew,
+       hasTournamentMonitors,
        patchResult,
+       patchRound,
+       patchUser,
+       sortParticipants,
 };