Update processing and add host check
This commit is contained in:
parent
004819ac7a
commit
7b8ade6f4c
1 changed files with 16 additions and 6 deletions
|
|
@ -2,22 +2,32 @@ import { Server, Socket } from 'socket.io';
|
||||||
import { active_games, log } from '../main';
|
import { active_games, log } from '../main';
|
||||||
export const StartGame = (io: Server, socket: Socket, data: StartGame) => {
|
export const StartGame = (io: Server, socket: Socket, data: StartGame) => {
|
||||||
try {
|
try {
|
||||||
let gc = data.game_code;
|
|
||||||
|
|
||||||
// Ensure game is active
|
// Ensure game is active
|
||||||
if (active_games[gc] == null) {
|
if (active_games[data.game_code] == null) {
|
||||||
log.error(`Cannot start an unknown game. (code: ${gc})`);
|
log.error(`Cannot start an unknown game. (code: ${data.game_code})`);
|
||||||
socket.emit(`GameStartError`, {
|
socket.emit(`Starting`, {
|
||||||
success: false,
|
success: false,
|
||||||
message: `Cannot start game with an unknown game code.`,
|
message: `Cannot start game with an unknown game code.`,
|
||||||
});
|
});
|
||||||
return;
|
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) {
|
} catch (err) {
|
||||||
log.prettyError(err);
|
log.prettyError(err);
|
||||||
socket.emit(`GameStartError`, {
|
socket.emit(`Starting`, {
|
||||||
success: false,
|
success: false,
|
||||||
message: `${err.name}: ${err.message}`,
|
message: `${err.name}: ${err.message}`,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue