diff --git a/app/db/tables.ts b/app/db/tables.ts index a55375e40..dc80759f8 100644 --- a/app/db/tables.ts +++ b/app/db/tables.ts @@ -530,6 +530,8 @@ export interface TournamentMatchGameResult { export interface TournamentMatchGameResultParticipant { matchGameResultId: number; userId: number; + // it only started mattering when we added the possibility to join many teams in a tournament, null for legacy events + tournamentTeamId: number | null; } export interface TournamentResult { diff --git a/app/features/tournament-bracket/components/MatchActions.tsx b/app/features/tournament-bracket/components/MatchActions.tsx index 768dbd047..d347a5d87 100644 --- a/app/features/tournament-bracket/components/MatchActions.tsx +++ b/app/features/tournament-bracket/components/MatchActions.tsx @@ -346,7 +346,7 @@ function EditScoreForm({ {points ? ( diff --git a/app/features/tournament-bracket/queries/insertTournamentMatchGameResultParticipant.server.ts b/app/features/tournament-bracket/queries/insertTournamentMatchGameResultParticipant.server.ts index 7a83cd55a..4149fce42 100644 --- a/app/features/tournament-bracket/queries/insertTournamentMatchGameResultParticipant.server.ts +++ b/app/features/tournament-bracket/queries/insertTournamentMatchGameResultParticipant.server.ts @@ -2,14 +2,15 @@ import { sql } from "~/db/sql"; const stm = sql.prepare(/* sql */ ` insert into "TournamentMatchGameResultParticipant" - ("matchGameResultId", "userId") + ("matchGameResultId", "userId", "tournamentTeamId") values - (@matchGameResultId, @userId) + (@matchGameResultId, @userId, @tournamentTeamId) `); export function insertTournamentMatchGameResultParticipant(args: { matchGameResultId: number; userId: number; + tournamentTeamId: number; }) { stm.run(args); } diff --git a/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx b/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx index 48096583c..e61c79699 100644 --- a/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx +++ b/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx @@ -211,10 +211,18 @@ export const action: ActionFunction = async ({ params, request }) => { opponentTwoPoints: data.points?.[1] ?? null, }); - for (const userId of [...teamOneRoster, ...teamTwoRoster]) { + for (const userId of teamOneRoster) { insertTournamentMatchGameResultParticipant({ matchGameResultId: result.id, userId, + tournamentTeamId: match.opponentOne!.id!, + }); + } + for (const userId of teamTwoRoster) { + insertTournamentMatchGameResultParticipant({ + matchGameResultId: result.id, + userId, + tournamentTeamId: match.opponentTwo!.id!, }); } })(); @@ -333,7 +341,8 @@ export const action: ActionFunction = async ({ params, request }) => { ); validate(result, "Result not found"); validate( - data.rosters.length === tournament.minMembersPerTeam * 2, + data.rosters[0].length === tournament.minMembersPerTeam && + data.rosters[1].length === tournament.minMembersPerTeam, "Invalid roster length", ); @@ -377,10 +386,18 @@ export const action: ActionFunction = async ({ params, request }) => { deleteParticipantsByMatchGameResultId(result.id); - for (const userId of data.rosters) { + for (const userId of data.rosters[0]) { insertTournamentMatchGameResultParticipant({ matchGameResultId: result.id, userId, + tournamentTeamId: match.opponentOne!.id!, + }); + } + for (const userId of data.rosters[1]) { + insertTournamentMatchGameResultParticipant({ + matchGameResultId: result.id, + userId, + tournamentTeamId: match.opponentTwo!.id!, }); } })(); diff --git a/app/features/tournament-bracket/tournament-bracket-schemas.server.ts b/app/features/tournament-bracket/tournament-bracket-schemas.server.ts index 839f16ce0..c9376244e 100644 --- a/app/features/tournament-bracket/tournament-bracket-schemas.server.ts +++ b/app/features/tournament-bracket/tournament-bracket-schemas.server.ts @@ -13,7 +13,10 @@ import * as PreparedMaps from "./core/PreparedMaps"; const activeRosterPlayerIds = z.preprocess(safeJSONParse, z.array(id)); -const bothTeamPlayerIds = z.preprocess(safeJSONParse, z.array(id)); +const bothTeamPlayerIds = z.preprocess( + safeJSONParse, + z.tuple([z.array(id), z.array(id)]), +); const reportedMatchPosition = z.preprocess( Number, diff --git a/migrations/075-tournament-result-team-id.js b/migrations/075-tournament-result-team-id.js new file mode 100644 index 000000000..88547aca5 --- /dev/null +++ b/migrations/075-tournament-result-team-id.js @@ -0,0 +1,7 @@ +export function up(db) { + db.transaction(() => { + db.prepare( + /* sql */ `alter table "TournamentMatchGameResultParticipant" add "tournamentTeamId" integer`, + ).run(); + })(); +}