From 22e954cb9e15f9f86eaf3ab03837fb363dc7f99f Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Wed, 5 Feb 2025 10:59:52 +0200 Subject: [PATCH] Handle trimming out third place match in prepared maps --- .../core/PreparedMaps.test.ts | 11 +++++++ .../tournament-bracket/core/PreparedMaps.ts | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/features/tournament-bracket/core/PreparedMaps.test.ts b/app/features/tournament-bracket/core/PreparedMaps.test.ts index 77b081cf5..199b704c4 100644 --- a/app/features/tournament-bracket/core/PreparedMaps.test.ts +++ b/app/features/tournament-bracket/core/PreparedMaps.test.ts @@ -209,6 +209,17 @@ describe("PreparedMaps - trimPreparedEliminationMaps", () => { expect(trimmed).toBe(FOUR_TEAM_SE_PREPARED); }); + test("returns trimmed if third place match disappeared", () => { + const trimmed = PreparedMaps.trimPreparedEliminationMaps({ + preparedMaps: FOUR_TEAM_SE_PREPARED, + teamCount: 3, + bracket: tournament.bracketByIdx(0)!, + }); + + expect(trimmed?.maps.length).toBe(FOUR_TEAM_SE_PREPARED.maps.length - 1); + expect(trimmed?.maps.some((m) => m.groupId === 1)).toBe(false); + }); + test("trims the maps (SE - 1 extra round)", () => { const trimmed = PreparedMaps.trimPreparedEliminationMaps({ preparedMaps: EIGHT_TEAM_SE_PREPARED, diff --git a/app/features/tournament-bracket/core/PreparedMaps.ts b/app/features/tournament-bracket/core/PreparedMaps.ts index cd68394cf..a1811835f 100644 --- a/app/features/tournament-bracket/core/PreparedMaps.ts +++ b/app/features/tournament-bracket/core/PreparedMaps.ts @@ -101,6 +101,10 @@ export function trimPreparedEliminationMaps({ eliminationTeamCountOptions(teamCount)[0].max; if (isPerfectCountMatch) { + if (thirdPlaceMatchDisappeared({ preparedMaps, teamCount, ...rest })) { + return filterOutThirdPlaceMatch(preparedMaps); + } + return preparedMaps; } @@ -160,3 +164,28 @@ function roundsWithVirtualIds( return rounds.map((r, i) => ({ ...r, roundId: virtualIds[i] })); } + +function thirdPlaceMatchDisappeared({ + bracket, + preparedMaps, + teamCount, +}: TrimPreparedEliminationMapsAgs & { preparedMaps: PreparedMaps }) { + if ( + bracket.type !== "single_elimination" || + !bracket.settings?.thirdPlaceMatch + ) { + return false; + } + + const preparedHasThirdPlace = + removeDuplicates(preparedMaps.maps.map((r) => r.groupId)).length > 1; + + return preparedHasThirdPlace && teamCount < 4; +} + +function filterOutThirdPlaceMatch(prepared: PreparedMaps): PreparedMaps { + return { + ...prepared, + maps: prepared.maps.filter((map) => map.groupId === 0), + }; +}