Skip to content

Commit

Permalink
final updates for this branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Techno11 committed Sep 15, 2024
1 parent 92e2bdd commit e5e0d05
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 9 additions & 2 deletions back-end/api/src/controllers/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@toa-lib/models';
import { NextFunction, Response, Request, Router } from 'express';
import { validateBodyZ } from '../middleware/BodyValidator.js';
import { DataNotFoundError } from '../util/Errors.js';
import { DataNotFoundError, InvalidDataError } from '../util/Errors.js';
import { join } from 'path';
import { writeFile } from 'fs/promises';
import {
Expand Down Expand Up @@ -159,7 +159,6 @@ router.get(

res.send(match);
} catch (e) {
console.log(e);
return next(e);
}
}
Expand Down Expand Up @@ -245,6 +244,14 @@ router.patch(
const funcs = getFunctionsBySeasonKey(
eventKey.split('-')[0].toLowerCase()
);
// Ensure they are actually updating what they say they're updating
if (
req.body.eventKey !== eventKey ||
req.body.tournamentKey !== tournamentKey ||
String(req.body.id) !== id
) {
return next(InvalidDataError);
}
const data = funcs?.detailsToJson
? funcs.detailsToJson(req.body)
: req.body;
Expand Down
5 changes: 5 additions & 0 deletions back-end/api/src/util/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export const DataNotFoundError: ApiError = {
message: 'Data requested was not found'
};

export const InvalidDataError: ApiError = {
code: 400,
message: 'The body sent does not match the route parameters. Please ensure the body matches the route.'
};

export const RouteNotFound: ApiError = {
code: 404,
message: 'Route not found.'
Expand Down
27 changes: 24 additions & 3 deletions front-end/src/apps/scorekeeper/hooks/use-prestart.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { useRecoilCallback } from 'recoil';
import { useMatchControl } from './use-match-control';
import { MatchSocketEvent, MatchState } from '@toa-lib/models';
import {
getDefaultMatchDetailsBySeasonKey,
getFunctionsBySeasonKey,
MatchSocketEvent,
MatchState
} from '@toa-lib/models';
import { matchOccurringAtom, socketConnectedAtom } from 'src/stores/recoil';
import { patchMatch, patchMatchParticipants } from 'src/api/use-match-data';
import { DateTime } from 'luxon';
import { once, sendPrestart, sendUpdate } from 'src/api/use-socket';
import { useSeasonFieldControl } from 'src/hooks/use-season-components';
import { c } from 'vite/dist/node/types.d-aGj9QkWt';

export const usePrestartCallback = () => {
const { canPrestart, setState } = useMatchControl();
const fieldControl = useSeasonFieldControl();
return useRecoilCallback(
({ snapshot }) =>
({ snapshot, set }) =>
async () => {
const match = await snapshot.getPromise(matchOccurringAtom);
const socketConnected = await snapshot.getPromise(socketConnectedAtom);
Expand All @@ -36,9 +42,24 @@ export const usePrestartCallback = () => {
{ eventKey, tournamentKey, id },
match.participants
);
let currentMatch = match;
if (!match.details) {
currentMatch = {
...match,
details: {
...getDefaultMatchDetailsBySeasonKey(
match.eventKey.split('-')[0].toLowerCase()
),
tournamentKey: match.tournamentKey,
eventKey: match.eventKey,
id: match.id
}
};
set(matchOccurringAtom, currentMatch);
}
fieldControl?.prestartField?.();
// Once we recieve the prestart response, immediately send update to load socket with match
once(MatchSocketEvent.PRESTART, () => sendUpdate(match));
once(MatchSocketEvent.PRESTART, () => sendUpdate(currentMatch));
// Send prestart to server
sendPrestart({ eventKey, tournamentKey, id });
setState(MatchState.PRESTART_COMPLETE);
Expand Down

0 comments on commit e5e0d05

Please sign in to comment.