add retry for fetching game state

This commit is contained in:
AlecM33
2023-01-31 23:17:35 -05:00
parent bc986005c8
commit a99d19614c
3 changed files with 39 additions and 15 deletions

View File

@@ -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);

View File

@@ -25,7 +25,6 @@ export const gameHandler = async (socket, XHRUtility, window, gameDOM) => {
stateBucket.timerWorker = null;
}
SharedStateUtil.syncWithGame(
stateBucket,
socket,
UserUtility.validateAnonUserSignature(response.content),
window

View File

@@ -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 {