feat: project competition results by team identity
This commit is contained in:
1 parent
39112ff7ff
commit
5d4fe443d0
9 files changed
+3408
No files matched your search
@@ -0,0 +1,399 @@
|
||||
import { projectCompetition } from './competition-projector';
|
||||
import { generateSeriesGraph } from './series-graph';
|
||||
|
||||
describe('projectCompetition', () => {
|
||||
it('keeps a three-way round-robin cycle on a shared rank', () => {
|
||||
const competitionId = 'competition-round-robin-tie';
|
||||
const teams = ['team-1', 'team-2', 'team-3'].map((teamId, index) => ({
|
||||
teamId,
|
||||
displayOrder: index + 1,
|
||||
seedNumber: index + 1,
|
||||
}));
|
||||
const format = {
|
||||
type: 'round-robin' as const,
|
||||
rule: fixedGames(1),
|
||||
tieBreakers: ['series-wins', 'game-diff', 'head-to-head'] as const,
|
||||
};
|
||||
const seriesGraph = generateSeriesGraph({
|
||||
competitionId,
|
||||
teams,
|
||||
format,
|
||||
});
|
||||
const accept = acceptedGameFactory(competitionId);
|
||||
const oneVersusTwo = requireRoundRobinSeries(
|
||||
seriesGraph,
|
||||
'team-1',
|
||||
'team-2',
|
||||
);
|
||||
const oneVersusThree = requireRoundRobinSeries(
|
||||
seriesGraph,
|
||||
'team-1',
|
||||
'team-3',
|
||||
);
|
||||
const twoVersusThree = requireRoundRobinSeries(
|
||||
seriesGraph,
|
||||
'team-2',
|
||||
'team-3',
|
||||
);
|
||||
const facts = [
|
||||
accept(oneVersusTwo, 1, 'team-1', 'team-2', 'team-1'),
|
||||
accept(twoVersusThree, 1, 'team-2', 'team-3', 'team-2'),
|
||||
accept(oneVersusThree, 1, 'team-3', 'team-1', 'team-3'),
|
||||
];
|
||||
|
||||
const projection = projectCompetition({
|
||||
competitionId,
|
||||
revision: facts.length,
|
||||
projectorVersion: 1,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts,
|
||||
});
|
||||
|
||||
expect(projection.outcome).toEqual({
|
||||
type: 'round-robin',
|
||||
leaderTeamIds: ['team-1', 'team-2', 'team-3'],
|
||||
standings: [
|
||||
standing('team-1', 1),
|
||||
standing('team-2', 1),
|
||||
standing('team-3', 1),
|
||||
],
|
||||
completedAt: '2026-07-12T00:00:03.000Z',
|
||||
});
|
||||
expect(projection.games).toHaveLength(3);
|
||||
expect(projection.gameSlots).toHaveLength(3);
|
||||
expect(projection.rounds).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ stage: 'round-robin', roundNumber: 1 }),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('attributes WB_R2_M2 wins to team ids when series and bracket slot orders differ', () => {
|
||||
const competitionId = 'competition-wb-r2-m2-regression';
|
||||
const teams = [
|
||||
seededTeam('team-1', 1),
|
||||
seededTeam('team-8', 2),
|
||||
seededTeam('team-3', 3),
|
||||
seededTeam('team-4', 4),
|
||||
seededTeam('team-5', 5),
|
||||
seededTeam('team-6', 6),
|
||||
seededTeam('team-7', 7),
|
||||
seededTeam('team-2', 8),
|
||||
];
|
||||
const format = doubleEliminationFormat({ upperWinsRequired: 2 });
|
||||
const seriesGraph = generateSeriesGraph({
|
||||
competitionId,
|
||||
teams,
|
||||
format,
|
||||
});
|
||||
const wbRoundOneMatchThree = requireSeries(seriesGraph, 'WB_R1_M3');
|
||||
const wbRoundOneMatchFour = requireSeries(seriesGraph, 'WB_R1_M4');
|
||||
const wbRoundTwoMatchTwo = requireSeries(seriesGraph, 'WB_R2_M2');
|
||||
const accept = acceptedGameFactory(competitionId);
|
||||
const facts = [
|
||||
accept(wbRoundOneMatchThree, 1, 'team-8', 'team-7', 'team-8'),
|
||||
accept(wbRoundOneMatchThree, 2, 'team-7', 'team-8', 'team-8'),
|
||||
accept(wbRoundOneMatchFour, 1, 'team-3', 'team-6', 'team-3'),
|
||||
accept(wbRoundOneMatchFour, 2, 'team-6', 'team-3', 'team-3'),
|
||||
accept(wbRoundTwoMatchTwo, 1, 'team-3', 'team-8', 'team-8'),
|
||||
accept(wbRoundTwoMatchTwo, 2, 'team-8', 'team-3', 'team-3'),
|
||||
accept(wbRoundTwoMatchTwo, 3, 'team-3', 'team-8', 'team-8'),
|
||||
];
|
||||
|
||||
const projection = projectCompetition({
|
||||
competitionId,
|
||||
revision: facts.length,
|
||||
projectorVersion: 1,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts,
|
||||
});
|
||||
|
||||
expect(projectedSeries(projection, 'WB_R2_M2')).toMatchObject({
|
||||
status: 'completed',
|
||||
entries: [
|
||||
{ slot: 'A', teamId: 'team-8', gameWins: 2 },
|
||||
{ slot: 'B', teamId: 'team-3', gameWins: 1 },
|
||||
],
|
||||
outcome: {
|
||||
type: 'played',
|
||||
winnerTeamId: 'team-8',
|
||||
loserTeamId: 'team-3',
|
||||
},
|
||||
});
|
||||
expect(projectedSeries(projection, 'WB_FINAL')).toMatchObject({
|
||||
entries: expect.arrayContaining([
|
||||
{ slot: 'B', teamId: 'team-8', gameWins: 0 },
|
||||
]),
|
||||
});
|
||||
expect(projectedSeries(projection, 'LB_R2_M1')).toMatchObject({
|
||||
entries: expect.arrayContaining([
|
||||
{ slot: 'B', teamId: 'team-3', gameWins: 0 },
|
||||
]),
|
||||
});
|
||||
expect(projection.games).toHaveLength(7);
|
||||
expect(
|
||||
projection.games.filter(
|
||||
(game) => game.seriesId === wbRoundTwoMatchTwo.id,
|
||||
),
|
||||
).toEqual([
|
||||
expect.objectContaining({ winnerTeamId: 'team-8', ordinal: 1 }),
|
||||
expect.objectContaining({ winnerTeamId: 'team-3', ordinal: 2 }),
|
||||
expect.objectContaining({ winnerTeamId: 'team-8', ordinal: 3 }),
|
||||
]);
|
||||
expect(
|
||||
projection.connectors.some(
|
||||
(connector) =>
|
||||
connector.from.seriesId === wbRoundTwoMatchTwo.id &&
|
||||
connector.from.select === 'winner' &&
|
||||
connector.to.slot === 'B',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('activates the predefined reset final only when the lower-bracket finalist wins GF', () => {
|
||||
const competitionId = 'competition-grand-final-reset';
|
||||
const teams = Array.from({ length: 4 }, (_, index) =>
|
||||
seededTeam(`team-${index + 1}`, index + 1),
|
||||
);
|
||||
const format = doubleEliminationFormat({ upperWinsRequired: 1 });
|
||||
const seriesGraph = generateSeriesGraph({
|
||||
competitionId,
|
||||
teams,
|
||||
format,
|
||||
});
|
||||
const resetDefinition = requireSeries(seriesGraph, 'GF_RESET');
|
||||
|
||||
const initial = projectCompetition({
|
||||
competitionId,
|
||||
revision: 0,
|
||||
projectorVersion: 1,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts: [],
|
||||
});
|
||||
expect(projectedSeries(initial, 'GF_RESET')).toMatchObject({
|
||||
seriesId: resetDefinition.id,
|
||||
status: 'inactive',
|
||||
});
|
||||
|
||||
const accept = acceptedGameFactory(competitionId);
|
||||
const facts = [
|
||||
accept(
|
||||
requireSeries(seriesGraph, 'WB_R1_M1'),
|
||||
1,
|
||||
'team-1',
|
||||
'team-4',
|
||||
'team-1',
|
||||
),
|
||||
accept(
|
||||
requireSeries(seriesGraph, 'WB_R1_M2'),
|
||||
1,
|
||||
'team-2',
|
||||
'team-3',
|
||||
'team-2',
|
||||
),
|
||||
accept(
|
||||
requireSeries(seriesGraph, 'WB_FINAL'),
|
||||
1,
|
||||
'team-1',
|
||||
'team-2',
|
||||
'team-1',
|
||||
),
|
||||
accept(
|
||||
requireSeries(seriesGraph, 'LB_R1_M1'),
|
||||
1,
|
||||
'team-3',
|
||||
'team-4',
|
||||
'team-3',
|
||||
),
|
||||
accept(
|
||||
requireSeries(seriesGraph, 'LB_FINAL'),
|
||||
1,
|
||||
'team-3',
|
||||
'team-2',
|
||||
'team-2',
|
||||
),
|
||||
accept(requireSeries(seriesGraph, 'GF'), 1, 'team-2', 'team-1', 'team-2'),
|
||||
];
|
||||
|
||||
const afterGrandFinal = projectCompetition({
|
||||
competitionId,
|
||||
revision: facts.length,
|
||||
projectorVersion: 1,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts,
|
||||
});
|
||||
|
||||
expect(projectedSeries(afterGrandFinal, 'GF_RESET')).toMatchObject({
|
||||
seriesId: resetDefinition.id,
|
||||
status: 'ready',
|
||||
entries: [
|
||||
{ slot: 'A', teamId: 'team-1', gameWins: 0 },
|
||||
{ slot: 'B', teamId: 'team-2', gameWins: 0 },
|
||||
],
|
||||
});
|
||||
expect(afterGrandFinal).not.toHaveProperty('outcome');
|
||||
expect(afterGrandFinal.gameSlots).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
seriesId: resetDefinition.id,
|
||||
status: 'ready',
|
||||
entries: [
|
||||
{ slot: 'A', teamId: 'team-1' },
|
||||
{ slot: 'B', teamId: 'team-2' },
|
||||
],
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function fixedGames(gameCount: number) {
|
||||
return {
|
||||
type: 'fixed-games' as const,
|
||||
gameCount,
|
||||
drawPolicy: 'allow' as const,
|
||||
};
|
||||
}
|
||||
|
||||
function seededTeam(teamId: string, seedNumber: number) {
|
||||
return { teamId, displayOrder: seedNumber, seedNumber };
|
||||
}
|
||||
|
||||
function doubleEliminationFormat(params: { upperWinsRequired: 1 | 2 }) {
|
||||
const upper =
|
||||
params.upperWinsRequired === 1
|
||||
? { type: 'first-to' as const, winsRequired: 1, maxGames: 1 }
|
||||
: { type: 'first-to' as const, winsRequired: 2, maxGames: 3 };
|
||||
const singleGame = {
|
||||
type: 'first-to' as const,
|
||||
winsRequired: 1,
|
||||
maxGames: 1,
|
||||
};
|
||||
return {
|
||||
type: 'double-elimination' as const,
|
||||
resetFinalEnabled: true,
|
||||
rules: {
|
||||
upper,
|
||||
lower: singleGame,
|
||||
grandFinal: singleGame,
|
||||
resetFinal: singleGame,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function requireSeries<
|
||||
T extends { code?: string; id: string; gameSlots: Array<GameSlotLike> },
|
||||
>(graph: T[], code: string): T {
|
||||
const definition = graph.find((series) => series.code === code);
|
||||
if (!definition) throw new Error(`Missing series ${code}`);
|
||||
return definition;
|
||||
}
|
||||
|
||||
function requireRoundRobinSeries<
|
||||
T extends {
|
||||
id: string;
|
||||
gameSlots: Array<GameSlotLike>;
|
||||
slots: Array<{ source: { type: string; teamId?: string } }>;
|
||||
},
|
||||
>(graph: T[], leftTeamId: string, rightTeamId: string): T {
|
||||
const expected = new Set([leftTeamId, rightTeamId]);
|
||||
const definition = graph.find((series) => {
|
||||
const actual = series.slots
|
||||
.filter((slot) => slot.source.type === 'team')
|
||||
.map((slot) => slot.source.teamId);
|
||||
return (
|
||||
actual.length === 2 && actual.every((teamId) => expected.has(teamId!))
|
||||
);
|
||||
});
|
||||
if (!definition) {
|
||||
throw new Error(`Missing round-robin series ${leftTeamId}/${rightTeamId}`);
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
|
||||
function acceptedGameFactory(competitionId: string) {
|
||||
let revision = 0;
|
||||
return <T extends { id: string; gameSlots: Array<GameSlotLike> }>(
|
||||
definition: T,
|
||||
ordinal: number,
|
||||
blueTeamId: string,
|
||||
redTeamId: string,
|
||||
winnerTeamId: string,
|
||||
) => {
|
||||
revision += 1;
|
||||
const gameSlot = definition.gameSlots.find(
|
||||
(candidate) => candidate.ordinal === ordinal,
|
||||
);
|
||||
if (!gameSlot) {
|
||||
throw new Error(`Missing game slot ${definition.id}/${ordinal}`);
|
||||
}
|
||||
return {
|
||||
id: `fact-${revision}`,
|
||||
type: 'game-accepted' as const,
|
||||
competitionId,
|
||||
revision,
|
||||
ordinalWithinRevision: 1,
|
||||
commandId: `command-${revision}`,
|
||||
schemaVersion: 1,
|
||||
actor: { type: 'system' as const },
|
||||
occurredAt: new Date(
|
||||
`2026-07-12T00:00:${String(revision).padStart(2, '0')}.000Z`,
|
||||
),
|
||||
recordedAt: new Date(
|
||||
`2026-07-12T00:01:${String(revision).padStart(2, '0')}.000Z`,
|
||||
),
|
||||
gameId: `game-${revision}`,
|
||||
seriesId: definition.id,
|
||||
gameSlotId: gameSlot.id,
|
||||
ordinal,
|
||||
evidence: {
|
||||
type: 'riot' as const,
|
||||
riotMatchId: `KR_${competitionId}_${revision}`,
|
||||
payloadHash: `sha256:${competitionId}:${revision}`,
|
||||
},
|
||||
mapping: {
|
||||
mappingVersion: 1,
|
||||
confidence: 1,
|
||||
blueTeamId,
|
||||
redTeamId,
|
||||
winnerTeamId,
|
||||
participantAssignments: [],
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function projectedSeries<
|
||||
T extends { series: Array<{ code?: string; seriesId: string }> },
|
||||
>(projection: T, code: string) {
|
||||
const series = projection.series.find((candidate) => candidate.code === code);
|
||||
if (!series) throw new Error(`Missing projected series ${code}`);
|
||||
return series;
|
||||
}
|
||||
|
||||
function standing(teamId: string, rank: number) {
|
||||
return {
|
||||
teamId,
|
||||
rank,
|
||||
seriesPlayed: 2,
|
||||
seriesWins: 1,
|
||||
seriesLosses: 1,
|
||||
seriesDraws: 0,
|
||||
gameWins: 1,
|
||||
gameLosses: 1,
|
||||
gameDiff: 0,
|
||||
};
|
||||
}
|
||||
|
||||
interface GameSlotLike {
|
||||
id: string;
|
||||
ordinal: number;
|
||||
}
|
||||
Reference in new issue
Block a user