feat(api): add tournament code helpers

This commit is contained in:
dahyeon committed 2026-04-20 22:42:00 +09:00
1 parent 422472a641
commit 6bad4861e6
4 files changed
+140

No files matched your search

@@ -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]);
});
});
@@ -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<T>(
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;
}
@@ -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;
@@ -0,0 +1,33 @@
import type {
TournamentCodeState,
TournamentSpectatorType,
} from '../global/database/schema';
export interface TournamentCodeUploadItem {
code: string;
expiresAt: string;
spectatorType?: TournamentSpectatorType;
metadata?: Record<string, unknown>;
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;
}