From 31244b2d7c91bd56063bfafe77e5b838e06800fc Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Thu, 29 Jan 2026 22:08:44 +0200 Subject: [PATCH] Tournament tier calculation fixes Closes #2783 --- .../to.$id.brackets.finalize.server.ts | 4 +- ...TournamentOrganizationRepository.server.ts | 50 ++++++++++++++++++- scripts/backfill-tournament-tiers.ts | 1 + 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/features/tournament-bracket/actions/to.$id.brackets.finalize.server.ts b/app/features/tournament-bracket/actions/to.$id.brackets.finalize.server.ts index 534b32cf7..4045c582b 100644 --- a/app/features/tournament-bracket/actions/to.$id.brackets.finalize.server.ts +++ b/app/features/tournament-bracket/actions/to.$id.brackets.finalize.server.ts @@ -108,7 +108,9 @@ export const action = async ({ request, params }: ActionFunctionArgs) => { finalizeTournament(tournamentId); } - await updateSeriesTierHistory(tournament); + if (!tournament.isTest) { + await updateSeriesTierHistory(tournament); + } if (tournament.ranked) { try { diff --git a/app/features/tournament-organization/TournamentOrganizationRepository.server.ts b/app/features/tournament-organization/TournamentOrganizationRepository.server.ts index a34136e40..c5095fe58 100644 --- a/app/features/tournament-organization/TournamentOrganizationRepository.server.ts +++ b/app/features/tournament-organization/TournamentOrganizationRepository.server.ts @@ -4,6 +4,7 @@ import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite"; import { db } from "~/db/sql"; import type { Tables, TablesInsertable } from "~/db/tables"; import { + TIER_HISTORY_LENGTH, type TournamentTierNumber, updateTierHistory, } from "~/features/tournament/core/tiering"; @@ -422,7 +423,7 @@ export function update({ .execute(); if (series.length > 0) { - await trx + const insertedSeries = await trx .insertInto("TournamentOrganizationSeries") .values( series.map((s) => ({ @@ -433,7 +434,54 @@ export function update({ showLeaderboard: Number(s.showLeaderboard), })), ) + .returning(["id", "substringMatches"]) .execute(); + + const finalizedTournaments = await trx + .selectFrom("Tournament") + .innerJoin( + "CalendarEvent", + "CalendarEvent.tournamentId", + "Tournament.id", + ) + .innerJoin( + "CalendarEventDate", + "CalendarEventDate.eventId", + "CalendarEvent.id", + ) + .select([ + "Tournament.id as tournamentId", + "CalendarEvent.name", + "Tournament.tier", + "CalendarEventDate.startTime", + ]) + .where("Tournament.isFinalized", "=", 1) + .where("CalendarEvent.organizationId", "=", id) + .where("CalendarEvent.hidden", "=", 0) + .orderBy("CalendarEventDate.startTime", "asc") + .execute(); + + for (const s of insertedSeries) { + const matchingTiers = finalizedTournaments + .filter((t) => { + const eventNameLower = t.name.toLowerCase(); + return s.substringMatches.some((match) => + eventNameLower.includes(match.toLowerCase()), + ); + }) + .filter((t) => t.tier !== null) + .map((t) => t.tier); + + if (matchingTiers.length === 0) continue; + + const tierHistory = matchingTiers.slice(-TIER_HISTORY_LENGTH); + + await trx + .updateTable("TournamentOrganizationSeries") + .set({ tierHistory: JSON.stringify(tierHistory) }) + .where("id", "=", s.id) + .execute(); + } } await trx diff --git a/scripts/backfill-tournament-tiers.ts b/scripts/backfill-tournament-tiers.ts index b5e805e8e..7ce3a3c68 100644 --- a/scripts/backfill-tournament-tiers.ts +++ b/scripts/backfill-tournament-tiers.ts @@ -99,6 +99,7 @@ function getTournamentsWithOrg(): TournamentWithOrg[] { INNER JOIN CalendarEvent ce ON ce.tournamentId = t.id INNER JOIN CalendarEventDate ced ON ced.eventId = ce.id WHERE t.isFinalized = 1 + AND ce.hidden = 0 ORDER BY ced.startTime ASC `; return sql.prepare(query).all() as TournamentWithOrg[];