Fix crash on prepared maps trim

This commit is contained in:
Kalle
2024-09-07 08:52:10 +03:00
parent 708c266de4
commit 206646b68a
2 changed files with 68 additions and 2 deletions

View File

@@ -198,6 +198,38 @@ describe("PreparedMaps - trimPreparedEliminationMaps", () => {
);
});
test("trimmed rounds have the same round ids (SE)", () => {
const trimmed = PreparedMaps.trimPreparedEliminationMaps({
preparedMaps: EIGHT_TEAM_SE_PREPARED,
teamCount: 4,
tournament,
type: "single_elimination",
});
const actualBracket = tournament.generateMatchesData(
[1, 2, 3, 4],
"single_elimination",
);
for (const round of actualBracket.round) {
expect(
trimmed!.maps.some((map) => map.roundId === round.id),
`Round ID ${round.id} not found in the actual bracket`,
).toBeTrue();
}
});
test("trimmed rounds start with round id 0", () => {
const trimmed = PreparedMaps.trimPreparedEliminationMaps({
preparedMaps: EIGHT_TEAM_SE_PREPARED,
teamCount: 4,
tournament,
type: "single_elimination",
});
expect(trimmed?.maps[0].roundId).toBe(0);
});
test("trims the maps (SE - disappearing 3rd place match)", () => {
const trimmed = PreparedMaps.trimPreparedEliminationMaps({
preparedMaps: EIGHT_TEAM_SE_PREPARED,
@@ -239,6 +271,27 @@ describe("PreparedMaps - trimPreparedEliminationMaps", () => {
).toBe(expectedFinalsCount);
});
test("trimmed rounds have the same round ids (DE)", () => {
const trimmed = PreparedMaps.trimPreparedEliminationMaps({
preparedMaps: EIGHT_TEAM_DE_PREPARED,
teamCount: 4,
tournament,
type: "double_elimination",
});
const actualBracket = tournament.generateMatchesData(
[1, 2, 3, 4],
"double_elimination",
);
for (const round of actualBracket.round) {
expect(
trimmed!.maps.some((map) => map.roundId === round.id),
`Round ID ${round.id} not found in the actual bracket`,
).toBeTrue();
}
});
const FOUR_TEAM_SE_PREPARED: PreparedMapsType = {
maps: [
{

View File

@@ -1,6 +1,7 @@
import compare from "just-compare";
import type { PreparedMaps } from "~/db/tables";
import { nullFilledArray, removeDuplicates } from "~/utils/arrays";
import invariant from "~/utils/invariant";
import type { Tournament } from "./Tournament";
/** Returns the prepared maps for one exact bracket index OR maps of a "sibling bracket" i.e. bracket that has the same sources */
@@ -130,8 +131,11 @@ function trimMapsByTeamCount({
const actualRoundsCount = actualRoundsForGroup.length;
const trimmedRounds = preparedRoundsForGroup.slice(
preparedRoundsForGroup.length - actualRoundsCount,
const trimmedRounds = roundsWithVirtualIds(
preparedRoundsForGroup.slice(
preparedRoundsForGroup.length - actualRoundsCount,
),
actualRoundsForGroup.map((r) => r.id).sort((a, b) => a - b),
);
result.maps = result.maps.filter((r) => r.groupId !== groupId);
@@ -148,3 +152,12 @@ function trimMapsByTeamCount({
return result;
}
function roundsWithVirtualIds<T extends { roundId: number }>(
rounds: T[],
virtualIds: number[],
) {
invariant(rounds.length === virtualIds.length, "Round id length mismatch");
return rounds.map((r, i) => ({ ...r, roundId: virtualIds[i] }));
}