Implement the KillGame event.

This commit is contained in:
Oliver-Akins 2020-10-21 22:15:52 -06:00
parent c2e670c5f8
commit 103ecf8bb4

View file

@ -1,4 +1,44 @@
import { Server, Socket } from 'socket.io'; import { Server, Socket } from 'socket.io';
import { active_games, log } from '../main';
export const KillGame = (io: Server, socket: Socket, data: KillGame) => { export const KillGame = (io: Server, socket: Socket, data: KillGame) => {
console.log(`Killing the game`); try {
// Ensure active game
if (active_games[data.game_code] == null) {
log.debug(`Could not find active game with code: ${data.game_code}`);
socket.emit(`GameKilled`, {
success: false,
message: `Could not find active game with code: ${data.game_code}`,
});
return;
};
let game = active_games[data.game_code];
// Ensure host is killing
if (game.host !== data.user) {
log.info(`${data.user} just tried to kill game ${game.code}`);
socket.emit(`GameKilled`, {
success: false,
message: `Only the host can kill the game.`,
});
return;
};
log.info(`Deleting game ${game.code} with ${game.player_count} players.`);
// Alert all players in the room
io.to(game.code).emit(`GameKilled`, {
success: true,
});
// Remove all players from room
for (var player in game.players) {
io.sockets.connected[game.players[player].socket].leave(game.code);
};
// Delete game object
delete active_games[game.code];
} catch (err) {
log.prettyError(err);
};
}; };