From cd7e8531aaf2b8f75aa1c719e4886614e9747b12 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 27 Mar 2022 17:43:28 +0300 Subject: [PATCH] Remove unnecessary fields from tournament team --- .../tournament/DuringMatchActions.tsx | 14 +-- app/core/tournament/utils.ts | 39 ------ app/models/Tournament.server.ts | 3 - app/models/TournamentTeam.server.ts | 27 ---- .../$organization.$tournament/manage-team.tsx | 119 +----------------- .../to/$organization.$tournament/register.tsx | 36 +----- .../migration.sql | 15 +++ prisma/schema.prisma | 4 +- prisma/seed/script.ts | 2 - 9 files changed, 28 insertions(+), 231 deletions(-) create mode 100644 prisma/migrations/20220327142556_clean_up_tournament_team/migration.sql diff --git a/app/components/tournament/DuringMatchActions.tsx b/app/components/tournament/DuringMatchActions.tsx index d36c087c3..673154103 100644 --- a/app/components/tournament/DuringMatchActions.tsx +++ b/app/components/tournament/DuringMatchActions.tsx @@ -1,6 +1,5 @@ import { Form, useMatches } from "remix"; import invariant from "tiny-invariant"; -import { resolveHostInfo } from "~/core/tournament/utils"; import type { BracketModified } from "~/services/bracket"; import type { FindTournamentByNameForUrlI } from "~/services/tournament"; import { Unpacked } from "~/utils"; @@ -19,7 +18,7 @@ export function DuringMatchActions({ currentRound: Unpacked; }) { const [, parentRoute] = useMatches(); - const { teams, seeds } = parentRoute.data as FindTournamentByNameForUrlI; + const { teams } = parentRoute.data as FindTournamentByNameForUrlI; const opponentTeam = teams.find( (team) => @@ -29,11 +28,12 @@ export function DuringMatchActions({ ); invariant(opponentTeam, "opponentTeam is undefined"); - const { weHost, friendCodeToAdd, roomPass } = resolveHostInfo({ - ourTeam: ownTeam, - theirTeam: opponentTeam, - seeds, - }); + // TODO: + const { weHost, friendCodeToAdd, roomPass } = { + weHost: true, + friendCodeToAdd: "1234-1234-1234", + roomPass: "1324", + }; const currentPosition = currentMatch.score?.reduce((acc, cur) => acc + cur, 1) ?? 1; diff --git a/app/core/tournament/utils.ts b/app/core/tournament/utils.ts index fa9fa93d7..f5eaf582a 100644 --- a/app/core/tournament/utils.ts +++ b/app/core/tournament/utils.ts @@ -1,7 +1,5 @@ import invariant from "tiny-invariant"; import { ROOM_PASS_LENGTH } from "~/constants"; -import type { FindTournamentByNameForUrlI } from "~/services/tournament"; -import { Unpacked } from "~/utils"; export function checkInHasStarted(checkInStartTime: string) { return new Date(checkInStartTime) < new Date(); @@ -60,43 +58,6 @@ export function matchIsOver( return upperTeamScore > half || lowerTeamScore > half; } -export function resolveHostInfo({ - ourTeam, - theirTeam, - seeds, -}: { - ourTeam: Unpacked; - theirTeam: Unpacked; - seeds: string[]; -}): { friendCodeToAdd: string; roomPass: string; weHost: boolean } { - const seededOrder = [ourTeam, theirTeam].sort(sortTeamsBySeed(seeds)); - const weAreHigherSeed = seededOrder[0].id === ourTeam.id; - - let weHost = false; - if (ourTeam.canHost && !theirTeam.canHost) weHost = true; - if (!ourTeam.canHost && !theirTeam.canHost && weAreHigherSeed) weHost = true; - const teamToHost = weHost ? ourTeam : theirTeam; - - return { - weHost, - roomPass: teamToHost.roomPass ?? idToRoomPass(teamToHost.id), - friendCodeToAdd: teamToHost.friendCode, - }; -} - -function idToRoomPass(id: string) { - let pass = ""; - for (const letter of id) { - if (pass.length === ROOM_PASS_LENGTH) break; - const maybeNumber = Number(letter); - if (Number.isNaN(maybeNumber)) continue; - - pass += letter; - } - - return pass; -} - export const tournamentURL = ({ organizerNameForUrl, tournamentNameForUrl, diff --git a/app/models/Tournament.server.ts b/app/models/Tournament.server.ts index 2e5bb9476..7aba0bcca 100644 --- a/app/models/Tournament.server.ts +++ b/app/models/Tournament.server.ts @@ -71,9 +71,6 @@ export async function findByNameForUrl({ id: true, name: true, createdAt: true, - friendCode: true, - roomPass: true, - canHost: true, inviteCode: withInviteCodes, members: { select: { diff --git a/app/models/TournamentTeam.server.ts b/app/models/TournamentTeam.server.ts index 5d6bae5aa..dcbf0bb56 100644 --- a/app/models/TournamentTeam.server.ts +++ b/app/models/TournamentTeam.server.ts @@ -6,18 +6,15 @@ export function create({ userId, teamName, tournamentId, - friendCode, }: { userId: string; teamName: string; tournamentId: string; - friendCode: string; }) { return db.tournamentTeam.create({ data: { name: teamName.trim(), tournamentId, - friendCode, members: { create: { memberId: userId, @@ -60,27 +57,3 @@ export function checkOut(id: string) { }, }); } - -export type EditTeam = Prisma.PromiseReturnType; -export function editTeam({ - id, - friendCode, - roomPass, - canHost, -}: { - id: string; - friendCode: string; - roomPass: string | null; - canHost: boolean; -}) { - return db.tournamentTeam.update({ - where: { - id, - }, - data: { - friendCode, - roomPass, - canHost, - }, - }); -} diff --git a/app/routes/to/$organization.$tournament/manage-team.tsx b/app/routes/to/$organization.$tournament/manage-team.tsx index 4636144bd..444da15f8 100644 --- a/app/routes/to/$organization.$tournament/manage-team.tsx +++ b/app/routes/to/$organization.$tournament/manage-team.tsx @@ -1,7 +1,6 @@ import { Prisma } from ".prisma/client"; import { ActionFunction, - Form, json, LinksFunction, LoaderFunction, @@ -14,18 +13,9 @@ import { z } from "zod"; import { AddPlayers } from "~/components/AddPlayers"; import { Alert } from "~/components/Alert"; import { Catcher } from "~/components/Catcher"; -import { FormInfoText } from "~/components/FormInfoText"; -import { Label } from "~/components/Label"; -import { SubmitButton } from "~/components/SubmitButton"; import { TeamRoster } from "~/components/tournament/TeamRoster"; import { TOURNAMENT_TEAM_ROSTER_MAX_SIZE } from "~/constants"; -import { - friendCodeRegExp, - friendCodeRegExpString, - roompassRegExp, - roompassRegExpString, - tournamentURL, -} from "~/core/tournament/utils"; +import { tournamentURL } from "~/core/tournament/utils"; import { isCaptainOfTheTeam, teamHasNotCheckedIn, @@ -54,18 +44,6 @@ const actionSchema = z.union([ userId: z.string().uuid(), teamId: z.string().uuid(), }), - z.object({ - _action: z.literal("EDIT_TEAM"), - teamId: z.string().uuid(), - friendCode: z.string().regex(friendCodeRegExp), - canHost: z - .enum(["yes", "no"]) - .transform((val) => (val === "yes" ? true : false)), - roomPass: z.preprocess( - (val) => val || null, - z.string().regex(roompassRegExp).nullable() - ), - }), ]); type ActionData = { @@ -135,24 +113,6 @@ export const action: ActionFunction = async ({ return { ok: "DELETE_PLAYER" }; } - case "EDIT_TEAM": { - const tournamentTeam = await TournamentTeam.findById(data.teamId); - - validate(tournamentTeam, "Invalid team id"); - validate( - isCaptainOfTheTeam(user, tournamentTeam), - "Not captain of the team" - ); - - await TournamentTeam.editTeam({ - id: data.teamId, - canHost: data.canHost, - friendCode: data.friendCode, - roomPass: data.roomPass, - }); - - return { ok: "EDIT_TEAM" }; - } default: { const exhaustive: never = data; throw new Response(`Unknown action: ${JSON.stringify(exhaustive)}`, { @@ -217,83 +177,6 @@ export default function ManageTeamPage() {
-
- - -
- Edit team info - - - - - - - If blank the password will be randomly generated whenever you host - - - -
- - -
- -
- - -
- - You might still have to host if both teams prefer not to - - - Save - -
-
{ownTeam.members.length < TOURNAMENT_TEAM_ROSTER_MAX_SIZE && ( { return [{ rel: "stylesheet", href: styles }]; @@ -39,7 +34,6 @@ type ActionData = { fieldErrors?: { teamName?: string }; fields?: { teamName: string; - friendCode: string; }; }; @@ -54,10 +48,6 @@ export const action: ActionFunction = async ({ typeof data.tournamentId === "string", "Invalid type for tournament id" ); - invariant( - typeof data.friendCode === "string", - "Invalid type for friend code" - ); if ( data.teamName.length < TEAM_NAME_MIN_LENGTH || @@ -69,10 +59,6 @@ export const action: ActionFunction = async ({ ); } - if (!friendCodeRegExp.test(data.friendCode)) { - return new Response("Invalid friend code", { status: 400 }); - } - const user = requireUser(context); // TODO: validate can register for tournament i.e. reg is open @@ -81,7 +67,6 @@ export const action: ActionFunction = async ({ await createTournamentTeam({ teamName: data.teamName, tournamentId: data.tournamentId, - friendCode: data.friendCode, userId: user.id, }); } catch (e) { @@ -89,7 +74,7 @@ export const action: ActionFunction = async ({ if (e.code === "P2002" && e.message.includes("`name`")) { return { fieldErrors: { teamName: "Team name already taken." }, - fields: { teamName: data.teamName, friendCode: data.friendCode }, + fields: { teamName: data.teamName }, }; } } @@ -149,19 +134,6 @@ export default function RegisterPage() { data-cy="team-name-input" /> - - - Friend code can be changed later