mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-25 04:53:00 -05:00
* Initial * CSS lint * Test CI * Add 1v1, 2v2, and 3v3 Tags (#1771) * Initial * CSS lint * Test CI * Rename step --------- Co-authored-by: xi <104683822+ximk@users.noreply.github.com>
104 lines
2.3 KiB
TypeScript
104 lines
2.3 KiB
TypeScript
import type { z } from "zod";
|
|
import type { TournamentSettings } from "~/db/tables";
|
|
import { isAdmin } from "~/permissions";
|
|
import { BRACKET_NAMES } from "../tournament/tournament-constants";
|
|
import type { newCalendarEventActionSchema } from "./actions/calendar.new.server";
|
|
import { validateFollowUpBrackets } from "./calendar-utils";
|
|
|
|
const usersWithTournamentPerms =
|
|
process.env.TOURNAMENT_PERMS?.split(",").map(Number) ?? [];
|
|
export function canCreateTournament(user?: { id: number }) {
|
|
if (!user) return false;
|
|
|
|
return isAdmin(user) || usersWithTournamentPerms.includes(user.id);
|
|
}
|
|
|
|
export function formValuesToBracketProgression(
|
|
args: z.infer<typeof newCalendarEventActionSchema>,
|
|
) {
|
|
if (!args.format) return null;
|
|
|
|
const result: TournamentSettings["bracketProgression"] = [];
|
|
if (args.format === "DE") {
|
|
result.push({
|
|
name: BRACKET_NAMES.MAIN,
|
|
type: "double_elimination",
|
|
});
|
|
|
|
if (args.withUndergroundBracket) {
|
|
result.push({
|
|
name: BRACKET_NAMES.UNDERGROUND,
|
|
type: "single_elimination",
|
|
sources: [{ bracketIdx: 0, placements: [-1, -2] }],
|
|
});
|
|
}
|
|
}
|
|
|
|
if (args.format === "SWISS") {
|
|
result.push({
|
|
name: BRACKET_NAMES.MAIN,
|
|
type: "swiss",
|
|
});
|
|
}
|
|
|
|
if (args.format === "SE") {
|
|
result.push({
|
|
name: BRACKET_NAMES.MAIN,
|
|
type: "single_elimination",
|
|
});
|
|
}
|
|
|
|
if (
|
|
args.format === "RR_TO_SE" &&
|
|
args.teamsPerGroup &&
|
|
args.followUpBrackets
|
|
) {
|
|
if (
|
|
validateFollowUpBrackets(
|
|
args.followUpBrackets,
|
|
args.format,
|
|
args.teamsPerGroup,
|
|
)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
result.push({
|
|
name: BRACKET_NAMES.GROUPS,
|
|
type: "round_robin",
|
|
});
|
|
|
|
for (const bracket of args.followUpBrackets) {
|
|
result.push({
|
|
name: bracket.name,
|
|
type: "single_elimination",
|
|
sources: [{ bracketIdx: 0, placements: bracket.placements }],
|
|
});
|
|
}
|
|
}
|
|
|
|
if (args.format === "SWISS_TO_SE" && args.followUpBrackets) {
|
|
if (validateFollowUpBrackets(args.followUpBrackets, args.format)) {
|
|
return null;
|
|
}
|
|
|
|
result.push({
|
|
name: BRACKET_NAMES.GROUPS,
|
|
type: "swiss",
|
|
});
|
|
|
|
for (const bracket of args.followUpBrackets) {
|
|
result.push({
|
|
name: bracket.name,
|
|
type: "single_elimination",
|
|
sources: [{ bracketIdx: 0, placements: bracket.placements }],
|
|
});
|
|
}
|
|
}
|
|
|
|
// should not happen
|
|
if (result.length === 0) return null;
|
|
|
|
return result;
|
|
}
|