From e8c612fe8be022853facfaacbff29cb08c52eec2 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Mon, 10 Jan 2022 09:20:50 +0200 Subject: [PATCH] Loser source in R2 if R1 skipped due to BYE --- app/services/bracket.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/services/bracket.ts b/app/services/bracket.ts index 264b1dd1a..be128b27d 100644 --- a/app/services/bracket.ts +++ b/app/services/bracket.ts @@ -115,23 +115,47 @@ function addLoserTeamSourceInfo({ winners: BracketModifiedSide; losers: BracketModifiedSide; }): BracketModifiedSide { - const matchIdToSourceMatchNumber = winners + const nextMatchAfterBye = (id: string) => { + const match = losers + .flatMap((round) => round.matches) + .find((match) => match.id === id); + if (!match) return; + if (match.number !== 0) return; + + return match.winnerDestinationMatchId; + }; + + const matchIdToSourceMatchNumbers = winners .flatMap((round) => round.matches) .reduce((acc: Map, match) => { if (!match.loserDestinationMatchId) return acc; - return acc.set( + acc.set( match.loserDestinationMatchId, (acc.get(match.loserDestinationMatchId) ?? []) .concat(match.number) .sort((a, b) => a - b) ); + + const nextMatchAfterByeId = nextMatchAfterBye( + match.loserDestinationMatchId + ); + if (nextMatchAfterByeId) { + acc.set( + nextMatchAfterByeId, + (acc.get(match.loserDestinationMatchId) ?? []) + .concat(match.number) + .sort((a, b) => a - b) + ); + } + + return acc; }, new Map()); return losers.map((round) => ({ ...round, matches: round.matches.map((match) => { - const sourceMatches = matchIdToSourceMatchNumber.get(match.id); + const sourceMatches = matchIdToSourceMatchNumbers.get(match.id); return { ...match, participantSourceMatches: sourceMatches