Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Commit

Permalink
Move GamePrecond to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
kcgidw committed May 23, 2020
1 parent d5e61f0 commit a37774b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 87 deletions.
87 changes: 87 additions & 0 deletions src/server/game-precond.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as Lobby from './lobby.js';
import GameError from './game-error.js';

// Room/game state validators

const GamePrecond = {
sockHasUser(sock) {
if (sock.user === undefined) {
throw new GameError('No user');
}
},
sockDoesNotHaveUser(sock) {
if (sock.user !== undefined) {
throw new GameError('Must not have user');
}
},
userIsInARoom(user) {
if (user.gameRoom === undefined) {
throw new GameError(`User ${user.name} should be in a room`, 'User must be in a room');
}
},
userIsNotInARoom(user) {
if (user.gameRoom !== undefined) {
throw new GameError(
'User must not be in a room. User is in room ' + user.gameRoom,
'User must not be in a room'
);
}
},
roomExists(roomCode) {
if (Lobby.getRoomByCode(roomCode) === undefined) {
throw new GameError(`Rm${roomCode} DNE`, 'This room is unavailable');
}
},
gameInProgress(room) {
if (!room.isGameInProgress()) {
throw new GameError('Game must be in progress');
}
},
gameNotInProgress(room) {
if (room.isGameInProgress()) {
throw new GameError(`Rm${room.roomCode} A game is already in progress`);
}
},
roomIsNotFull(room) {
if (room.isFull()) {
throw new GameError(`Rm${room.roomCode} is full`, 'This room is full', true);
}
},
lobbyIsNotFull() {
if (Lobby.isFull()) {
throw new GameError('The lobby is at max capacity');
}
},
isUsersTurn(user) {
let room = user.gameRoom;
if (room.whoseTurn() !== user) {
throw new GameError("Not user's turn");
}
},
nameIsNotTakenInRoom(username, room) {
if (room.findUser(username)) {
throw new GameError(
`Username ${username} is taken in Rm${room.roomCode}`,
'This username is taken in this room'
);
}
},
nameIsTakenInRoom(username, room) {
if (room.findUser(username) === undefined) {
throw new GameError(
`Username ${username} DNE in Rm${room.roomCode}`,
"This username doesn't exist in this room"
);
}
},
userIsDisconnected(username, room) {
if (!room.findUser(username).connected) {
throw new GameError(
`Username ${username} connected to Rm${room.roomCode}`,
'This username is taken in this room'
);
}
},
};

export default GamePrecond;
93 changes: 6 additions & 87 deletions src/server/socket-handler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import GAME_PHASE from '../common/game-phase.js';
import MESSAGE from '../common/message.js';
import User from '../common/user.js';
import debugLog from './debug-log.js';
import GameError from './game-error.js';
import GamePrecond from './game-precond';
import { ClientAdapter } from './game-room.js';
import * as Schema from './schema.js';
import * as Lobby from './lobby.js';
import GAME_PHASE from '../common/game-phase.js';
import GameError from './game-error.js';
import debugLog from './debug-log.js';

import MESSAGE from '../common/message.js';
import * as Schema from './schema.js';

function handleSockets(io) {
io.on('connection', function(sock) {
Expand Down Expand Up @@ -191,87 +191,6 @@ function joinRoom(user, room, rejoin, isHost = false) {
return room;
}

const GamePrecond = {
sockHasUser(sock) {
if (sock.user === undefined) {
throw new GameError('No user');
}
},
sockDoesNotHaveUser(sock) {
if (sock.user !== undefined) {
throw new GameError('Must not have user');
}
},
userIsInARoom(user) {
if (user.gameRoom === undefined) {
throw new GameError(`User ${user.name} should be in a room`, 'User must be in a room');
}
},
userIsNotInARoom(user) {
if (user.gameRoom !== undefined) {
throw new GameError(
'User must not be in a room. User is in room ' + user.gameRoom,
'User must not be in a room'
);
}
},
roomExists(roomCode) {
if (Lobby.getRoomByCode(roomCode) === undefined) {
throw new GameError(`Rm${roomCode} DNE`, 'This room is unavailable');
}
},
gameInProgress(room) {
if (!room.isGameInProgress()) {
throw new GameError('Game must be in progress');
}
},
gameNotInProgress(room) {
if (room.isGameInProgress()) {
throw new GameError(`Rm${room.roomCode} A game is already in progress`);
}
},
roomIsNotFull(room) {
if (room.isFull()) {
throw new GameError(`Rm${room.roomCode} is full`, 'This room is full', true);
}
},
lobbyIsNotFull() {
if (Lobby.isFull()) {
throw new GameError('The lobby is at max capacity');
}
},
isUsersTurn(user) {
let room = user.gameRoom;
if (room.whoseTurn() !== user) {
throw new GameError("Not user's turn");
}
},
nameIsNotTakenInRoom(username, room) {
if (room.findUser(username)) {
throw new GameError(
`Username ${username} is taken in Rm${room.roomCode}`,
'This username is taken in this room'
);
}
},
nameIsTakenInRoom(username, room) {
if (room.findUser(username) === undefined) {
throw new GameError(
`Username ${username} DNE in Rm${room.roomCode}`,
"This username doesn't exist in this room"
);
}
},
userIsDisconnected(username, room) {
if (!room.findUser(username).connected) {
throw new GameError(
`Username ${username} connected to Rm${room.roomCode}`,
'This username is taken in this room'
);
}
},
};

// send roomstate update to all users, accounting for different roles (i.e., faker vs artist)
function broadcastRoomState(io, room, messageName, addtlProcessFn) {
let state = ClientAdapter.generateStateJson(room);
Expand Down

0 comments on commit a37774b

Please sign in to comment.