Advance losers on report if BYE

This commit is contained in:
Kalle (Sendou)
2022-01-09 23:33:54 +02:00
parent 340c640c39
commit 1efa499461
5 changed files with 80 additions and 38 deletions

View File

@@ -89,8 +89,8 @@ export function DuringMatchActions({
)}
</div>
<div className="tournament-bracket__infos">
{roundInfos.map((info) => (
<div>{info}</div>
{roundInfos.map((info, i) => (
<div key={i}>{info}</div>
))}
</div>
<ActionSectionWrapper>

View File

@@ -56,11 +56,14 @@ export function eliminationBracket(
!(upperTeam === "BYE" && lowerTeam === "BYE"),
"Unexpected both teams in the first round are BYEs"
);
const firstRoundMatch = createMatch({
upperTeam,
lowerTeam,
side: "winners",
});
const firstRoundMatch = createMatch(
{
upperTeam,
lowerTeam,
side: "winners",
},
upperTeam === "BYE" || lowerTeam === "BYE"
);
matchesWQueue.push(firstRoundMatch);
matchesLQueue.push(firstRoundMatch);
@@ -123,9 +126,13 @@ export function eliminationBracket(
invariant(match1, "Unexpected no match1 in losers bracket");
invariant(match2, "Unexpected no match2 in losers bracket");
const losersMatch = createMatch({
side: "losers",
});
const losersMatch = createMatch(
{
side: "losers",
},
// If the first match already has a bye then this losers match will also be skipped
match1.lowerTeam === "BYE" || match1.upperTeam === "BYE"
);
match1[
match1.side === "winners"
@@ -169,11 +176,11 @@ export function eliminationBracket(
return bracket;
// TODO: also need to make match number 0 if loser bye
function createMatch(args: Omit<Match, "id" | "number">): Match {
const number = [args.lowerTeam, args.upperTeam].includes("BYE")
? 0
: matchNumber++;
function createMatch(
args: Omit<Match, "id" | "number">,
willBeSkipped?: boolean
): Match {
const number = willBeSkipped ? 0 : matchNumber++;
return {
id: uuidv4(),
number,

View File

@@ -672,11 +672,14 @@ function resolveNewOrder({
newMatchId,
}: {
bracket: NonNullable<TournamentBracket.FindById>;
oldMatch: NonNullable<TournamentMatch.FindById>;
oldMatch: { id: string };
newMatchId: string;
}): TeamOrder {
const allMatches = bracket.rounds.flat().flatMap((round) => {
return round.matches;
return round.matches.map((match) => ({
...match,
isWinners: round.position > 0,
}));
});
const newMatch = allMatches.find((m) => m.id === newMatchId);
@@ -688,7 +691,19 @@ function resolveNewOrder({
newMatchId
)
)
.sort((a, b) => a.position - b.position);
.sort((a, b) => {
// first let's put teams from winners above teams from losers
// this might come to question if losers match is bye (so position 0)
// and would thus be above the winners bracket match if not for
// this condition
if (a.isWinners !== b.isWinners) {
return Number(b.isWinners) - Number(a.isWinners);
}
// otherwise if match number is smaller it
// should mean the match is above the other match.
return a.position - b.position;
});
invariant(
matchesThatLeadToNewMatch.length === 2,
`matchesThatLeadToNewMatch length was unexpected: ${matchesThatLeadToNewMatch.length}`
@@ -698,10 +713,6 @@ function resolveNewOrder({
"oldMatch not among matchesThatLeadToNewMatch"
);
// if match number is smaller it should mean the match is above
// the other match. thanks to sorting above 0 index should have
// the smaller match number. Winner's bracket match should
// always have the smaller number compared to loser's bracket match
if (matchesThatLeadToNewMatch[0].id === oldMatch.id) return "UPPER";
return "LOWER";
}
@@ -741,9 +752,29 @@ function newParticipantsForMatches({
}),
teamId: loserTeam.teamId,
});
}
// if match number === 0 let's advance
const losersMatch = bracket.rounds
.flatMap((round) => round.matches)
.find(({ id }) => id === match.loserDestinationMatchId);
invariant(losersMatch, "losersMatch undefined");
// if the match will have a BYE then we need to generate one more participant
if (losersMatch.position === 0) {
invariant(
losersMatch.winnerDestinationMatchId,
"losersMatch.winnerDestinationMatchId undefined"
);
result.push({
matchId: losersMatch.winnerDestinationMatchId,
order: resolveNewOrder({
bracket,
oldMatch: losersMatch,
newMatchId: losersMatch.winnerDestinationMatchId,
}),
teamId: loserTeam.teamId,
});
}
}
return result;
}

View File

@@ -6,15 +6,15 @@ import { LoggedInUserSchema } from "~/validators/user";
export const useUser = () => {
const [root] = useMatches();
const { user } = LoggedInUserSchema.parse(root.data);
return user;
const parsed = LoggedInUserSchema.parse(root.data);
return parsed?.user;
};
export const useBaseURL = () => {
const [root] = useMatches();
const { baseURL } = z.object({ baseURL: z.string() }).parse(root.data);
return baseURL;
const parsed = z.object({ baseURL: z.string() }).nullish().parse(root.data);
return parsed?.baseURL;
};
// TODO: fix causes memory leak

View File

@@ -1,12 +1,16 @@
import { z } from "zod";
export type LoggedInUser = z.infer<typeof LoggedInUserSchema>["user"];
export const LoggedInUserSchema = z.object({
user: z
.object({
id: z.string(),
discordId: z.string(),
discordAvatar: z.string().nullable(),
})
.nullish(),
});
export type LoggedInUser = NonNullable<
z.infer<typeof LoggedInUserSchema>
>["user"];
export const LoggedInUserSchema = z
.object({
user: z
.object({
id: z.string(),
discordId: z.string(),
discordAvatar: z.string().nullable(),
})
.nullish(),
})
.nullish();