feat: add limited round-robin scheduling

This commit is contained in:
dahyeon committed 2026-06-19 01:13:50 -07:00
1 parent 24983aeb09
commit 658592973e
15 files changed
+403 -63

No files matched your search

@@ -1,5 +1,6 @@
import {
resolveRoundRobinTeamCount,
roundRobinGameSchedule,
roundRobinPairCount,
roundRobinPairs,
shuffleInPlace,
@@ -25,6 +26,30 @@ describe('round-robin util', () => {
expect(roundRobinPairCount(6)).toBe(15);
});
it('builds round-robin games grouped into simultaneously playable rounds', () => {
const schedule = roundRobinGameSchedule(6, { gamesPerPair: 1 });
expect(schedule).toHaveLength(15);
for (const roundNumber of [1, 2, 3, 4, 5]) {
const round = schedule.filter((game) => game.roundNumber === roundNumber);
expect(round).toHaveLength(3);
expect(new Set(round.flatMap((game) => [game.teamAIndex, game.teamBIndex])))
.toHaveProperty('size', 6);
}
});
it('limits round-robin games by max rounds', () => {
const schedule = roundRobinGameSchedule(8, {
gamesPerPair: 1,
maxRounds: 3,
});
expect(schedule).toHaveLength(12);
expect(new Set(schedule.map((game) => game.roundNumber))).toEqual(
new Set([1, 2, 3]),
);
});
it('resolves team count from teams first, then active participants', () => {
expect(
resolveRoundRobinTeamCount({
@@ -3,11 +3,23 @@ export interface RoundRobinPair {
teamBIndex: number;
}
export interface RoundRobinGameAssignment extends RoundRobinPair {
gameNumber: number;
roundNumber: number;
}
export interface RoundRobinTeamCountInput {
teams?: Array<{ teamIndex: number }>;
participants: Array<{ excluded?: boolean }>;
}
export interface RoundRobinGameScheduleOptions {
gamesPerPair?: number;
maxRounds?: number;
rng?: () => number;
shuffleTeams?: boolean;
}
export function roundRobinPairs(teamCount: number): RoundRobinPair[] {
const pairs: RoundRobinPair[] = [];
for (let teamAIndex = 1; teamAIndex <= teamCount; teamAIndex += 1) {
@@ -27,6 +39,70 @@ export function roundRobinPairCount(teamCount: number): number {
return (teamCount * (teamCount - 1)) / 2;
}
export function roundRobinRoundCount(teamCount: number): number {
if (teamCount < 2) return 0;
return teamCount % 2 === 0 ? teamCount - 1 : teamCount;
}
export function roundRobinGameSchedule(
teamCount: number,
options: RoundRobinGameScheduleOptions = {},
): RoundRobinGameAssignment[] {
if (teamCount < 2) return [];
const rawGamesPerPair = options.gamesPerPair ?? 1;
const gamesPerPair = Number.isFinite(rawGamesPerPair)
? Math.max(1, Math.floor(rawGamesPerPair))
: 1;
const fullRoundCount = roundRobinRoundCount(teamCount);
const requestedRoundCount =
typeof options.maxRounds === 'number' && Number.isFinite(options.maxRounds)
? Math.floor(options.maxRounds)
: fullRoundCount;
const roundLimit = Math.max(
0,
Math.min(fullRoundCount, requestedRoundCount),
);
if (roundLimit === 0) return [];
const bye = 0;
const teams = Array.from({ length: teamCount }, (_, index) => index + 1);
if (options.shuffleTeams) shuffleInPlace(teams, options.rng);
if (teams.length % 2 === 1) teams.push(bye);
const rotating = [...teams];
const schedule: RoundRobinGameAssignment[] = [];
let gameNumber = 1;
for (let roundIndex = 0; roundIndex < roundLimit; roundIndex += 1) {
const roundNumber = roundIndex + 1;
for (let index = 0; index < rotating.length / 2; index += 1) {
const left = rotating[index];
const right = rotating[rotating.length - 1 - index];
if (left === bye || right === bye) continue;
const teamAIndex = Math.min(left, right);
const teamBIndex = Math.max(left, right);
for (let gameIndex = 0; gameIndex < gamesPerPair; gameIndex += 1) {
schedule.push({
gameNumber,
roundNumber,
teamAIndex,
teamBIndex,
});
gameNumber += 1;
}
}
const fixed = rotating[0];
const tail = rotating.slice(1);
tail.unshift(tail.pop() as number);
rotating.splice(0, rotating.length, fixed, ...tail);
}
return schedule;
}
export function resolveRoundRobinTeamCount(
match: RoundRobinTeamCountInput,
): number {
@@ -19,10 +19,12 @@ function createService() {
interface MatchOverrides {
gameCount?: number;
gameMode?: 'pair' | 'bracket' | 'round-robin';
roundRobinMaxRounds?: number;
participants?: Array<{ excluded?: boolean }>;
teams?: Array<{ teamIndex: number }>;
gameSchedule?: Array<{
gameNumber: number;
roundNumber?: number;
teamAIndex: number;
teamBIndex: number;
}>;
@@ -102,6 +104,23 @@ describe('TournamentCodeService round-robin scheduling', () => {
).toBe(15);
});
it('calculates limited round-robin slots from max rounds', () => {
const service = createService();
expect(
service.resolveRequiredSlots(
createMatch({
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: 3,
teams: Array.from({ length: 8 }, (_, index) => ({
teamIndex: index + 1,
})),
}),
),
).toBe(12);
});
it('uses persisted round-robin game schedule for game refs', () => {
const service = createService();
const match = createMatch({
@@ -124,7 +143,7 @@ describe('TournamentCodeService round-robin scheduling', () => {
});
});
it('falls back to lexicographic round-robin pairs before schedule is persisted', () => {
it('falls back to round-based pairs before schedule is persisted', () => {
const service = createService();
const match = createMatch({
gameMode: 'round-robin',
@@ -137,10 +156,10 @@ describe('TournamentCodeService round-robin scheduling', () => {
expect(service.resolveSeriesRefForGameNumber(match, 1)).toEqual({
teamAIndex: 1,
teamBIndex: 2,
teamBIndex: 4,
});
expect(service.resolveSeriesRefForGameNumber(match, 2)).toEqual({
teamAIndex: 1,
teamAIndex: 2,
teamBIndex: 3,
});
});
@@ -173,6 +192,31 @@ describe('TournamentCodeService round-robin scheduling', () => {
expect(pair.gameSchedule).toEqual([]);
});
it('creates a limited round-robin schedule grouped by playable rounds', () => {
const service = createService();
const roundRobin = createMatch({
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: 3,
teams: Array.from({ length: 8 }, (_, index) => ({
teamIndex: index + 1,
})),
gameSchedule: [],
});
service.ensureGameSchedule(roundRobin);
expect(roundRobin.gameSchedule).toHaveLength(12);
for (const roundNumber of [1, 2, 3]) {
const round = roundRobin.gameSchedule.filter(
(game) => game.roundNumber === roundNumber,
);
expect(round).toHaveLength(4);
expect(new Set(round.flatMap((game) => [game.teamAIndex, game.teamBIndex])).size)
.toBe(8);
}
});
it('does not expose fallback round-robin slots before the schedule is persisted', async () => {
const service = createService();
const slots = await service.buildSlots(
@@ -25,10 +25,8 @@ import type {
TournamentCodeUploadItem,
} from './tournament-code.types';
import {
roundRobinGameSchedule,
resolveRoundRobinTeamCount,
roundRobinPairCount,
roundRobinPairs,
shuffleInPlace,
} from './round-robin.util';
const DEFAULT_REGION = 'kr';
@@ -368,6 +366,7 @@ export class TournamentCodeService {
const seriesRef = this.resolveSeriesRefForGameNumber(match, gameNumber);
return {
gameNumber,
roundNumber: this.resolveRoundNumberForGameNumber(match, gameNumber),
...seriesRef,
code: null,
expiresAt: null,
@@ -377,6 +376,7 @@ export class TournamentCodeService {
}
return {
gameNumber,
roundNumber: this.resolveRoundNumberForGameNumber(match, gameNumber),
...this.resolveSeriesRefForGameNumber(match, gameNumber),
code: code.code,
expiresAt: code.expiresAt.toISOString(),
@@ -395,8 +395,10 @@ export class TournamentCodeService {
const gameMode = match.gameMode ?? 'pair';
if (gameMode === 'round-robin') {
const teamCount = resolveRoundRobinTeamCount(match);
return roundRobinPairCount(teamCount) * gameCount;
if ((match.gameSchedule?.length ?? 0) > 0) {
return match.gameSchedule.length;
}
return this.buildRoundRobinGameSchedule(match).length;
}
const teamCount = (match.teams ?? []).length;
@@ -426,13 +428,18 @@ export class TournamentCodeService {
};
}
const teamCount = resolveRoundRobinTeamCount(match);
const perSeries = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
const pairIndex = Math.max(0, Math.ceil(gameNumber / perSeries) - 1);
return roundRobinPairs(teamCount)[pairIndex] ?? {
teamAIndex: 1,
teamBIndex: 2,
};
const fallback = this.buildRoundRobinGameSchedule(match).find(
(entry) => entry.gameNumber === gameNumber,
);
return fallback
? {
teamAIndex: fallback.teamAIndex,
teamBIndex: fallback.teamBIndex,
}
: {
teamAIndex: 1,
teamBIndex: 2,
};
}
const perSeries = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
@@ -450,21 +457,33 @@ export class TournamentCodeService {
const teamCount = resolveRoundRobinTeamCount(match);
if (teamCount < 2) return;
const gameCount = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
const pairs = shuffleInPlace([...roundRobinPairs(teamCount)]);
const schedule: MatchGameAssignment[] = [];
match.gameSchedule = this.buildRoundRobinGameSchedule(match, true);
}
for (let pairIndex = 0; pairIndex < pairs.length; pairIndex += 1) {
const pair = pairs[pairIndex];
for (let gameIndex = 0; gameIndex < gameCount; gameIndex += 1) {
schedule.push({
gameNumber: pairIndex * gameCount + gameIndex + 1,
teamAIndex: pair.teamAIndex,
teamBIndex: pair.teamBIndex,
});
}
}
private resolveRoundNumberForGameNumber(
match: MatchDocument,
gameNumber: number,
): number | undefined {
if ((match.gameMode ?? 'pair') !== 'round-robin') return undefined;
match.gameSchedule = schedule;
const persisted = match.gameSchedule?.find(
(entry) => entry.gameNumber === gameNumber,
);
if (persisted?.roundNumber) return persisted.roundNumber;
return this.buildRoundRobinGameSchedule(match).find(
(entry) => entry.gameNumber === gameNumber,
)?.roundNumber;
}
private buildRoundRobinGameSchedule(
match: MatchDocument,
shuffleTeams = false,
): MatchGameAssignment[] {
return roundRobinGameSchedule(resolveRoundRobinTeamCount(match), {
gamesPerPair: match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT,
maxRounds: match.roundRobinMaxRounds,
shuffleTeams,
});
}
}
@@ -24,6 +24,7 @@ export interface BulkUploadTournamentCodesResponse {
export interface TournamentCodeSlotView {
gameNumber: number;
roundNumber?: number;
teamAIndex: number;
teamBIndex: number;
code: string | null;