feat(web): align double-elimination winner connector rails
This commit is contained in:
1 parent
61f1db0e6a
commit
8ec9edc35f
5 files changed
+866
-44
No files matched your search
@@ -367,6 +367,7 @@ test("orders mobile groups as winners, losers, grand final, then reset final", (
|
||||
shuffledLayout.mobileGroups.map((group) => group.seriesIds),
|
||||
orderedLayout.mobileGroups.map((group) => group.seriesIds),
|
||||
);
|
||||
assert.deepEqual(shuffledLayout.connectors, orderedLayout.connectors);
|
||||
assert.deepEqual(
|
||||
shuffledLayout.mobileGroups.flatMap((group) => group.seriesIds).sort(),
|
||||
shuffled.projection.series.map((series) => series.seriesId).sort(),
|
||||
@@ -474,11 +475,10 @@ test("returns null instead of throwing for an unknown connector discriminator",
|
||||
);
|
||||
});
|
||||
|
||||
test("keeps connector rails distinct and outside intervening card rectangles", () => {
|
||||
test("keeps connector rails finite and outside intervening card rectangles", () => {
|
||||
const { projection } = eightTeamProjection();
|
||||
const layout = requireLayout(projection, EIGHT_TEAM_LAYOUT);
|
||||
const nodes = new Map(layout.nodes.map((node) => [node.seriesId, node]));
|
||||
const routesByTargetColumn = new Map<number, number[]>();
|
||||
|
||||
for (const route of layout.connectors) {
|
||||
const source = nodes.get(route.fromSeriesId);
|
||||
@@ -511,23 +511,256 @@ test("keeps connector rails distinct and outside intervening card rectangles", (
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const rails = routesByTargetColumn.get(target.column) ?? [];
|
||||
rails.push(route.verticalX);
|
||||
routesByTargetColumn.set(target.column, rails);
|
||||
test("aligns paired winner bends on one shared rail", () => {
|
||||
const fourTeam = fourTeamProjection();
|
||||
const fourTeamLayout = requireLayout(fourTeam.projection, FOUR_TEAM_LAYOUT);
|
||||
for (const targetSeriesId of [fourTeam.ids.WB_FINAL, fourTeam.ids.GF]) {
|
||||
const routes = winnerRoutesTo(fourTeamLayout, targetSeriesId);
|
||||
assert.equal(routes.length, 2);
|
||||
assert.equal(routes[0].verticalX, routes[1].verticalX);
|
||||
}
|
||||
|
||||
for (const rails of routesByTargetColumn.values()) {
|
||||
assert.equal(new Set(rails).size, rails.length);
|
||||
const { projection, ids } = eightTeamProjection();
|
||||
const layout = requireLayout(projection, EIGHT_TEAM_LAYOUT);
|
||||
|
||||
for (const targetSeriesId of [
|
||||
ids.WB_R2_M1,
|
||||
ids.WB_R2_M2,
|
||||
ids.WB_FINAL,
|
||||
ids.LB_R3_M1,
|
||||
ids.GF,
|
||||
]) {
|
||||
const routes = winnerRoutesTo(layout, targetSeriesId);
|
||||
assert.equal(routes.length, 2);
|
||||
assert.equal(
|
||||
routes[0].verticalX,
|
||||
routes[1].verticalX,
|
||||
`${targetSeriesId} winner routes must share one bend rail`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("keeps reversed winner slots on deterministic non-overlapping rails", () => {
|
||||
const { projection, ids } = fourTeamProjection();
|
||||
const reversed = cloneProjection(projection);
|
||||
const firstWinner = reversed.connectors.find(
|
||||
({ from, to }) =>
|
||||
from.seriesId === ids.WB_R1_M1 &&
|
||||
from.select === "winner" &&
|
||||
to.seriesId === ids.WB_FINAL,
|
||||
);
|
||||
const secondWinner = reversed.connectors.find(
|
||||
({ from, to }) =>
|
||||
from.seriesId === ids.WB_R1_M2 &&
|
||||
from.select === "winner" &&
|
||||
to.seriesId === ids.WB_FINAL,
|
||||
);
|
||||
assert.ok(firstWinner);
|
||||
assert.ok(secondWinner);
|
||||
firstWinner.to = { seriesId: ids.WB_FINAL, slot: "B" };
|
||||
secondWinner.to = { seriesId: ids.WB_FINAL, slot: "A" };
|
||||
|
||||
const orderedLayout = requireLayout(reversed, FOUR_TEAM_LAYOUT);
|
||||
const shuffled = cloneProjection(reversed);
|
||||
shuffled.connectors = shuffledCopy(shuffled.connectors);
|
||||
const shuffledLayout = requireLayout(shuffled, FOUR_TEAM_LAYOUT);
|
||||
const routes = winnerRoutesTo(orderedLayout, ids.WB_FINAL);
|
||||
|
||||
assert.equal(routes.length, 2);
|
||||
assert.equal(new Set(routes.map(({ verticalX }) => verticalX)).size, 2);
|
||||
assert.equal(countCollinearConnectorOverlaps(orderedLayout.connectors), 0);
|
||||
assert.deepEqual(shuffledLayout.connectors, orderedLayout.connectors);
|
||||
});
|
||||
|
||||
test("moves reversed winner rails clear of overlapping connector underlays", () => {
|
||||
const { projection, ids } = eightTeamProjection();
|
||||
const reversed = cloneProjection(projection);
|
||||
const firstWinner = reversed.connectors.find(
|
||||
({ from, to }) =>
|
||||
from.seriesId === ids.WB_R1_M1 &&
|
||||
from.select === "winner" &&
|
||||
to.seriesId === ids.WB_R2_M1,
|
||||
);
|
||||
const secondWinner = reversed.connectors.find(
|
||||
({ from, to }) =>
|
||||
from.seriesId === ids.WB_R1_M2 &&
|
||||
from.select === "winner" &&
|
||||
to.seriesId === ids.WB_R2_M1,
|
||||
);
|
||||
assert.ok(firstWinner);
|
||||
assert.ok(secondWinner);
|
||||
firstWinner.to = { seriesId: ids.WB_R2_M1, slot: "B" };
|
||||
secondWinner.to = { seriesId: ids.WB_R2_M1, slot: "A" };
|
||||
|
||||
const layout = requireLayout(reversed, EIGHT_TEAM_LAYOUT);
|
||||
const shuffled = cloneProjection(reversed);
|
||||
shuffled.connectors = shuffledCopy(shuffled.connectors);
|
||||
const shuffledLayout = requireLayout(shuffled, EIGHT_TEAM_LAYOUT);
|
||||
const winners = winnerRoutesTo(layout, ids.WB_R2_M1);
|
||||
|
||||
assert.equal(winners.length, 2);
|
||||
for (const winner of winners) {
|
||||
for (const route of layout.connectors) {
|
||||
if (
|
||||
route === winner ||
|
||||
!intervalsStrictlyOverlap(
|
||||
winner.sourceY,
|
||||
winner.targetY,
|
||||
route.sourceY,
|
||||
route.targetY,
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
assert.ok(
|
||||
Math.abs(winner.verticalX - route.verticalX) >=
|
||||
DOUBLE_ELIMINATION_BRACKET_GEOMETRY.connectorRailClearance,
|
||||
`${winner.fromSeriesId} winner rail ${winner.verticalX} is too close to ${route.fromSeriesId} ${route.kind} rail ${route.verticalX}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(shuffledLayout.connectors, layout.connectors);
|
||||
});
|
||||
|
||||
test("centers conflict-free paired winner bends in the routing gap", () => {
|
||||
const fourTeam = fourTeamProjection();
|
||||
const fourTeamLayout = requireLayout(fourTeam.projection, FOUR_TEAM_LAYOUT);
|
||||
const fourTeamGrandFinal = nodeFor(fourTeamLayout, fourTeam.ids.GF);
|
||||
assert.deepEqual(
|
||||
winnerRoutesTo(fourTeamLayout, fourTeam.ids.GF).map(
|
||||
({ verticalX }) => verticalX,
|
||||
),
|
||||
[
|
||||
fourTeamGrandFinal.x -
|
||||
DOUBLE_ELIMINATION_BRACKET_GEOMETRY.columnGap / 2,
|
||||
fourTeamGrandFinal.x -
|
||||
DOUBLE_ELIMINATION_BRACKET_GEOMETRY.columnGap / 2,
|
||||
],
|
||||
);
|
||||
|
||||
const { projection, ids } = eightTeamProjection();
|
||||
const layout = requireLayout(projection, EIGHT_TEAM_LAYOUT);
|
||||
|
||||
for (const targetSeriesId of [ids.LB_R3_M1, ids.GF]) {
|
||||
const target = nodeFor(layout, targetSeriesId);
|
||||
const expectedVerticalX =
|
||||
target.x - DOUBLE_ELIMINATION_BRACKET_GEOMETRY.columnGap / 2;
|
||||
const routes = winnerRoutesTo(layout, targetSeriesId);
|
||||
|
||||
assert.equal(routes.length, 2);
|
||||
assert.deepEqual(
|
||||
routes.map(({ verticalX }) => verticalX),
|
||||
[expectedVerticalX, expectedVerticalX],
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("preserves baseline rails for loser and reset paths", () => {
|
||||
const { projection } = eightTeamProjection({ resetFinal: true });
|
||||
const layout = requireLayout(projection, EIGHT_TEAM_RESET_LAYOUT);
|
||||
const nodes = new Map(layout.nodes.map((node) => [node.seriesId, node]));
|
||||
const routesByTargetColumn = new Map<number, ConnectorLayout[]>();
|
||||
|
||||
for (const route of layout.connectors) {
|
||||
const target = nodes.get(route.toSeriesId);
|
||||
assert.ok(target);
|
||||
const routes = routesByTargetColumn.get(target.column) ?? [];
|
||||
routes.push(route);
|
||||
routesByTargetColumn.set(target.column, routes);
|
||||
}
|
||||
|
||||
for (const routes of routesByTargetColumn.values()) {
|
||||
routes.sort(
|
||||
(left, right) =>
|
||||
right.sourceY - left.sourceY ||
|
||||
right.targetY - left.targetY ||
|
||||
connectorLayoutKey(left).localeCompare(connectorLayoutKey(right)),
|
||||
);
|
||||
routes.forEach((route, index) => {
|
||||
if (route.kind === "winner") return;
|
||||
assert.equal(
|
||||
route.verticalX,
|
||||
route.targetX -
|
||||
DOUBLE_ELIMINATION_BRACKET_GEOMETRY.columnGap +
|
||||
(DOUBLE_ELIMINATION_BRACKET_GEOMETRY.columnGap * (index + 1)) /
|
||||
(routes.length + 1),
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("keeps paired winner rails clear of overlapping loser paths", () => {
|
||||
const fourTeam = fourTeamProjection();
|
||||
const eightTeam = eightTeamProjection();
|
||||
const fixtures = [
|
||||
{
|
||||
layout: requireLayout(fourTeam.projection, FOUR_TEAM_LAYOUT),
|
||||
pairedWinnerTargets: new Set<SeriesId>([
|
||||
fourTeam.ids.WB_FINAL,
|
||||
fourTeam.ids.GF,
|
||||
]),
|
||||
},
|
||||
{
|
||||
layout: requireLayout(eightTeam.projection, EIGHT_TEAM_LAYOUT),
|
||||
pairedWinnerTargets: new Set<SeriesId>([
|
||||
eightTeam.ids.WB_R2_M1,
|
||||
eightTeam.ids.WB_R2_M2,
|
||||
eightTeam.ids.WB_FINAL,
|
||||
eightTeam.ids.LB_R3_M1,
|
||||
eightTeam.ids.GF,
|
||||
]),
|
||||
},
|
||||
];
|
||||
|
||||
for (const { layout, pairedWinnerTargets } of fixtures) {
|
||||
for (const winner of layout.connectors) {
|
||||
if (
|
||||
winner.kind !== "winner" ||
|
||||
!pairedWinnerTargets.has(winner.toSeriesId)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
for (const loser of layout.connectors) {
|
||||
if (
|
||||
loser.kind !== "loser" ||
|
||||
!intervalsStrictlyOverlap(
|
||||
winner.sourceY,
|
||||
winner.targetY,
|
||||
loser.sourceY,
|
||||
loser.targetY,
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
assert.ok(
|
||||
Math.abs(winner.verticalX - loser.verticalX) >=
|
||||
DOUBLE_ELIMINATION_BRACKET_GEOMETRY.connectorRailClearance,
|
||||
`${winner.fromSeriesId} winner rail ${winner.verticalX} is too close to ${loser.fromSeriesId} loser rail ${loser.verticalX}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("minimizes connector crossings without introducing segment overlaps", () => {
|
||||
const { projection } = eightTeamProjection();
|
||||
const layout = requireLayout(projection, EIGHT_TEAM_LAYOUT);
|
||||
const fourTeam = fourTeamProjection();
|
||||
const fourTeamReset = fourTeamProjection({ resetFinal: true });
|
||||
const eightTeam = eightTeamProjection();
|
||||
const eightTeamReset = eightTeamProjection({ resetFinal: true });
|
||||
const layouts = [
|
||||
requireLayout(fourTeam.projection, FOUR_TEAM_LAYOUT),
|
||||
requireLayout(fourTeamReset.projection, FOUR_TEAM_RESET_LAYOUT),
|
||||
requireLayout(eightTeam.projection, EIGHT_TEAM_LAYOUT),
|
||||
requireLayout(eightTeamReset.projection, EIGHT_TEAM_RESET_LAYOUT),
|
||||
];
|
||||
|
||||
assert.equal(countStrictConnectorCrossings(layout.connectors), 12);
|
||||
assert.equal(countCollinearConnectorOverlaps(layout.connectors), 0);
|
||||
assert.ok(countStrictConnectorCrossings(layouts[2].connectors) <= 12);
|
||||
for (const layout of layouts) {
|
||||
assert.equal(countCollinearConnectorOverlaps(layout.connectors), 0);
|
||||
}
|
||||
});
|
||||
|
||||
test("aligns connector endpoints with the A and B entry row centers", () => {
|
||||
@@ -921,6 +1154,18 @@ function segment(
|
||||
};
|
||||
}
|
||||
|
||||
function intervalsStrictlyOverlap(
|
||||
firstStart: number,
|
||||
firstEnd: number,
|
||||
secondStart: number,
|
||||
secondEnd: number,
|
||||
): boolean {
|
||||
return (
|
||||
Math.min(firstStart, firstEnd) < Math.max(secondStart, secondEnd) &&
|
||||
Math.min(secondStart, secondEnd) < Math.max(firstStart, firstEnd)
|
||||
);
|
||||
}
|
||||
|
||||
function countStrictConnectorCrossings(routes: readonly ConnectorLayout[]): number {
|
||||
let crossings = 0;
|
||||
for (let leftIndex = 0; leftIndex < routes.length; leftIndex += 1) {
|
||||
@@ -1009,6 +1254,16 @@ function nodeFor(
|
||||
return node;
|
||||
}
|
||||
|
||||
function winnerRoutesTo(
|
||||
layout: NonNullable<ReturnType<typeof createDoubleEliminationBracketLayout>>,
|
||||
targetSeriesId: SeriesId,
|
||||
) {
|
||||
return layout.connectors.filter(
|
||||
(route) =>
|
||||
route.kind === "winner" && route.toSeriesId === targetSeriesId,
|
||||
);
|
||||
}
|
||||
|
||||
function routeFrom(
|
||||
layout: NonNullable<ReturnType<typeof createDoubleEliminationBracketLayout>>,
|
||||
id: SeriesId,
|
||||
@@ -1036,6 +1291,10 @@ function connectorIdentity(
|
||||
};
|
||||
}
|
||||
|
||||
function connectorLayoutKey(connector: ConnectorLayout): string {
|
||||
return `${connector.fromSeriesId}:${connector.select}:${connector.toSeriesId}:${connector.toSlot}`;
|
||||
}
|
||||
|
||||
function mobileGroupKeys(
|
||||
layout: NonNullable<ReturnType<typeof createDoubleEliminationBracketLayout>>,
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user