From 7f395e989757a0abd75cee230738ee4f983fb4bf Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 9 Dec 2023 15:21:47 +0200 Subject: [PATCH] Allow for concluding tournaments happening between seasons Closes #1578 --- .../core/summarizer.server.ts | 26 ++++++++++++------- .../queries/addSummary.server.ts | 2 +- .../routes/to.$id.brackets.tsx | 15 +++++------ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/features/tournament-bracket/core/summarizer.server.ts b/app/features/tournament-bracket/core/summarizer.server.ts index a58a7df47..dda8f08fc 100644 --- a/app/features/tournament-bracket/core/summarizer.server.ts +++ b/app/features/tournament-bracket/core/summarizer.server.ts @@ -51,6 +51,7 @@ export function tournamentSummary({ queryCurrentTeamRating, queryTeamPlayerRatingAverage, queryCurrentUserRating, + calculateSeasonalStats = true, }: { results: AllMatchResult[]; teams: TeamsArg; @@ -58,19 +59,26 @@ export function tournamentSummary({ queryCurrentTeamRating: (identifier: string) => Rating; queryTeamPlayerRatingAverage: (identifier: string) => Rating; queryCurrentUserRating: (userId: number) => Rating; + calculateSeasonalStats?: boolean; }): TournamentSummary { const userIdsToTeamId = userIdsToTeamIdRecord(teams); return { - skills: skills({ - results, - userIdsToTeamId, - queryCurrentTeamRating, - queryCurrentUserRating, - queryTeamPlayerRatingAverage, - }), - mapResultDeltas: mapResultDeltas({ results, userIdsToTeamId }), - playerResultDeltas: playerResultDeltas({ results, userIdsToTeamId }), + skills: calculateSeasonalStats + ? skills({ + results, + userIdsToTeamId, + queryCurrentTeamRating, + queryCurrentUserRating, + queryTeamPlayerRatingAverage, + }) + : [], + mapResultDeltas: calculateSeasonalStats + ? mapResultDeltas({ results, userIdsToTeamId }) + : [], + playerResultDeltas: calculateSeasonalStats + ? playerResultDeltas({ results, userIdsToTeamId }) + : [], tournamentResults: tournamentResults({ participantCount: teams.length, finalStandings, diff --git a/app/features/tournament-bracket/queries/addSummary.server.ts b/app/features/tournament-bracket/queries/addSummary.server.ts index 7aa526489..9b99ec970 100644 --- a/app/features/tournament-bracket/queries/addSummary.server.ts +++ b/app/features/tournament-bracket/queries/addSummary.server.ts @@ -111,7 +111,7 @@ export const addSummary = sql.transaction( }: { tournamentId: number; summary: TournamentSummary; - season: number; + season?: number; }) => { for (const skill of summary.skills) { const insertedSkill = addSkillStm.get({ diff --git a/app/features/tournament-bracket/routes/to.$id.brackets.tsx b/app/features/tournament-bracket/routes/to.$id.brackets.tsx index f7adb167c..c58c8a35f 100644 --- a/app/features/tournament-bracket/routes/to.$id.brackets.tsx +++ b/app/features/tournament-bracket/routes/to.$id.brackets.tsx @@ -184,11 +184,9 @@ export const action: ActionFunction = async ({ params, request }) => { const results = allMatchResultsByTournamentId(tournamentId); invariant(results.length > 0, "No results found"); - // TODO: support tournaments outside of seasons as well as unranked tournaments - const _currentSeason = currentSeason( + const season = currentSeason( databaseTimestampToDate(tournament.startTime), ); - validate(_currentSeason, "No current season found"); addSummary({ tournamentId, @@ -196,19 +194,18 @@ export const action: ActionFunction = async ({ params, request }) => { teams, finalStandings: _finalStandings, results, + calculateSeasonalStats: typeof season === "number", queryCurrentTeamRating: (identifier) => - queryCurrentTeamRating({ identifier, season: _currentSeason.nth }) - .rating, + queryCurrentTeamRating({ identifier, season: season!.nth }).rating, queryCurrentUserRating: (userId) => - queryCurrentUserRating({ userId, season: _currentSeason.nth }) - .rating, + queryCurrentUserRating({ userId, season: season!.nth }).rating, queryTeamPlayerRatingAverage: (identifier) => queryTeamPlayerRatingAverage({ identifier, - season: _currentSeason.nth, + season: season!.nth, }), }), - season: _currentSeason.nth, + season: season?.nth, }); return null;