mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 07:47:50 +01:00
simplify connection logic
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user