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,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;
}