Skip to content

Commit

Permalink
Merge pull request #41 from thyckcorgis/preventServerCrash
Browse files Browse the repository at this point in the history
add return guards for when room does not exist
  • Loading branch information
cbebe authored Jan 23, 2021
2 parents 670e739 + 12d66d6 commit 7b23447
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions server/src/socketEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ export const quitGameEvent: SocketEvent<QuitHandler> = (socket) => (
name,
isHost
) => {
const room = rooms[code];
socket.leave(code);
if (!roomExists(code)) return;

const room = rooms[code];

room.removeUser(name);
if (room.isEmpty()) return removeRoom(code);

Expand All @@ -77,7 +80,6 @@ export const quitGameEvent: SocketEvent<QuitHandler> = (socket) => (
newHost: room.getNewHost(isHost),
users: rooms[code].users,
};

socket.to(code).emit("quit-game", broadcast);
};

Expand All @@ -86,12 +88,13 @@ export const quitLobbyEvent: SocketEvent<QuitHandler> = (socket) => (
name,
isHost
) => {
socket.leave(code);

if (!roomExists(code)) return;
const room = rooms[code];
room.removeUser(name);
if (room.isEmpty()) return removeRoom(code);

socket.leave(code);

const broadcast: QuitLobbyResponse = {
newHost: room.getNewHost(isHost),
users: rooms[code].users,
Expand All @@ -101,6 +104,8 @@ export const quitLobbyEvent: SocketEvent<QuitHandler> = (socket) => (
};

export const customCardEvent: EventHandler = (code, question, fn) => {
if (!roomExists(code)) return;

const room = rooms[code];
if (!room.customCardsEnabled())
return fn(errorMessage("Custom cards are not allowed in this room"));
Expand All @@ -113,6 +118,8 @@ export const nextCardEvent: SocketEvent<AnonEventHandler> = (socket) => (
code,
fn
) => {
if (!roomExists(code)) return;

const room = rooms[code];
room.nextTurn();
room.drawCard();
Expand All @@ -128,6 +135,8 @@ export const settingEvent: SocketEvent<SettingHandler> = (socket) => (
code,
settings
) => {
if (!roomExists(code)) return;

rooms[code].setSettings(settings);
socket.to(code).emit("setting", settings);
};
Expand All @@ -136,6 +145,8 @@ export const startGameEvent: SocketEvent<AnonEventHandler> = (socket) => (
code,
fn
) => {
if (!roomExists(code)) return;

const room = rooms[code];
room.startGame();
room.createCardDeck();
Expand Down

0 comments on commit 7b23447

Please sign in to comment.