From 86199d71d561d9d4c5cb584339a25dcbbc77f75a Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Tue, 6 Oct 2020 23:15:58 -0600 Subject: [PATCH] Begin work on the Kickplayer event --- src/events/KickPlayer.ts | 64 ++++++++++++++++++++++++++++++++++++++++ src/types/data.d.ts | 5 +++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/events/KickPlayer.ts b/src/events/KickPlayer.ts index e69de29..a7c35f7 100644 --- a/src/events/KickPlayer.ts +++ b/src/events/KickPlayer.ts @@ -0,0 +1,64 @@ +/* +Removes a player from the game. This alerts the player's client that they were +kicked, as well as can prevent them from re-joining the game again if the ban +property was set to true. + +Emissions: + PlayerKicked->All - This event is sent to everyone to indicate that the + player was removed from the game. If the client recognizes the player + name to be itself, it kicks the player back to he main page. Otherwise + it just removes the player from the display list. +*/ +import { Socket } from 'socket.io'; +import { active_games, log } from '../main'; +export const KickPlayer = (socket: Socket, data: KickPlayer) => { + try { + // Check if it's an active game + if (!Object.keys(active_games).includes(data.game_code)) { + log.debug(`Can't find an active game with code: ${data.game_code}`); + socket.emit(`PlayerKicked`, { + success: false, + message: `Error: The supplied game code could not be found.` + }); + return; + }; + + let game = active_games[data.game_code]; + + // Ensure it's the host kicking the player + if (game.host !== data.user) { + log.info(`${data.user} attempted to kick ${data.target} from the game`); + socket.emit(`PlayerKicked`, { + success: false, + message: `Cannot kick players when you are not the host.`, + }); + return; + }; + + // Ensure the user can be kicked during the state + if (game.status !== `lobby`) { + log.error(`Cannot kick a player from the game when not in the lobby. (state=${game.status})`); + socket.emit(`GameJoined`, { + success: false, + message: `Cannot kick a player when the game is in progress.`, + }); + return; + }; + + game.remove_player(data.target, data.ban); + log.info(`Removed ${data.target} from ${data.game_code}`); + + socket.broadcast.emit(`PlayerKicked`, { + success: true, + game_code: data.game_code, + player: data.target, + }); + } catch (err) { + log.prettyError(err); + socket.emit(`PlayerKicked`, { + success: false, + game_code: data.game_code, + message: `${err.name}: ${err.message}`, + }); + }; +}; \ No newline at end of file diff --git a/src/types/data.d.ts b/src/types/data.d.ts index a79df32..2045d4d 100644 --- a/src/types/data.d.ts +++ b/src/types/data.d.ts @@ -39,7 +39,10 @@ interface JoinGame { username: string; } -interface KickPlayer extends request {} +interface KickPlayer extends request { + target: string; + ban: boolean; +} interface KillGame extends request {}