refactor: remove legacy match result writers

This commit is contained in:
dahyeon committed 2026-07-12 19:47:53 +00:00
1 parent ce07bfa0c8
commit 2958106e42
4 files changed
+48 -319

No files matched your search

+6 -228
View File
@@ -6,8 +6,6 @@ import {
} from '@nestjs/common';
import mongoose from 'mongoose';
import {
cancelDoubleEliminationBracketMatch,
completeDoubleEliminationBracketMatch,
reshuffleDoubleEliminationSeeds,
swapDoubleEliminationSeeds,
} from '../double-elimination/double-elimination.util';
@@ -17,8 +15,6 @@ import { ConnectionService } from '../global/database/connection.service';
import {
Match,
MatchDocument,
MatchGameMode,
MatchSeriesResult,
MatchStatus,
POSITIONS,
Position,
@@ -116,17 +112,6 @@ interface SwapDoubleEliminationSeedsParams extends DoubleEliminationAdminParams
rightSeedNumber: number;
}
interface ApplyDoubleEliminationWalkoverParams extends DoubleEliminationAdminParams {
bracketMatchId: string;
winnerTeamIndex: number;
reason?: string;
}
interface CancelDoubleEliminationActionParams extends DoubleEliminationAdminParams {
bracketMatchId: string;
reason?: string;
}
@Injectable()
export class MatchService {
constructor(
@@ -313,7 +298,7 @@ export class MatchService {
match.doubleElimination = reshuffleDoubleEliminationSeeds(
match.doubleElimination as NonNullable<Match['doubleElimination']>,
);
match.doubleElimination!.events.push({
match.doubleElimination.events.push({
type: 'seeds-reshuffled',
actorUserId: params.actorUserId,
createdAt: new Date(),
@@ -337,7 +322,7 @@ export class MatchService {
rightSeedNumber: params.rightSeedNumber,
},
);
match.doubleElimination!.events.push({
match.doubleElimination.events.push({
type: 'seeds-swapped',
actorUserId: params.actorUserId,
createdAt: new Date(),
@@ -348,130 +333,6 @@ export class MatchService {
return match;
}
public async applyDoubleEliminationWalkover(
params: ApplyDoubleEliminationWalkoverParams,
): Promise<MatchDocument> {
const match = await this.loadDoubleEliminationMatch(params);
const bracketMatch = this.requireDoubleEliminationBracketMatch(
match,
params.bracketMatchId,
);
if (bracketMatch.state !== 'ready') {
throw new BadRequestException('Bracket match is not ready.');
}
if (
bracketMatch.teamAIndex !== params.winnerTeamIndex &&
bracketMatch.teamBIndex !== params.winnerTeamIndex
) {
throw new BadRequestException(
'Winner team is not assigned to this bracket match.',
);
}
await this.releaseReservedDoubleEliminationCodes(match, [
params.bracketMatchId,
]);
match.doubleElimination = completeDoubleEliminationBracketMatch(
match.doubleElimination as NonNullable<Match['doubleElimination']>,
{
bracketMatchId: params.bracketMatchId,
winnerTeamIndex: params.winnerTeamIndex,
completedBy: 'walkover',
completedAt: new Date(),
walkover: {
winnerTeamIndex: params.winnerTeamIndex,
reason: params.reason,
awardedBy: params.actorUserId,
awardedAt: new Date(),
active: true,
},
},
);
match.markModified('doubleElimination');
this.syncDoubleEliminationWinner(match);
await match.save();
await this.tournamentCodeService.reserveForMatch(match);
await this.communityStatsService.invalidateCommunity(params.communityId);
return match;
}
public async cancelDoubleEliminationWalkover(
params: CancelDoubleEliminationActionParams,
): Promise<MatchDocument> {
const match = await this.loadDoubleEliminationMatch(params);
const bracketMatch = this.requireDoubleEliminationBracketMatch(
match,
params.bracketMatchId,
);
if (
bracketMatch.completedBy !== 'walkover' ||
!bracketMatch.walkover?.active
) {
throw new BadRequestException('Active walkover was not found.');
}
await this.releaseReservedDoubleEliminationCodes(match);
match.doubleElimination = cancelDoubleEliminationBracketMatch(
match.doubleElimination as NonNullable<Match['doubleElimination']>,
{
bracketMatchId: params.bracketMatchId,
cancelledBy: params.actorUserId,
cancelReason: params.reason,
},
);
match.markModified('doubleElimination');
this.syncDoubleEliminationWinner(match);
await match.save();
await this.tournamentCodeService.reserveForMatch(match);
await this.communityStatsService.invalidateCommunity(params.communityId);
return match;
}
public async cancelDoubleEliminationRiotResult(
params: CancelDoubleEliminationActionParams,
): Promise<MatchDocument> {
const match = await this.loadDoubleEliminationMatch(params);
const game = match.result?.games?.find(
(entry) => entry.bracketMatchId === params.bracketMatchId,
);
if (!game) throw new NotFoundException('Collected result not found.');
await this.releaseReservedDoubleEliminationCodes(match);
match.doubleElimination = cancelDoubleEliminationBracketMatch(
match.doubleElimination as NonNullable<Match['doubleElimination']>,
{
bracketMatchId: params.bracketMatchId,
cancelledBy: params.actorUserId,
cancelReason: params.reason,
},
);
match.doubleElimination!.events.push({
type: 'riot-result-cancelled',
bracketMatchId: params.bracketMatchId,
actorUserId: params.actorUserId,
reason: params.reason,
createdAt: new Date(),
});
const games = (match.result?.games ?? []).filter(
(entry) => entry.bracketMatchId !== params.bracketMatchId,
);
match.result = {
winnerTeamIndex: undefined,
mvpUserId: match.result?.mvpUserId,
riotMatchIds: (match.result?.riotMatchIds ?? []).filter(
(riotMatchId) => riotMatchId !== game.riotMatchId,
),
games,
seriesResults: this.buildSeriesResults(games),
};
await this.voidDoubleEliminationCode(match, params.bracketMatchId);
if (match.status === 'finished') match.status = 'in-progress';
match.markModified('doubleElimination');
await match.save();
await this.tournamentCodeService.reserveForMatch(match);
await this.matchStatsService.buildAndUpsert(match._id);
await this.communityStatsService.invalidateCommunity(params.communityId);
return match;
}
public async join(params: JoinMatchParams): Promise<MatchDocument> {
const MatchModel = await this.connectionService.getModel('match');
const UserModel = await this.connectionService.getModel('user');
@@ -518,7 +379,7 @@ export class MatchService {
T extends Partial<Match> & DoubleEliminationConfigInput,
>(input: T): Partial<Match> {
const patch = this.stripMatchConfigInput(input);
const gameMode = (patch.gameMode ?? 'pair') as MatchGameMode;
const gameMode = patch.gameMode ?? 'pair';
const maxParticipants = patch.maxParticipants ?? 10;
if (gameMode === 'double-elimination') {
@@ -565,7 +426,8 @@ export class MatchService {
private stripMatchConfigInput<
T extends Partial<Match> & DoubleEliminationConfigInput,
>(input: T): Partial<Match> {
const { doubleEliminationResetFinalEnabled: _ignored, ...patch } = input;
const patch = { ...input };
delete patch.doubleEliminationResetFinalEnabled;
return patch;
}
@@ -623,20 +485,8 @@ export class MatchService {
}
}
private requireDoubleEliminationBracketMatch(
match: MatchDocument,
bracketMatchId: string,
) {
const bracketMatch = match.doubleElimination?.bracketMatches.find(
(entry) => entry.bracketMatchId === bracketMatchId,
);
if (!bracketMatch) throw new NotFoundException('Bracket match not found.');
return bracketMatch;
}
private async releaseReservedDoubleEliminationCodes(
match: MatchDocument,
bracketMatchIds?: string[],
): Promise<void> {
const ids = match.tournamentCodeIds ?? [];
if (ids.length === 0) return;
@@ -646,9 +496,7 @@ export class MatchService {
_id: { $in: ids },
matchId: match._id,
state: 'reserved',
...(bracketMatchIds
? { bracketMatchId: { $in: bracketMatchIds } }
: { bracketMatchId: { $exists: true } }),
bracketMatchId: { $exists: true },
}).exec();
if (codes.length === 0) return;
const releasedIds = codes.map((code) => code._id.toString());
@@ -669,46 +517,6 @@ export class MatchService {
);
}
private async voidDoubleEliminationCode(
match: MatchDocument,
bracketMatchId: string,
): Promise<void> {
const TournamentCodeModel =
await this.connectionService.getModel('tournamentCode');
await TournamentCodeModel.updateOne(
{
_id: { $in: match.tournamentCodeIds ?? [] },
matchId: match._id,
bracketMatchId,
state: 'used',
},
{
$set: { state: 'voided' },
},
).exec();
}
private syncDoubleEliminationWinner(match: MatchDocument): void {
if ((match.gameMode ?? 'pair') !== 'double-elimination') return;
const firstTeamIndex = match.doubleElimination?.placements.firstTeamIndex;
if (firstTeamIndex) {
match.status = 'finished';
match.result = match.result ?? {
riotMatchIds: [],
games: [],
seriesResults: [],
};
match.result.winnerTeamIndex = firstTeamIndex;
return;
}
if (match.status === 'finished') {
match.status = 'in-progress';
}
if (match.result) {
match.result.winnerTeamIndex = undefined;
}
}
public async cancelJoin(
params: CancelMatchJoinParams,
): Promise<MatchDocument> {
@@ -1029,34 +837,4 @@ export class MatchService {
}
return code;
}
private buildSeriesResults(
games: NonNullable<Match['result']>['games'],
): MatchSeriesResult[] {
const bySeries = new Map<string, MatchSeriesResult>();
for (const game of games) {
const teamAIndex = Math.min(game.blueTeamIndex, game.redTeamIndex);
const teamBIndex = Math.max(game.blueTeamIndex, game.redTeamIndex);
const key = game.bracketMatchId ?? `${teamAIndex}:${teamBIndex}`;
const current =
bySeries.get(key) ??
({
bracketMatchId: game.bracketMatchId,
teamAIndex,
teamBIndex,
teamAWins: 0,
teamBWins: 0,
} satisfies MatchSeriesResult);
if (game.winnerTeamIndex === teamAIndex) current.teamAWins += 1;
if (game.winnerTeamIndex === teamBIndex) current.teamBWins += 1;
current.winnerTeamIndex =
current.teamAWins > current.teamBWins
? teamAIndex
: current.teamBWins > current.teamAWins
? teamBIndex
: undefined;
bySeries.set(key, current);
}
return [...bySeries.values()];
}
}