feat: preserve explicit round robin pairings
This commit is contained in:
1 parent
640e593676
commit
c2f89f9cb2
5 files changed
+347
-34
No files matched your search
@@ -68,6 +68,13 @@ export interface CompetitionGraphTeam {
|
||||
seedNumber?: number;
|
||||
}
|
||||
|
||||
export interface RoundRobinPairing {
|
||||
roundNumber: number;
|
||||
matchNumber: number;
|
||||
teamAId: string;
|
||||
teamBId: string;
|
||||
}
|
||||
|
||||
export type CompetitionGraphFormat =
|
||||
| {
|
||||
type: 'paired-series';
|
||||
@@ -77,6 +84,7 @@ export type CompetitionGraphFormat =
|
||||
type: 'round-robin';
|
||||
rule: FixedGamesSeriesRule;
|
||||
maxRounds?: number;
|
||||
pairings?: readonly RoundRobinPairing[];
|
||||
tieBreakers: readonly ['series-wins', 'game-diff', 'head-to-head'];
|
||||
}
|
||||
| {
|
||||
@@ -116,7 +124,7 @@ type TemplateParticipantSource =
|
||||
}
|
||||
| { type: 'series-slot'; seriesCode: string; slot: SeriesSlot };
|
||||
|
||||
interface RoundRobinPairing {
|
||||
interface CanonicalRoundRobinPairing {
|
||||
roundNumber: number;
|
||||
teamAIndex: number;
|
||||
teamBIndex: number;
|
||||
@@ -143,12 +151,7 @@ export function generateSeriesGraph(
|
||||
);
|
||||
break;
|
||||
case 'round-robin':
|
||||
graph = generateRoundRobinGraph(
|
||||
input.competitionId,
|
||||
teams,
|
||||
input.format.rule,
|
||||
input.format.maxRounds,
|
||||
);
|
||||
graph = generateRoundRobinGraph(input.competitionId, teams, input.format);
|
||||
break;
|
||||
case 'double-elimination':
|
||||
graph = generateDoubleEliminationGraph(
|
||||
@@ -200,10 +203,9 @@ function generatePairedSeriesGraph(
|
||||
function generateRoundRobinGraph(
|
||||
competitionId: string,
|
||||
teams: CompetitionGraphTeam[],
|
||||
rule: FixedGamesSeriesRule,
|
||||
maxRounds?: number,
|
||||
format: Extract<CompetitionGraphFormat, { type: 'round-robin' }>,
|
||||
): SeriesDefinition[] {
|
||||
assertFixedGamesRule(rule);
|
||||
assertFixedGamesRule(format.rule);
|
||||
if (teams.length < 2) {
|
||||
throw graphError(
|
||||
'INVALID_ROUND_ROBIN_TEAM_COUNT',
|
||||
@@ -211,13 +213,16 @@ function generateRoundRobinGraph(
|
||||
);
|
||||
}
|
||||
|
||||
const pairings = roundRobinPairings(teams.length, maxRounds);
|
||||
const matchNumberByRound = new Map<number, number>();
|
||||
const pairings = format.pairings
|
||||
? validateExplicitRoundRobinPairings(
|
||||
teams,
|
||||
format.pairings,
|
||||
format.maxRounds,
|
||||
)
|
||||
: canonicalRoundRobinPairings(teams, format.maxRounds);
|
||||
|
||||
return pairings.map((pairing, index) => {
|
||||
const matchNumber = (matchNumberByRound.get(pairing.roundNumber) ?? 0) + 1;
|
||||
matchNumberByRound.set(pairing.roundNumber, matchNumber);
|
||||
const code = `RR_R${pairing.roundNumber}_M${matchNumber}`;
|
||||
const code = `RR_R${pairing.roundNumber}_M${pairing.matchNumber}`;
|
||||
return createSeriesDefinition({
|
||||
competitionId,
|
||||
code,
|
||||
@@ -225,14 +230,117 @@ function generateRoundRobinGraph(
|
||||
roundNumber: pairing.roundNumber,
|
||||
displayOrder: index + 1,
|
||||
sources: [
|
||||
{ type: 'team', teamId: teams[pairing.teamAIndex].teamId },
|
||||
{ type: 'team', teamId: teams[pairing.teamBIndex].teamId },
|
||||
{ type: 'team', teamId: pairing.teamAId },
|
||||
{ type: 'team', teamId: pairing.teamBId },
|
||||
],
|
||||
rule,
|
||||
rule: format.rule,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function canonicalRoundRobinPairings(
|
||||
teams: CompetitionGraphTeam[],
|
||||
maxRounds?: number,
|
||||
): RoundRobinPairing[] {
|
||||
const matchNumberByRound = new Map<number, number>();
|
||||
return roundRobinPairings(teams.length, maxRounds).map((pairing) => {
|
||||
const matchNumber = (matchNumberByRound.get(pairing.roundNumber) ?? 0) + 1;
|
||||
matchNumberByRound.set(pairing.roundNumber, matchNumber);
|
||||
return {
|
||||
roundNumber: pairing.roundNumber,
|
||||
matchNumber,
|
||||
teamAId: teams[pairing.teamAIndex].teamId,
|
||||
teamBId: teams[pairing.teamBIndex].teamId,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function validateExplicitRoundRobinPairings(
|
||||
teams: CompetitionGraphTeam[],
|
||||
pairings: readonly RoundRobinPairing[],
|
||||
maxRounds?: number,
|
||||
): RoundRobinPairing[] {
|
||||
if (pairings.length === 0) {
|
||||
throw graphError(
|
||||
'INVALID_ROUND_ROBIN_PAIRING',
|
||||
'Explicit round-robin pairings cannot be empty.',
|
||||
);
|
||||
}
|
||||
const teamIds = new Set(teams.map(({ teamId }) => teamId));
|
||||
const positions = new Set<string>();
|
||||
const opponentPairs = new Set<string>();
|
||||
const teamsByRound = new Map<number, Set<string>>();
|
||||
const sorted = pairings
|
||||
.map((pairing) => ({ ...pairing }))
|
||||
.sort(
|
||||
(left, right) =>
|
||||
left.roundNumber - right.roundNumber ||
|
||||
left.matchNumber - right.matchNumber,
|
||||
);
|
||||
|
||||
for (const pairing of sorted) {
|
||||
if (
|
||||
!Number.isInteger(pairing.roundNumber) ||
|
||||
pairing.roundNumber < 1 ||
|
||||
!Number.isInteger(pairing.matchNumber) ||
|
||||
pairing.matchNumber < 1 ||
|
||||
(maxRounds !== undefined && pairing.roundNumber > maxRounds) ||
|
||||
!teamIds.has(pairing.teamAId) ||
|
||||
!teamIds.has(pairing.teamBId) ||
|
||||
pairing.teamAId === pairing.teamBId
|
||||
) {
|
||||
throw graphError(
|
||||
'INVALID_ROUND_ROBIN_PAIRING',
|
||||
'Every explicit round-robin pairing needs a valid position and two known, distinct teams.',
|
||||
);
|
||||
}
|
||||
|
||||
const position = `${pairing.roundNumber}:${pairing.matchNumber}`;
|
||||
if (positions.has(position)) {
|
||||
throw graphError(
|
||||
'DUPLICATE_ROUND_ROBIN_PAIRING_POSITION',
|
||||
`Round-robin pairing position ${position} occurs more than once.`,
|
||||
);
|
||||
}
|
||||
positions.add(position);
|
||||
|
||||
const opponents = [pairing.teamAId, pairing.teamBId].sort().join(':');
|
||||
if (opponentPairs.has(opponents)) {
|
||||
throw graphError(
|
||||
'DUPLICATE_ROUND_ROBIN_OPPONENTS',
|
||||
'A round-robin opponent pair occurs more than once.',
|
||||
);
|
||||
}
|
||||
opponentPairs.add(opponents);
|
||||
|
||||
const roundTeams = teamsByRound.get(pairing.roundNumber) ?? new Set();
|
||||
if (roundTeams.has(pairing.teamAId) || roundTeams.has(pairing.teamBId)) {
|
||||
throw graphError(
|
||||
'INVALID_ROUND_ROBIN_PAIRING',
|
||||
'A team cannot appear in multiple Series in the same round.',
|
||||
);
|
||||
}
|
||||
roundTeams.add(pairing.teamAId);
|
||||
roundTeams.add(pairing.teamBId);
|
||||
teamsByRound.set(pairing.roundNumber, roundTeams);
|
||||
}
|
||||
|
||||
for (const roundNumber of new Set(
|
||||
sorted.map((pairing) => pairing.roundNumber),
|
||||
)) {
|
||||
const matches = sorted.filter(
|
||||
(pairing) => pairing.roundNumber === roundNumber,
|
||||
);
|
||||
if (matches.some((pairing, index) => pairing.matchNumber !== index + 1)) {
|
||||
throw graphError(
|
||||
'INVALID_ROUND_ROBIN_PAIRING',
|
||||
`Round ${roundNumber} match numbers must be contiguous from one.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
function generateDoubleEliminationGraph(
|
||||
competitionId: string,
|
||||
teams: CompetitionGraphTeam[],
|
||||
@@ -436,7 +544,7 @@ function ruleForStage(
|
||||
function roundRobinPairings(
|
||||
teamCount: number,
|
||||
maxRounds?: number,
|
||||
): RoundRobinPairing[] {
|
||||
): CanonicalRoundRobinPairing[] {
|
||||
const bye = -1;
|
||||
const rotating = Array.from({ length: teamCount }, (_, index) => index);
|
||||
if (rotating.length % 2 === 1) rotating.push(bye);
|
||||
@@ -449,7 +557,7 @@ function roundRobinPairings(
|
||||
? Math.floor(maxRounds)
|
||||
: fullRoundCount;
|
||||
const roundCount = Math.max(0, Math.min(fullRoundCount, requestedRounds));
|
||||
const pairings: RoundRobinPairing[] = [];
|
||||
const pairings: CanonicalRoundRobinPairing[] = [];
|
||||
|
||||
for (let roundIndex = 0; roundIndex < roundCount; roundIndex += 1) {
|
||||
for (let index = 0; index < rotating.length / 2; index += 1) {
|
||||
|
||||
Reference in new issue
Block a user