feat: allow tournament code replacement
This commit is contained in:
1 parent
1272c81fa9
commit
303886ed53
5 files changed
+537
-37
No files matched your search
@@ -273,6 +273,107 @@ export class TournamentCodeService {
|
||||
return match;
|
||||
}
|
||||
|
||||
public async replaceForMatchGame(params: {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
gameNumber: number;
|
||||
}): Promise<TournamentCodeSlotView> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const TournamentCodeModel =
|
||||
await this.connectionService.getModel('tournamentCode');
|
||||
const match = await MatchModel.findById(params.matchId).exec();
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (match.status !== 'team-built' && match.status !== 'in-progress') {
|
||||
throw new BadRequestException(
|
||||
'Tournament codes can only be replaced while the match is ready or in progress.',
|
||||
);
|
||||
}
|
||||
|
||||
match.gameCount = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
|
||||
match.tournamentCodeIds = match.tournamentCodeIds ?? [];
|
||||
this.ensureGameSchedule(match);
|
||||
|
||||
const requiredSlots = this.resolveRequiredSlots(match);
|
||||
if (
|
||||
!Number.isInteger(params.gameNumber) ||
|
||||
params.gameNumber < 1 ||
|
||||
params.gameNumber > requiredSlots
|
||||
) {
|
||||
throw new BadRequestException('Invalid tournament code game number.');
|
||||
}
|
||||
if (
|
||||
(match.result?.games ?? []).some(
|
||||
(game) => game.gameNumber === params.gameNumber,
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'This game already has a collected result and cannot be replaced.',
|
||||
);
|
||||
}
|
||||
|
||||
const currentCode = await TournamentCodeModel.findOne({
|
||||
_id: { $in: match.tournamentCodeIds },
|
||||
matchId: match._id,
|
||||
gameNumber: params.gameNumber,
|
||||
}).exec();
|
||||
if (!currentCode) {
|
||||
throw new NotFoundException('Assigned tournament code not found.');
|
||||
}
|
||||
if (currentCode.state === 'used' || currentCode.riotMatchId) {
|
||||
throw new BadRequestException('Used tournament codes cannot be replaced.');
|
||||
}
|
||||
|
||||
const expiresAfter = new Date(Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS);
|
||||
const replacement = await TournamentCodeModel.findOneAndUpdate(
|
||||
{
|
||||
state: 'available',
|
||||
region: DEFAULT_REGION,
|
||||
mapType: 'SUMMONERS_RIFT',
|
||||
teamSize: 5,
|
||||
expiresAt: { $gt: expiresAfter },
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
state: 'reserved',
|
||||
matchId: match._id,
|
||||
gameNumber: params.gameNumber,
|
||||
reservedAt: new Date(),
|
||||
},
|
||||
$unset: { releasedAt: 1 },
|
||||
},
|
||||
{ sort: { expiresAt: 1 }, returnDocument: 'after' },
|
||||
).exec();
|
||||
if (!replacement) {
|
||||
throw new BadRequestException('No available tournament code to replace with.');
|
||||
}
|
||||
|
||||
currentCode.state = 'released';
|
||||
currentCode.matchId = undefined;
|
||||
currentCode.gameNumber = undefined;
|
||||
currentCode.reservedAt = undefined;
|
||||
currentCode.releasedAt = new Date();
|
||||
currentCode.riotMatchId = undefined;
|
||||
match.tournamentCodeIds = this.replaceTournamentCodeId(
|
||||
match.tournamentCodeIds,
|
||||
currentCode._id,
|
||||
replacement._id,
|
||||
);
|
||||
await currentCode.save();
|
||||
await match.save();
|
||||
|
||||
return {
|
||||
gameNumber: params.gameNumber,
|
||||
roundNumber: this.resolveRoundNumberForGameNumber(
|
||||
match,
|
||||
params.gameNumber,
|
||||
),
|
||||
...this.resolveSeriesRefForGameNumber(match, params.gameNumber),
|
||||
code: replacement.code,
|
||||
expiresAt: replacement.expiresAt.toISOString(),
|
||||
state: replacement.state,
|
||||
riotMatchId: replacement.riotMatchId ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
public async releaseFromMatch(
|
||||
matchOrId: MatchDocument | mongoose.Types.ObjectId,
|
||||
): Promise<void> {
|
||||
@@ -390,6 +491,21 @@ export class TournamentCodeService {
|
||||
return (code ?? '').trim().toUpperCase();
|
||||
}
|
||||
|
||||
private replaceTournamentCodeId(
|
||||
currentIds: mongoose.Types.ObjectId[],
|
||||
oldId: mongoose.Types.ObjectId,
|
||||
newId: mongoose.Types.ObjectId,
|
||||
): mongoose.Types.ObjectId[] {
|
||||
let replaced = false;
|
||||
const next = currentIds.map((id) => {
|
||||
if (id.toString() !== oldId.toString()) return id;
|
||||
replaced = true;
|
||||
return newId;
|
||||
});
|
||||
if (!replaced) next.push(newId);
|
||||
return next;
|
||||
}
|
||||
|
||||
private resolveRequiredSlots(match: MatchDocument): number {
|
||||
const gameCount = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT;
|
||||
const gameMode = match.gameMode ?? 'pair';
|
||||
|
||||
Reference in new issue
Block a user