From f37604746362b320cdd01d570a2ca7151bbe294f Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Wed, 12 Jan 2022 18:13:09 +0200 Subject: [PATCH] Refactor BracketModified to common format --- app/components/tournament/BracketActions.tsx | 23 ++++++------------- .../tournament/DuringMatchActions.tsx | 4 ++-- .../tournament/EliminationBracket.tsx | 22 +++++++++--------- .../tournament/EliminationBracketMatch.tsx | 2 +- .../$organization.$tournament/bracket.$id.tsx | 4 ++-- app/services/bracket.ts | 17 ++++++++++---- 6 files changed, 35 insertions(+), 37 deletions(-) diff --git a/app/components/tournament/BracketActions.tsx b/app/components/tournament/BracketActions.tsx index 3da9cfeb2..0115d5e05 100644 --- a/app/components/tournament/BracketActions.tsx +++ b/app/components/tournament/BracketActions.tsx @@ -29,22 +29,13 @@ export function BracketActions() { if (tournamentIsOver || !ownTeam) return null; - const allMatches = [ - ...data.winners.flatMap((round, roundI) => - round.matches.map((match) => ({ - ...match, - round, - isFirstRound: roundI === 0, - })) - ), - ...data.losers.flatMap((round) => - round.matches.map((match) => ({ - ...match, - round, - isFirstRound: false, - })) - ), - ]; + const allMatches = data.rounds.flatMap((round, roundI) => + round.matches.map((match) => ({ + ...match, + round, + isFirstRound: roundI === 0, + })) + ); const currentMatch = allMatches.find((match) => { const hasBothParticipants = match.participants?.every( (p) => typeof p === "string" diff --git a/app/components/tournament/DuringMatchActions.tsx b/app/components/tournament/DuringMatchActions.tsx index c0c3002fd..af4ccdb82 100644 --- a/app/components/tournament/DuringMatchActions.tsx +++ b/app/components/tournament/DuringMatchActions.tsx @@ -15,8 +15,8 @@ export function DuringMatchActions({ currentRound, }: { ownTeam: Unpacked; - currentMatch: Unpacked["matches"]>; - currentRound: Unpacked; + currentMatch: Unpacked["matches"]>; + currentRound: Unpacked; }) { const [, parentRoute] = useMatches(); const { teams, seeds } = parentRoute.data as FindTournamentByNameForUrlI; diff --git a/app/components/tournament/EliminationBracket.tsx b/app/components/tournament/EliminationBracket.tsx index 92126d3d6..be30cb334 100644 --- a/app/components/tournament/EliminationBracket.tsx +++ b/app/components/tournament/EliminationBracket.tsx @@ -6,31 +6,31 @@ import { MyCSSProperties, Unpacked } from "~/utils"; import { EliminationBracketMatch } from "./EliminationBracketMatch"; export function EliminationBracket({ - bracketSide, + rounds, ownTeamName, }: { - bracketSide: BracketModified["winners"]; + rounds: BracketModified["rounds"]; ownTeamName?: string; }) { const style: MyCSSProperties = { - "--brackets-columns": bracketSide.length, - "--brackets-max-matches": bracketSide[0].matches.length, + "--brackets-columns": rounds.length, + "--brackets-max-matches": rounds[0].matches.length, }; return (
- {bracketSide.map((round, i) => ( + {rounds.map((round, i) => ( ))} - {bracketSide.map((round, roundI) => { - const nextRound: Unpacked | undefined = - bracketSide[roundI + 1]; + {rounds.map((round, roundI) => { + const nextRound: Unpacked | undefined = + rounds[roundI + 1]; const amountOfMatchesBetweenRoundsEqual = round.matches.length === nextRound?.matches.length; const drawStraightLines = @@ -64,7 +64,7 @@ export function EliminationBracket({ })}
- {roundI !== bracketSide.length - 1 && + {roundI !== rounds.length - 1 && theKindOfLinesToDraw({ amountOfMatchesBetweenRoundsEqual, round, @@ -86,7 +86,7 @@ function theKindOfLinesToDraw({ roundI, amountOfMatchesBetweenRoundsEqual, }: { - round: Unpacked; + round: Unpacked; roundI: number; amountOfMatchesBetweenRoundsEqual: boolean; }): (undefined | "no-line" | "bottom-only" | "top-only")[] { diff --git a/app/components/tournament/EliminationBracketMatch.tsx b/app/components/tournament/EliminationBracketMatch.tsx index 383540e3b..54a22a755 100644 --- a/app/components/tournament/EliminationBracketMatch.tsx +++ b/app/components/tournament/EliminationBracketMatch.tsx @@ -8,7 +8,7 @@ export function EliminationBracketMatch({ ownTeamName, isOver, }: { - match: Unpacked["matches"]>; + match: Unpacked["matches"]>; hidden?: boolean; ownTeamName?: string; isOver: boolean; diff --git a/app/routes/to/$organization.$tournament/bracket.$id.tsx b/app/routes/to/$organization.$tournament/bracket.$id.tsx index bd091b986..45a4d131f 100644 --- a/app/routes/to/$organization.$tournament/bracket.$id.tsx +++ b/app/routes/to/$organization.$tournament/bracket.$id.tsx @@ -131,11 +131,11 @@ export default function BracketTabWrapper() {
round.side === "winners")} ownTeamName={ownTeam?.name} /> round.side === "losers")} ownTeamName={ownTeam?.name} />
diff --git a/app/services/bracket.ts b/app/services/bracket.ts index 79e28f51e..c9c6e0d6f 100644 --- a/app/services/bracket.ts +++ b/app/services/bracket.ts @@ -2,7 +2,6 @@ import type { Stage, TeamOrder } from ".prisma/client"; import type { BracketData } from "server/events"; import invariant from "tiny-invariant"; import { - EliminationBracket, EliminationBracketSide, losersRoundNames, winnersRoundNames, @@ -16,11 +15,14 @@ import { db } from "~/utils/db.server"; export type BracketModified = { id: string; -} & EliminationBracket; + rounds: BracketModifiedSide; +}; + type BracketModifiedSide = { id: string; name: string; stages: { position: number; stage: Stage }[]; + side?: EliminationBracketSide; matches: { id: string; number: number; @@ -53,12 +55,17 @@ export async function bracketById(bracketId: string): Promise { "winners", losersRounds.length === 0 ); - const losers = modifyRounds(losersRounds, "losers", false); + const losers = addLoserTeamSourceInfo({ + winners, + losers: modifyRounds(losersRounds, "losers", false), + }); + const rounds = winners + .map((round) => ({ ...round, side: "winners" as EliminationBracketSide })) + .concat(losers.map((round) => ({ ...round, side: "losers" }))); return { id: bracketId, - winners, - losers: addLoserTeamSourceInfo({ winners, losers }), + rounds, }; }