Add some sanity comments and a full lobby check

This commit is contained in:
Oliver Akins 2022-03-15 01:37:59 -06:00
parent c340755ba2
commit 3f96b97194
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

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