diff --git a/frontend/src/pages/pong/socket.ts b/frontend/src/pages/pong/socket.ts index 9c419fd..7a530aa 100644 --- a/frontend/src/pages/pong/socket.ts +++ b/frontend/src/pages/pong/socket.ts @@ -40,7 +40,6 @@ export type TourInfo = { export enum JoinRes { yes = 'yes', no = 'dont ever talk to me or my kid ever again', - dev = 'yaaaaaaaaaaaaaaaaaaaaaaaa', }; export interface ClientToServer { diff --git a/src/pong/src/game.ts b/src/pong/src/game.ts index c3c81ee..d21ead9 100644 --- a/src/pong/src/game.ts +++ b/src/pong/src/game.ts @@ -101,6 +101,7 @@ const RIGHT: number = 1; export class Pong { public gameUpdate: NodeJS.Timeout | null = null; + public userOnPage : [boolean, boolean] = [false, false]; public static readonly CONCEDED_TIMEOUT: number = 1500; diff --git a/src/pong/src/routes/createPausedGame.ts b/src/pong/src/routes/createPausedGame.ts index efe0b40..e59a875 100644 --- a/src/pong/src/routes/createPausedGame.ts +++ b/src/pong/src/routes/createPausedGame.ts @@ -32,9 +32,9 @@ const route: FastifyPluginAsync = async (fastify): Promise => { }, async function(req, res) { const resp = State.newPausedGame(req.body.user1 as UserId, req.body.user2 as UserId); - if (isNullish(resp)) { return (res.makeResponse(404, 'failure', 'createPausedGame.generic.fail')); } - // else - return (res.makeResponse(200, 'success', 'createPausedGame.success', { gameId: resp })); + + if (isNullish(resp)) { return (res.makeResponse(404, 'failure', 'createPausedGame.userUknown.fail')); } + return (res.makeResponse(200, 'success', 'createPausedGame.success', { gameId :resp })); }, ); }; diff --git a/src/pong/src/routes/startPausedGame.ts b/src/pong/src/routes/startPausedGame.ts deleted file mode 100644 index ef2f1c2..0000000 --- a/src/pong/src/routes/startPausedGame.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { MakeStaticResponse, typeResponse } from '@shared/utils'; -import { FastifyPluginAsync } from 'fastify'; -import Type, { Static } from 'typebox'; -import { State } from '../state'; -import { PongGameId } from '@shared/database/mixin/pong'; - -const startPausedGameParam = Type.Object({ - gameId: Type.String({ description: '\'id\' | ' }), -}); - -type startPausedGameParam = Static; - -const startPausedGameResponse = { - '200': typeResponse('success', 'startPausedGame.success', {}), - '404': typeResponse('failure', 'startPausedGame.no_such_game'), -}; - -type startPausedGameResponse = MakeStaticResponse; - -const route: FastifyPluginAsync = async (fastify): Promise => { - fastify.post<{ Body: startPausedGameParam }>( - '/startPausedGame', - { - schema: { - body: startPausedGameParam, - response: startPausedGameResponse, - operationId: 'pongstartPauseGame', - }, - }, - async function(req, res) { - const resp = State.startPausedGame(req.body.gameId as PongGameId); - - if (resp !== true) { return (res.makeResponse(404, 'failure', 'startPausedGame.generic.fail')); } - // else - return (res.makeResponse(200, 'success', 'startPausedGame.success')); - }, - ); -}; -export default route; \ No newline at end of file diff --git a/src/pong/src/socket.ts b/src/pong/src/socket.ts index f9dcd4f..6c787ee 100644 --- a/src/pong/src/socket.ts +++ b/src/pong/src/socket.ts @@ -40,7 +40,6 @@ export type TourInfo = { export enum JoinRes { yes = 'yes', no = 'dont ever talk to me or my kid ever again', - dev = 'yaaaaaaaaaaaaaaaaaaaaaaaa', }; export interface ClientToServer { diff --git a/src/pong/src/state.ts b/src/pong/src/state.ts index 00b961a..d03a7ec 100644 --- a/src/pong/src/state.ts +++ b/src/pong/src/state.ts @@ -223,12 +223,9 @@ class StateI { } public newPausedGame(suid1: string, suid2: string): GameId | undefined { - // if ( - // !this.users.has(suid1 as UserId) || - // !this.users.has(suid2 as UserId) - // ) { - // return undefined; - // } + if (!this.fastify.db.getUser(suid1) || !this.fastify.db.getUser(suid2)) { + return undefined; + } const uid1: UserId = suid1 as UserId; const uid2: UserId = suid2 as UserId; const g = new Pong(uid1, uid2); @@ -236,19 +233,10 @@ class StateI { const gameId = newUUID() as unknown as GameId; this.games.set(gameId, g); + this.fastify.log.info('new paused game \'' + gameId + '\''); return gameId; } - public startPausedGame(g_id: PongGameId): boolean { - let game: Pong | undefined; - - if ((game = this.games.get(g_id)) === undefined) { - return false; - } - this.initGame(game, g_id, game.userLeft, game.userRight); - return true; - } - private tournamentIntervalFunc() { const broadcastTourEnding = (msg: string) => { this.users.forEach((u) => { @@ -387,14 +375,17 @@ class StateI { private tryJoinGame(g_id : string, sock : SSocket) : JoinRes { const game_id : PongGameId = g_id as PongGameId; - let game : Pong; - if (this.games.has(game_id) === false) + if (this.games.has(game_id) === false) { return (JoinRes.no); } + const game : Pong = this.games.get(game_id)!; + if (game.local || (game.userLeft !== sock.authUser.id && game.userRight !== sock.authUser.id)) { return (JoinRes.no); - game = this.games.get(game_id)!; - if (game.local || game.userLeft !== sock.authUser.id || game.userRight !== sock.authUser.id) - return (JoinRes.no); - return (JoinRes.dev); + } + game.userOnPage[game.userLeft === sock.authUser.id ? 0 : 1] = true; + if (game.userOnPage[0] === game.userOnPage[1]) { + this.initGame(game, game_id, game.userLeft, game.userRight); + } + return (JoinRes.yes); } public registerUser(socket: SSocket): void { @@ -586,4 +577,4 @@ export let State: StateI = undefined as unknown as StateI; export function newState(f: FastifyInstance) { State = new StateI(f); -} +} \ No newline at end of file