diff --git a/client/src/modules/game_state/states/shared/SharedStateUtil.js b/client/src/modules/game_state/states/shared/SharedStateUtil.js index 3fbdd90..2d35f26 100644 --- a/client/src/modules/game_state/states/shared/SharedStateUtil.js +++ b/client/src/modules/game_state/states/shared/SharedStateUtil.js @@ -113,27 +113,53 @@ export const SharedStateUtil = { }); }, - syncWithGame: (stateBucket, socket, cookie, window) => { + syncWithGame: (socket, cookie, window) => { const splitUrl = window.location.href.split('/game/'); const accessCode = splitUrl[1]; if (/^[a-zA-Z0-9]+$/.test(accessCode) && accessCode.length === globals.ACCESS_CODE_LENGTH) { - socket.emit(globals.SOCKET_EVENTS.IN_GAME_MESSAGE, globals.EVENT_IDS.FETCH_GAME_STATE, accessCode, { personId: cookie }, function (gameState) { - if (gameState === null) { - window.location = '/not-found?reason=' + encodeURIComponent('game-not-found'); - } else { - stateBucket.currentGameState = gameState; - document.querySelector('.spinner-container')?.remove(); - document.querySelector('.spinner-background')?.remove(); - document.getElementById('game-content').innerHTML = HTMLFragments.INITIAL_GAME_DOM; - toast('You are connected.', 'success', true, true, 'short'); - processGameState(stateBucket.currentGameState, cookie, socket, true, true); + socket.timeout(5000).emit( + globals.SOCKET_EVENTS.IN_GAME_MESSAGE, + globals.EVENT_IDS.FETCH_GAME_STATE, + accessCode, + { personId: cookie }, + (err, gameState) => { + if (err) { + SharedStateUtil.retrySync(accessCode, socket, cookie) + } else { + SharedStateUtil.handleGameState(gameState, cookie, socket); + } } - }); + ); } else { window.location = '/not-found?reason=' + encodeURIComponent('invalid-access-code'); } }, + retrySync: (accessCode, socket, cookie) => { + socket.emit( + globals.SOCKET_EVENTS.IN_GAME_MESSAGE, + globals.EVENT_IDS.FETCH_GAME_STATE, + accessCode, + { personId: cookie }, + (gameState) => { + SharedStateUtil.handleGameState(gameState, cookie, socket); + } + ); + }, + + handleGameState: (gameState, cookie, socket) => { + if (gameState === null) { + window.location = '/not-found?reason=' + encodeURIComponent('game-not-found'); + } else { + stateBucket.currentGameState = gameState; + document.querySelector('.spinner-container')?.remove(); + document.querySelector('.spinner-background')?.remove(); + document.getElementById('game-content').innerHTML = HTMLFragments.INITIAL_GAME_DOM; + toast('You are connected.', 'success', true, true, 'short'); + processGameState(stateBucket.currentGameState, cookie, socket, true, true); + } + }, + buildSpectatorList (people) { const list = document.createElement('div'); const spectators = people.filter(p => p.userType === globals.USER_TYPES.SPECTATOR); diff --git a/client/src/modules/page_handlers/gameHandler.js b/client/src/modules/page_handlers/gameHandler.js index 87fa9e6..1d120aa 100644 --- a/client/src/modules/page_handlers/gameHandler.js +++ b/client/src/modules/page_handlers/gameHandler.js @@ -25,7 +25,6 @@ export const gameHandler = async (socket, XHRUtility, window, gameDOM) => { stateBucket.timerWorker = null; } SharedStateUtil.syncWithGame( - stateBucket, socket, UserUtility.validateAnonUserSignature(response.content), window diff --git a/server/api/GamesAPI.js b/server/api/GamesAPI.js index dc71263..6bb3ce5 100644 --- a/server/api/GamesAPI.js +++ b/server/api/GamesAPI.js @@ -36,8 +36,7 @@ router.patch('/restart', (req, res, next) => { }); router.post('/create', gameEndpointLimiter, function (req, res) { - const gameCreationPromise = gameManager.createGame(req.body, false); - gameCreationPromise.then((result) => { + gameManager.createGame(req.body, false).then((result) => { if (result instanceof Error) { res.status(500).send(); } else {