From 6bad4861e68404779c571700dda62555ed37d36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8B=A4=ED=98=84=20=28Dahyeon=20Park=29?= Date: Mon, 20 Apr 2026 22:42:00 +0900 Subject: [PATCH] feat(api): add tournament code helpers --- .../tournament-code/round-robin.util.spec.ts | 52 +++++++++++++++++++ .../tournament-code/round-robin.util.ts | 51 ++++++++++++++++++ .../tournament-code.constants.ts | 4 ++ .../tournament-code/tournament-code.types.ts | 33 ++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 apps/api/src/modules/tournament-code/round-robin.util.spec.ts create mode 100644 apps/api/src/modules/tournament-code/round-robin.util.ts create mode 100644 apps/api/src/modules/tournament-code/tournament-code.constants.ts create mode 100644 apps/api/src/modules/tournament-code/tournament-code.types.ts diff --git a/apps/api/src/modules/tournament-code/round-robin.util.spec.ts b/apps/api/src/modules/tournament-code/round-robin.util.spec.ts new file mode 100644 index 00000000..77691089 --- /dev/null +++ b/apps/api/src/modules/tournament-code/round-robin.util.spec.ts @@ -0,0 +1,52 @@ +import { + resolveRoundRobinTeamCount, + roundRobinPairCount, + roundRobinPairs, + shuffleInPlace, +} from './round-robin.util'; + +describe('round-robin util', () => { + it('builds every team pair in lexicographic order', () => { + expect(roundRobinPairs(2)).toEqual([ + { teamAIndex: 1, teamBIndex: 2 }, + ]); + expect(roundRobinPairs(4)).toEqual([ + { teamAIndex: 1, teamBIndex: 2 }, + { teamAIndex: 1, teamBIndex: 3 }, + { teamAIndex: 1, teamBIndex: 4 }, + { teamAIndex: 2, teamBIndex: 3 }, + { teamAIndex: 2, teamBIndex: 4 }, + { teamAIndex: 3, teamBIndex: 4 }, + ]); + }); + + it('counts round-robin pairs', () => { + expect(roundRobinPairCount(1)).toBe(0); + expect(roundRobinPairCount(6)).toBe(15); + }); + + it('resolves team count from teams first, then active participants', () => { + expect( + resolveRoundRobinTeamCount({ + teams: [{ teamIndex: 1 }, { teamIndex: 2 }, { teamIndex: 3 }], + participants: Array.from({ length: 30 }, () => ({})), + }), + ).toBe(3); + expect( + resolveRoundRobinTeamCount({ + teams: [], + participants: [ + ...Array.from({ length: 29 }, () => ({})), + { excluded: true }, + ], + }), + ).toBe(5); + }); + + it('shuffles in place with an injectable rng', () => { + const items = [1, 2, 3, 4]; + const result = shuffleInPlace(items, () => 0); + expect(result).toBe(items); + expect(items).toEqual([2, 3, 4, 1]); + }); +}); diff --git a/apps/api/src/modules/tournament-code/round-robin.util.ts b/apps/api/src/modules/tournament-code/round-robin.util.ts new file mode 100644 index 00000000..4d928f50 --- /dev/null +++ b/apps/api/src/modules/tournament-code/round-robin.util.ts @@ -0,0 +1,51 @@ +export interface RoundRobinPair { + teamAIndex: number; + teamBIndex: number; +} + +export interface RoundRobinTeamCountInput { + teams?: Array<{ teamIndex: number }>; + participants: Array<{ excluded?: boolean }>; +} + +export function roundRobinPairs(teamCount: number): RoundRobinPair[] { + const pairs: RoundRobinPair[] = []; + for (let teamAIndex = 1; teamAIndex <= teamCount; teamAIndex += 1) { + for ( + let teamBIndex = teamAIndex + 1; + teamBIndex <= teamCount; + teamBIndex += 1 + ) { + pairs.push({ teamAIndex, teamBIndex }); + } + } + return pairs; +} + +export function roundRobinPairCount(teamCount: number): number { + if (teamCount < 2) return 0; + return (teamCount * (teamCount - 1)) / 2; +} + +export function resolveRoundRobinTeamCount( + match: RoundRobinTeamCountInput, +): number { + if (match.teams && match.teams.length > 0) { + return match.teams.length; + } + const activeParticipantCount = match.participants.filter( + (participant) => !participant.excluded, + ).length; + return Math.floor(activeParticipantCount / 5); +} + +export function shuffleInPlace( + items: T[], + rng: () => number = Math.random, +): T[] { + for (let index = items.length - 1; index > 0; index -= 1) { + const swapIndex = Math.floor(rng() * (index + 1)); + [items[index], items[swapIndex]] = [items[swapIndex], items[index]]; + } + return items; +} diff --git a/apps/api/src/modules/tournament-code/tournament-code.constants.ts b/apps/api/src/modules/tournament-code/tournament-code.constants.ts new file mode 100644 index 00000000..ee4da598 --- /dev/null +++ b/apps/api/src/modules/tournament-code/tournament-code.constants.ts @@ -0,0 +1,4 @@ +export const TOURNAMENT_CODE_PATTERN = /^[A-Z0-9-]{8,}$/i; +export const TOURNAMENT_CODE_EXPIRY_MARGIN_MS = 24 * 60 * 60 * 1000; +export const TOURNAMENT_CODE_EXPIRING_SOON_MS = 7 * 24 * 60 * 60 * 1000; +export const DEFAULT_TOURNAMENT_GAME_COUNT = 3; diff --git a/apps/api/src/modules/tournament-code/tournament-code.types.ts b/apps/api/src/modules/tournament-code/tournament-code.types.ts new file mode 100644 index 00000000..207f5b0c --- /dev/null +++ b/apps/api/src/modules/tournament-code/tournament-code.types.ts @@ -0,0 +1,33 @@ +import type { + TournamentCodeState, + TournamentSpectatorType, +} from '../global/database/schema'; + +export interface TournamentCodeUploadItem { + code: string; + expiresAt: string; + spectatorType?: TournamentSpectatorType; + metadata?: Record; + source?: string; + notes?: string; +} + +export interface BulkUploadTournamentCodesRequest { + codes: TournamentCodeUploadItem[]; +} + +export interface BulkUploadTournamentCodesResponse { + inserted: number; + duplicates: string[]; + invalid: Array<{ code: string; reason: string }>; +} + +export interface TournamentCodeSlotView { + gameNumber: number; + teamAIndex: number; + teamBIndex: number; + code: string | null; + expiresAt: string | null; + state: TournamentCodeState | 'pending'; + riotMatchId: string | null; +}