From 3f96b971940825618f6fed4d568c71b60b602204 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Tue, 15 Mar 2022 01:37:59 -0600 Subject: [PATCH] Add some sanity comments and a full lobby check --- server/src/events/lobby/players/join.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/src/events/lobby/players/join.ts b/server/src/events/lobby/players/join.ts index 5d45845..de82ecc 100644 --- a/server/src/events/lobby/players/join.ts +++ b/server/src/events/lobby/players/join.ts @@ -15,7 +15,7 @@ const data: WebsocketEvent = { let game = games.get(gID); - // Ensure the user provided a game code that could be found + // Make sure the lobby exists that the player is attempting to connect to if (!game) { socket.emit(`res:lobby.players.join`, { status: Status.NotFound, @@ -24,6 +24,7 @@ const data: WebsocketEvent = { return; }; + // Player name unique-ness check let sameName = game.players.find(p => p.name == name); if (sameName) { socket.emit(`res:lobby.players.join`, { @@ -33,7 +34,7 @@ const data: WebsocketEvent = { return; }; - // Ensure the game is currently in the lobby state + // Make sure the player isn't trying to join a game part-way through if (game.status !== `lobby`) { socket.emit(`res:lobby.players.join`, { status: Status.Forbidden, @@ -42,6 +43,15 @@ const data: WebsocketEvent = { return; }; + // Lobby is full already + if (game.players.length >= 4) { + socket.emit(`res:lobby.players.join`, { + status: Status.Forbidden, + message: `Can't join a game that has all 4 spots taken already.`, + }); + return; + }; + let usedColours = game.players.map(p => p.colour); let newPlayerColour = null; do {