import type { Match as MatchType } from "~/modules/brackets-model";
import type { Bracket as BracketType } from "../../core/Bracket";
import { groupNumberToLetter } from "../../tournament-bracket-utils";
import { Match } from "./Match";
import { PlacementsTable } from "./PlacementsTable";
import { RoundHeader } from "./RoundHeader";
export function RoundRobinBracket({ bracket }: { bracket: BracketType }) {
const groups = getGroups(bracket);
return (
{groups.map(({ groupName, groupId }) => {
const rounds = bracket.data.round.filter((r) => r.group_id === groupId);
const allMatchesFinished = rounds.every((round) => {
const matches = bracket.data.match.filter(
(match) => match.round_id === round.id,
);
return matches.every(
(match) =>
!match.opponent1 ||
!match.opponent2 ||
match.opponent1?.result === "win" ||
match.opponent2?.result === "win",
);
});
return (
{groupName}
{rounds.flatMap((round) => {
const bestOf = round.maps?.count;
const matches = bracket.data.match.filter(
(match) => match.round_id === round.id,
);
const someMatchOngoing = matches.some(
(match) =>
match.opponent1 &&
match.opponent2 &&
match.opponent1.result !== "win" &&
match.opponent2.result !== "win",
);
return (
{matches.map((match) => {
if (!match.opponent1 || !match.opponent2) {
return null;
}
return (
);
})}
);
})}
);
})}
);
}
function getGroups(bracket: BracketType) {
const result: Array<{
groupName: string;
matches: MatchType[];
groupId: number;
}> = [];
for (const group of bracket.data.group) {
const matches = bracket.data.match.filter(
(match) => match.group_id === group.id,
);
result.push({
groupName: `Group ${groupNumberToLetter(group.number)}`,
matches,
groupId: group.id,
});
}
return result;
}