feat: support double-elimination series games
This commit is contained in:
1 parent
2ee8c3807b
commit
e188828423
16 files changed
+1176
-234
No files matched your search
@@ -8,13 +8,13 @@ import {
|
||||
import { randomUUID } from 'crypto';
|
||||
import mongoose from 'mongoose';
|
||||
import {
|
||||
buildDoubleEliminationGameSlots,
|
||||
buildDoubleEliminationBracket,
|
||||
findNextReadyDoubleEliminationBracketMatches,
|
||||
type DoubleEliminationGameSlot,
|
||||
} from '../double-elimination/double-elimination.util';
|
||||
import { AuctionRedisService } from '../auction/redis/auction-redis.service';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import type {
|
||||
DoubleEliminationBracketMatch,
|
||||
MatchGameAssignment,
|
||||
MatchDocument,
|
||||
TournamentCode,
|
||||
@@ -358,9 +358,9 @@ export class TournamentCodeService {
|
||||
match.gameCount = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
|
||||
match.tournamentCodeIds = match.tournamentCodeIds ?? [];
|
||||
this.ensureGameSchedule(match);
|
||||
const doubleEliminationBracketMatch =
|
||||
const doubleEliminationSlot =
|
||||
(match.gameMode ?? 'pair') === 'double-elimination'
|
||||
? this.resolveDoubleEliminationBracketMatchByGameNumber(
|
||||
? this.resolveDoubleEliminationGameSlotByGameNumber(
|
||||
match,
|
||||
params.gameNumber,
|
||||
)
|
||||
@@ -376,11 +376,7 @@ export class TournamentCodeService {
|
||||
}
|
||||
if (
|
||||
(match.result?.games ?? []).some(
|
||||
(game) =>
|
||||
game.gameNumber === params.gameNumber ||
|
||||
(doubleEliminationBracketMatch?.bracketMatchId &&
|
||||
game.bracketMatchId ===
|
||||
doubleEliminationBracketMatch.bracketMatchId),
|
||||
(game) => game.gameNumber === params.gameNumber,
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
@@ -393,8 +389,12 @@ export class TournamentCodeService {
|
||||
TournamentCodeModel.findOne({
|
||||
_id: { $in: match.tournamentCodeIds },
|
||||
matchId: match._id,
|
||||
...(doubleEliminationBracketMatch
|
||||
? { bracketMatchId: doubleEliminationBracketMatch.bracketMatchId }
|
||||
...(doubleEliminationSlot
|
||||
? {
|
||||
gameNumber: params.gameNumber,
|
||||
bracketMatchId:
|
||||
doubleEliminationSlot.bracketMatch.bracketMatchId,
|
||||
}
|
||||
: { gameNumber: params.gameNumber }),
|
||||
}),
|
||||
session,
|
||||
@@ -444,9 +444,10 @@ export class TournamentCodeService {
|
||||
state: 'reserved',
|
||||
matchId: match._id,
|
||||
gameNumber: params.gameNumber,
|
||||
...(doubleEliminationBracketMatch
|
||||
...(doubleEliminationSlot
|
||||
? {
|
||||
bracketMatchId: doubleEliminationBracketMatch.bracketMatchId,
|
||||
bracketMatchId:
|
||||
doubleEliminationSlot.bracketMatch.bracketMatchId,
|
||||
}
|
||||
: {}),
|
||||
reservedAt: new Date(),
|
||||
@@ -470,8 +471,8 @@ export class TournamentCodeService {
|
||||
|
||||
return {
|
||||
gameNumber: params.gameNumber,
|
||||
...(doubleEliminationBracketMatch
|
||||
? this.toDoubleEliminationSlotBase(doubleEliminationBracketMatch)
|
||||
...(doubleEliminationSlot
|
||||
? this.toDoubleEliminationSlotBase(doubleEliminationSlot)
|
||||
: {}),
|
||||
roundNumber: this.resolveRoundNumberForGameNumber(
|
||||
match,
|
||||
@@ -735,9 +736,9 @@ export class TournamentCodeService {
|
||||
};
|
||||
return Boolean(
|
||||
candidate?.hasErrorLabel?.('TransientTransactionError') ||
|
||||
candidate?.hasErrorLabel?.('UnknownTransactionCommitResult') ||
|
||||
candidate?.code === 112 ||
|
||||
candidate?.codeName === 'WriteConflict',
|
||||
candidate?.hasErrorLabel?.('UnknownTransactionCommitResult') ||
|
||||
candidate?.code === 112 ||
|
||||
candidate?.codeName === 'WriteConflict',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -768,9 +769,9 @@ export class TournamentCodeService {
|
||||
TournamentCodeModel: mongoose.Model<TournamentCode>,
|
||||
session: mongoose.ClientSession,
|
||||
): Promise<MatchDocument> {
|
||||
const readyBracketMatches = findNextReadyDoubleEliminationBracketMatches(
|
||||
match.doubleElimination,
|
||||
);
|
||||
const readySlots = buildDoubleEliminationGameSlots(
|
||||
match.doubleElimination?.bracketMatches ?? [],
|
||||
).filter((slot) => slot.bracketMatch.state === 'ready');
|
||||
const existingCodes =
|
||||
match.tournamentCodeIds.length > 0
|
||||
? await this.execQuery<TournamentCodeDocument[]>(
|
||||
@@ -782,22 +783,22 @@ export class TournamentCodeService {
|
||||
),
|
||||
)
|
||||
: [];
|
||||
const byBracketMatchId = new Map(
|
||||
const byGameNumber = new Map(
|
||||
existingCodes
|
||||
.filter(
|
||||
(code) =>
|
||||
code.bracketMatchId &&
|
||||
typeof code.gameNumber === 'number' &&
|
||||
(code.state === 'reserved' || code.state === 'used'),
|
||||
)
|
||||
.map((code) => [code.bracketMatchId as string, code]),
|
||||
.map((code) => [code.gameNumber as number, code]),
|
||||
);
|
||||
const expiresAfter = new Date(
|
||||
Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS,
|
||||
);
|
||||
const reservedIds: mongoose.Types.ObjectId[] = [];
|
||||
|
||||
for (const bracketMatch of readyBracketMatches) {
|
||||
if (byBracketMatchId.has(bracketMatch.bracketMatchId)) continue;
|
||||
for (const slot of readySlots) {
|
||||
if (byGameNumber.has(slot.gameNumber)) continue;
|
||||
const code = await TournamentCodeModel.findOneAndUpdate(
|
||||
{
|
||||
state: 'available',
|
||||
@@ -810,8 +811,8 @@ export class TournamentCodeService {
|
||||
$set: {
|
||||
state: 'reserved',
|
||||
matchId: match._id,
|
||||
gameNumber: bracketMatch.gameNumber,
|
||||
bracketMatchId: bracketMatch.bracketMatchId,
|
||||
gameNumber: slot.gameNumber,
|
||||
bracketMatchId: slot.bracketMatch.bracketMatchId,
|
||||
reservedAt: new Date(),
|
||||
},
|
||||
$unset: { releasedAt: 1 },
|
||||
@@ -820,7 +821,7 @@ export class TournamentCodeService {
|
||||
).exec();
|
||||
if (!code) break;
|
||||
reservedIds.push(code._id);
|
||||
byBracketMatchId.set(bracketMatch.bracketMatchId, code);
|
||||
byGameNumber.set(slot.gameNumber, code);
|
||||
}
|
||||
|
||||
if (reservedIds.length > 0) {
|
||||
@@ -841,6 +842,7 @@ export class TournamentCodeService {
|
||||
this.ensureGameSchedule(match);
|
||||
const bracketMatches = match.doubleElimination?.bracketMatches ?? [];
|
||||
if (bracketMatches.length === 0) return [];
|
||||
const slots = buildDoubleEliminationGameSlots(bracketMatches);
|
||||
const TournamentCodeModel =
|
||||
await this.connectionService.getModel('tournamentCode');
|
||||
const ids = match.tournamentCodeIds ?? [];
|
||||
@@ -848,21 +850,21 @@ export class TournamentCodeService {
|
||||
ids.length > 0
|
||||
? await TournamentCodeModel.find({ _id: { $in: ids } }).exec()
|
||||
: [];
|
||||
const byBracketMatchId = new Map(
|
||||
const byGameNumber = new Map(
|
||||
codes
|
||||
.filter(
|
||||
(code) =>
|
||||
code.bracketMatchId &&
|
||||
typeof code.gameNumber === 'number' &&
|
||||
code.state !== 'released' &&
|
||||
code.state !== 'voided',
|
||||
)
|
||||
.map((code) => [code.bracketMatchId as string, code]),
|
||||
.map((code) => [code.gameNumber as number, code]),
|
||||
);
|
||||
|
||||
return bracketMatches.map((bracketMatch) => {
|
||||
const code = byBracketMatchId.get(bracketMatch.bracketMatchId);
|
||||
return slots.map((slot) => {
|
||||
const code = byGameNumber.get(slot.gameNumber);
|
||||
return {
|
||||
...this.toDoubleEliminationSlotBase(bracketMatch),
|
||||
...this.toDoubleEliminationSlotBase(slot),
|
||||
code: code?.code ?? null,
|
||||
expiresAt: code?.expiresAt.toISOString() ?? null,
|
||||
state: code?.state ?? 'pending',
|
||||
@@ -872,10 +874,17 @@ export class TournamentCodeService {
|
||||
}
|
||||
|
||||
private toDoubleEliminationSlotBase(
|
||||
bracketMatch: DoubleEliminationBracketMatch,
|
||||
): Omit<TournamentCodeSlotView, 'code' | 'expiresAt' | 'state' | 'riotMatchId'> {
|
||||
slot: DoubleEliminationGameSlot,
|
||||
): Omit<
|
||||
TournamentCodeSlotView,
|
||||
'code' | 'expiresAt' | 'state' | 'riotMatchId'
|
||||
> {
|
||||
const bracketMatch = slot.bracketMatch;
|
||||
return {
|
||||
gameNumber: bracketMatch.gameNumber,
|
||||
gameNumber: slot.gameNumber,
|
||||
seriesGameNumber: slot.seriesGameNumber,
|
||||
seriesGameCount: slot.seriesGameCount,
|
||||
seriesRequiredWins: slot.seriesRequiredWins,
|
||||
bracketMatchId: bracketMatch.bracketMatchId,
|
||||
bracketSide: bracketMatch.bracketSide,
|
||||
roundNumber: bracketMatch.roundNumber,
|
||||
@@ -890,7 +899,9 @@ export class TournamentCodeService {
|
||||
const gameMode = match.gameMode ?? 'pair';
|
||||
|
||||
if (gameMode === 'double-elimination') {
|
||||
return match.doubleElimination?.bracketMatches.length ?? 0;
|
||||
return buildDoubleEliminationGameSlots(
|
||||
match.doubleElimination?.bracketMatches ?? [],
|
||||
).length;
|
||||
}
|
||||
|
||||
if (gameMode === 'round-robin') {
|
||||
@@ -917,13 +928,13 @@ export class TournamentCodeService {
|
||||
const gameMode = match.gameMode ?? 'pair';
|
||||
|
||||
if (gameMode === 'double-elimination') {
|
||||
const bracketMatch = this.resolveDoubleEliminationBracketMatchByGameNumber(
|
||||
const slot = this.resolveDoubleEliminationGameSlotByGameNumber(
|
||||
match,
|
||||
gameNumber,
|
||||
);
|
||||
return {
|
||||
teamAIndex: bracketMatch?.teamAIndex ?? 1,
|
||||
teamBIndex: bracketMatch?.teamBIndex ?? 2,
|
||||
teamAIndex: slot?.bracketMatch.teamAIndex ?? 1,
|
||||
teamBIndex: slot?.bracketMatch.teamBIndex ?? 2,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -984,20 +995,21 @@ export class TournamentCodeService {
|
||||
match.doubleElimination = buildDoubleEliminationBracket(
|
||||
shuffledTeamIndexes,
|
||||
{
|
||||
resetFinalEnabled:
|
||||
match.doubleElimination?.resetFinalEnabled ?? false,
|
||||
resetFinalEnabled: match.doubleElimination?.resetFinalEnabled ?? false,
|
||||
winnerBracketGameCount:
|
||||
match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT,
|
||||
},
|
||||
);
|
||||
match.markModified?.('doubleElimination');
|
||||
}
|
||||
|
||||
private resolveDoubleEliminationBracketMatchByGameNumber(
|
||||
private resolveDoubleEliminationGameSlotByGameNumber(
|
||||
match: MatchDocument,
|
||||
gameNumber: number,
|
||||
): DoubleEliminationBracketMatch | undefined {
|
||||
return match.doubleElimination?.bracketMatches.find(
|
||||
(bracketMatch) => bracketMatch.gameNumber === gameNumber,
|
||||
);
|
||||
): DoubleEliminationGameSlot | undefined {
|
||||
return buildDoubleEliminationGameSlots(
|
||||
match.doubleElimination?.bracketMatches ?? [],
|
||||
).find((slot) => slot.gameNumber === gameNumber);
|
||||
}
|
||||
|
||||
private shuffleTeamIndexes(teamIndexes: number[]): number[] {
|
||||
|
||||
Reference in new issue
Block a user