From b932a06fe0c400eedb3f761ded705fac101b1a36 Mon Sep 17 00:00:00 2001 From: AlecM33 Date: Thu, 30 Dec 2021 02:20:50 -0500 Subject: [PATCH] simplify connection logic --- server/modules/GameManager.js | 27 +++----------- spec/unit/server/modules/GameManager_Spec.js | 39 -------------------- 2 files changed, 5 insertions(+), 61 deletions(-) diff --git a/server/modules/GameManager.js b/server/modules/GameManager.js index a89b887..f80d2e6 100644 --- a/server/modules/GameManager.js +++ b/server/modules/GameManager.js @@ -258,12 +258,7 @@ class GameManager { } } - /* Since clients are anonymous, we have to rely to some extent on a cookie to identify them. Socket ids - are unique to a client, but they are re-generated if a client disconnects and then reconnects. - Thus, to have the most resilient identification i.e. to let them refresh, navigate away and come back, - get disconnected and reconnect, etc. we should have a combination of the socket id and the cookie. - My philosophy is to make it exceptionally difficult for clients to _accidentally_ break their experience. - */ + handleRequestForGameState = (namespace, logger, gameRunner, accessCode, personCookie, ackFn, socket) => { const game = gameRunner.activeGames[accessCode]; if (game) { @@ -279,15 +274,10 @@ class GameManager { logger.trace("matching person found with an established connection to the room: " + matchingPerson.name); ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson, gameRunner, socket, logger)); } else { - if (!this.roomContainsSocketOfMatchingPerson(namespace, matchingPerson, logger, accessCode)) { - logger.trace("matching person found with a new connection to the room: " + matchingPerson.name); - socket.join(accessCode); - matchingPerson.socketId = socket.id; - ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson, gameRunner, socket, logger)); - } else { - logger.trace('this person is already associated with a socket connection'); - this.handleRequestFromNonMatchingPerson(game, socket, gameRunner, ackFn, logger); - } + logger.trace("matching person found with a new connection to the room: " + matchingPerson.name); + socket.join(accessCode); + matchingPerson.socketId = socket.id; + ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson, gameRunner, socket, logger)); } } else { this.handleRequestFromNonMatchingPerson(game, socket, gameRunner, ackFn, logger); @@ -334,13 +324,6 @@ class GameManager { } } - // starting with socket.io 4.x, the rooms object is a Map, and its values a Set. - roomContainsSocketOfMatchingPerson = (namespace, matchingPerson, logger, accessCode) => { - return namespace.adapter - && namespace.adapter.rooms.get(accessCode) - && namespace.adapter.rooms.get(accessCode).has(matchingPerson.socketId); - } - } function getRandomInt (max) { diff --git a/spec/unit/server/modules/GameManager_Spec.js b/spec/unit/server/modules/GameManager_Spec.js index d0a8b4a..28bd36e 100644 --- a/spec/unit/server/modules/GameManager_Spec.js +++ b/spec/unit/server/modules/GameManager_Spec.js @@ -185,7 +185,6 @@ describe('GameManager', function () { let socket = { id: "socket_222222", join: () => {}}; spyOn(socket, 'join'); spyOn(GameStateCurator, 'getGameStateFromPerspectiveOfPerson'); - spyOn(gameManager, 'roomContainsSocketOfMatchingPerson').and.callFake(() => { return false }); player.socketId = "socket_111111"; let gameRunner = { activeGames: { @@ -215,43 +214,5 @@ describe('GameManager', function () { expect(player.socketId).toEqual(socket.id); expect(socket.join).toHaveBeenCalled(); }); - - it('should seek to re-assign a socket connection should two connections match the same person', () => { - let player = new Person("1", "123", "Joe", USER_TYPES.PLAYER); - let socket = { id: "socket_222222", join: () => {}}; - let ackFn = () => {}; - spyOn(socket, 'join'); - spyOn(GameStateCurator, 'getGameStateFromPerspectiveOfPerson'); - spyOn(gameManager, 'handleRequestFromNonMatchingPerson'); - spyOn(gameManager, 'roomContainsSocketOfMatchingPerson').and.callFake(() => { return true }); - player.socketId = "socket_111111"; - let gameRunner = { - activeGames: { - "abc": new Game( - "abc", - globals.STATUS.IN_PROGRESS, - [ player ], - [], - false, - new Person("2", "456", "Jane", USER_TYPES.MODERATOR) - ) - } - } - spyOn(namespace.in(), 'emit'); - gameManager.handleRequestForGameState( - namespace, - logger, - gameRunner, - "abc", - "123", - ackFn, - socket - ); - - expect(GameStateCurator.getGameStateFromPerspectiveOfPerson).not.toHaveBeenCalled(); - expect(gameManager.handleRequestFromNonMatchingPerson).toHaveBeenCalledWith(gameRunner.activeGames["abc"], socket, gameRunner, ackFn, logger) - expect(player.socketId).not.toEqual(socket.id); - expect(socket.join).not.toHaveBeenCalled(); - }); }); });