feat: add limited round-robin scheduling
This commit is contained in:
1 parent
24983aeb09
commit
658592973e
15 files changed
+403
-63
No files matched your search
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user