feat: support double-elimination series games

This commit is contained in:
dahyeon committed 2026-07-07 17:34:59 -07:00
1 parent 2ee8c3807b
commit e188828423
16 files changed
+1176 -234

No files matched your search

@@ -3,6 +3,7 @@ import type { MatchDocument } from '../global/database/schema';
import { DistributedLockService } from '../global/redis/distributed-lock.service';
import type { RiotMatchDto } from '../riot/riot.types';
import { TournamentResultService } from './tournament-result.service';
import { buildDoubleEliminationBracket } from '../double-elimination/double-elimination.util';
type TournamentResultServiceInternals = TournamentResultService & {
nextPollDelayMs(elapsedMs: number): number | null;
@@ -14,6 +15,11 @@ type TournamentResultServiceInternals = TournamentResultService & {
buildSeriesResults(
games: NonNullable<MatchDocument['result']>['games'],
): NonNullable<MatchDocument['result']>['seriesResults'];
advanceDoubleEliminationIfNeeded(
match: MatchDocument,
gameResult: NonNullable<MatchDocument['result']>['games'][number],
completedBy: 'riot-result' | 'walkover',
): string | undefined;
};
function createService() {
@@ -38,23 +44,27 @@ function createDistributedLockService() {
options.lockExpirationDurationMilliseconds,
}),
),
runWithRenewedLock: jest.fn().mockImplementation(async (options, operation) =>
operation({
lockKey: options.lockKey,
lockOwnershipToken: 'lock-owner',
lockExpirationDurationMilliseconds:
options.lockExpirationDurationMilliseconds,
}),
),
runWithRenewedLock: jest
.fn()
.mockImplementation(async (options, operation) =>
operation({
lockKey: options.lockKey,
lockOwnershipToken: 'lock-owner',
lockExpirationDurationMilliseconds:
options.lockExpirationDurationMilliseconds,
}),
),
} as unknown as jest.Mocked<DistributedLockService>;
}
interface MatchOverrides {
gameCount?: number;
gameMode?: 'pair' | 'bracket' | 'round-robin';
gameMode?: 'pair' | 'bracket' | 'round-robin' | 'double-elimination';
roundRobinMaxRounds?: number;
participants?: Array<{ excluded?: boolean }>;
teams?: Array<{ teamIndex: number }>;
doubleElimination?: MatchDocument['doubleElimination'];
result?: MatchDocument['result'];
gameSchedule?: Array<{
gameNumber: number;
roundNumber?: number;
@@ -70,6 +80,7 @@ function createMatch(overrides: MatchOverrides): MatchDocument {
participants: [],
teams: [],
gameSchedule: [],
result: undefined,
...overrides,
} as unknown as MatchDocument;
}
@@ -177,6 +188,96 @@ describe('TournamentResultService round-robin scheduling', () => {
},
]);
});
it('maps double-elimination series game numbers to bracket matches', () => {
const service = createService();
const match = createMatch({
gameMode: 'double-elimination',
doubleElimination: buildDoubleEliminationBracket([1, 2, 3, 4], {
resetFinalEnabled: true,
}),
});
expect(service.resolveRequiredGameSlots(match)).toBe(12);
expect(service.resolveSeriesRefForGameNumber(match, 1)).toEqual({
teamAIndex: 1,
teamBIndex: 4,
});
expect(service.resolveSeriesRefForGameNumber(match, 4)).toEqual({
teamAIndex: 2,
teamBIndex: 3,
});
});
it('advances double-elimination only after a team reaches required series wins', () => {
const service = createService();
const game = {
gameNumber: 1,
bracketMatchId: 'WB_R1_M1',
riotMatchId: 'KR_1',
blueTeamIndex: 1,
redTeamIndex: 4,
winnerTeamIndex: 1,
winnerSide: 'blue',
gameCreation: 1,
gameDuration: 1800,
participants: [],
} as NonNullable<MatchDocument['result']>['games'][number];
const match = createMatch({
gameMode: 'double-elimination',
doubleElimination: buildDoubleEliminationBracket([1, 2, 3, 4], {
resetFinalEnabled: true,
}),
result: {
riotMatchIds: ['KR_1'],
games: [game],
seriesResults: [
{
bracketMatchId: 'WB_R1_M1',
teamAIndex: 1,
teamBIndex: 4,
teamAWins: 1,
teamBWins: 0,
winnerTeamIndex: 1,
},
],
},
} as MatchOverrides);
expect(
service.advanceDoubleEliminationIfNeeded(match, game, 'riot-result'),
).toBeUndefined();
const firstGameBracketMatch = match.doubleElimination?.bracketMatches.find(
(entry) => entry.bracketMatchId === 'WB_R1_M1',
);
expect(firstGameBracketMatch).toMatchObject({ state: 'ready' });
expect(firstGameBracketMatch?.winnerTeamIndex).toBeUndefined();
match.result!.seriesResults = [
{
bracketMatchId: 'WB_R1_M1',
teamAIndex: 1,
teamBIndex: 4,
teamAWins: 2,
teamBWins: 0,
winnerTeamIndex: 1,
},
];
expect(
service.advanceDoubleEliminationIfNeeded(match, game, 'riot-result'),
).toBe('WB_R1_M1');
expect(
match.doubleElimination?.bracketMatches.find(
(entry) => entry.bracketMatchId === 'WB_R1_M1',
),
).toMatchObject({ state: 'completed', winnerTeamIndex: 1 });
expect(
match.doubleElimination?.bracketMatches.find(
(entry) => entry.bracketMatchId === 'WB_FINAL',
),
).toMatchObject({ teamAIndex: 1 });
});
});
describe('TournamentResultService tournament code polling', () => {