Update processing and add host check

This commit is contained in:
Oliver-Akins 2020-10-09 16:30:14 -06:00
parent 004819ac7a
commit 7b8ade6f4c

View file

@ -2,22 +2,32 @@ import { Server, Socket } from 'socket.io';
import { active_games, log } from '../main';
export const StartGame = (io: Server, socket: Socket, data: StartGame) => {
try {
let gc = data.game_code;
// Ensure game is active
if (active_games[gc] == null) {
log.error(`Cannot start an unknown game. (code: ${gc})`);
socket.emit(`GameStartError`, {
if (active_games[data.game_code] == null) {
log.error(`Cannot start an unknown game. (code: ${data.game_code})`);
socket.emit(`Starting`, {
success: false,
message: `Cannot start game with an unknown game code.`,
});
return;
};
let game = active_games[data.game_code];
// Ensure user is the host
if (data.user !== game.host) {
log.info(`Non-host player (${data.user}) attempted to start the game.`);
socket.emit(`Starting`, {
success: false,
message: `Only the host can start the game.`,
});
return;
};
// Ensure the game can be started
} catch (err) {
log.prettyError(err);
socket.emit(`GameStartError`, {
socket.emit(`Starting`, {
success: false,
message: `${err.name}: ${err.message}`,
});