diff --git a/convex/_model/fowV4/calculateFowV4MatchResultScore.ts b/convex/_model/fowV4/calculateFowV4MatchResultScore.ts index 21d695ba..74c2517c 100644 --- a/convex/_model/fowV4/calculateFowV4MatchResultScore.ts +++ b/convex/_model/fowV4/calculateFowV4MatchResultScore.ts @@ -1,5 +1,4 @@ import { Doc } from '../../_generated/dataModel'; -import { DeepMatchResult } from '../matchResults'; /** * Calculate the Victory Points (i.e. score) for a given match result. @@ -10,7 +9,7 @@ import { DeepMatchResult } from '../matchResults'; * @param matchResult - The match result to score * @returns - A tuple with the scores for player 0 and 1 respectively */ -export const calculateFowV4MatchResultScore = (matchResult: Doc<'matchResults'> | DeepMatchResult): [number, number] => { +export const calculateFowV4MatchResultScore = (matchResult: Doc<'matchResults'>): [number, number] => { // TODO: Add some guards in case matchResult is not FowV4 diff --git a/convex/_model/matchResults/_helpers/deepenMatchResult.ts b/convex/_model/matchResults/_helpers/deepenMatchResult.ts index 55cd026f..b9d7625f 100644 --- a/convex/_model/matchResults/_helpers/deepenMatchResult.ts +++ b/convex/_model/matchResults/_helpers/deepenMatchResult.ts @@ -1,5 +1,6 @@ import { Doc } from '../../../_generated/dataModel'; import { QueryCtx } from '../../../_generated/server'; +import { calculateFowV4MatchResultScore } from '../../fowV4/calculateFowV4MatchResultScore'; import { getMission } from '../../fowV4/getMission'; import { getUser } from '../../users/queries/getUser'; import { checkMatchResultBattlePlanVisibility } from './checkMatchResultBattlePlanVisibility'; @@ -38,6 +39,9 @@ export const deepenMatchResult = async ( const mission = getMission(matchResult.details.missionId); const battlePlansVisible = await checkMatchResultBattlePlanVisibility(ctx, matchResult); + // TODO: This is FowV4 specific, needs to be made generic! + const [player0Score, player1Score] = calculateFowV4MatchResultScore(matchResult); + return { ...matchResult, ...(player0User ? { player0User } : {}), @@ -47,6 +51,8 @@ export const deepenMatchResult = async ( player0BattlePlan: battlePlansVisible ? matchResult.details.player0BattlePlan : undefined, player1BattlePlan: battlePlansVisible ? matchResult.details.player1BattlePlan : undefined, missionName: mission?.displayName, + player0Score, + player1Score, }, likedByUserIds: likes.map((like) => like.userId), commentCount: comments.length, diff --git a/convex/_model/tournamentPairings/_helpers/deepenTournamentPairing.ts b/convex/_model/tournamentPairings/_helpers/deepenTournamentPairing.ts index 1ed4b351..00650d54 100644 --- a/convex/_model/tournamentPairings/_helpers/deepenTournamentPairing.ts +++ b/convex/_model/tournamentPairings/_helpers/deepenTournamentPairing.ts @@ -14,7 +14,7 @@ import { getTournamentShallow } from '../../tournaments'; * This method's return type is, by nature, the definition of a deep TournamentPairing. * * @param ctx - Convex query context - * @param tournament - Raw TournamentPairing document + * @param tournamentPairing - Raw TournamentPairing document * @returns A deep TournamentPairing */ export const deepenTournamentPairing = async ( diff --git a/src/components/FowV4MatchResultDetails/FowV4MatchResultDetails.types.ts b/src/components/FowV4MatchResultDetails/FowV4MatchResultDetails.types.ts index 15c3b11c..4ecf00f2 100644 --- a/src/components/FowV4MatchResultDetails/FowV4MatchResultDetails.types.ts +++ b/src/components/FowV4MatchResultDetails/FowV4MatchResultDetails.types.ts @@ -3,7 +3,7 @@ import { MatchResult, User } from '~/api'; export type FowV4MatchResultDetailsData = Pick & { player0User?: User; player1User?: User; - details: Omit & { + details: Omit & { missionName?: string; } }; diff --git a/src/components/FowV4MatchResultForm/FowV4MatchResultForm.tsx b/src/components/FowV4MatchResultForm/FowV4MatchResultForm.tsx index 4c872870..04a0de8f 100644 --- a/src/components/FowV4MatchResultForm/FowV4MatchResultForm.tsx +++ b/src/components/FowV4MatchResultForm/FowV4MatchResultForm.tsx @@ -3,7 +3,7 @@ import { zodResolver } from '@hookform/resolvers/zod'; import clsx from 'clsx'; import { - MatchResultId, + MatchResult, TournamentPairingId, UserId, } from '~/api'; @@ -16,11 +16,7 @@ import { SelectValue } from '~/components/generic/InputSelect/InputSelect.types' import { Label } from '~/components/generic/Label'; import { Separator } from '~/components/generic/Separator'; import { useAsyncState } from '~/hooks/useAsyncState'; -import { - useCreateMatchResult, - useGetMatchResult, - useUpdateMatchResult, -} from '~/services/matchResults'; +import { useCreateMatchResult, useUpdateMatchResult } from '~/services/matchResults'; import { useGetActiveTournamentPairingsByUser } from '~/services/tournamentPairings'; import { getTournamentPairingDisplayName } from '~/utils/common/getTournamentPairingDisplayName'; import { CommonFields } from './components/CommonFields'; @@ -40,7 +36,7 @@ const confirmMatchResultDialogId = 'confirm-match-result'; export interface FowV4MatchResultFormProps { id: string; className?: string; - matchResultId?: MatchResultId; + matchResult?: MatchResult; tournamentPairingId?: TournamentPairingId; onSuccess?: () => void; } @@ -48,17 +44,12 @@ export interface FowV4MatchResultFormProps { export const FowV4MatchResultForm = ({ id, className, - matchResultId, + matchResult, tournamentPairingId: forcedTournamentPairingId, onSuccess, }: FowV4MatchResultFormProps): JSX.Element => { const user = useAuth(); - const { - data: matchResult, - loading: matchResultLoading, - } = useGetMatchResult(matchResultId ? { id: matchResultId } : 'skip'); - const [ tournamentPairingId, setTournamentPairingId, @@ -89,9 +80,10 @@ export const FowV4MatchResultForm = ({ const form = useForm({ resolver: zodResolver(fowV4MatchResultFormSchema), - defaultValues, - // React-Hook-Form is stupid and doesn't allow applying a partial record to the form values - values: { ...matchResult as FowV4MatchResultFormData }, + defaultValues: { + ...defaultValues, + ...(matchResult ? fowV4MatchResultFormSchema.parse(matchResult) : {}), + }, mode: 'onSubmit', }); @@ -143,13 +135,13 @@ export const FowV4MatchResultForm = ({ const disableSubmit = createMatchResultLoading || updateMatchResultLoading; - if (tournamentPairingsLoading || matchResultLoading) { + if (tournamentPairingsLoading) { return
Loading...
; } return (
- {!matchResultId && ( + {!matchResult && !forcedTournamentPairingId && ( <>