Begin work on the Kickplayer event
This commit is contained in:
parent
5fa807f034
commit
86199d71d5
2 changed files with 68 additions and 1 deletions
|
|
@ -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}`,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
5
src/types/data.d.ts
vendored
5
src/types/data.d.ts
vendored
|
|
@ -39,7 +39,10 @@ interface JoinGame {
|
||||||
username: string;
|
username: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface KickPlayer extends request {}
|
interface KickPlayer extends request {
|
||||||
|
target: string;
|
||||||
|
ban: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
interface KillGame extends request {}
|
interface KillGame extends request {}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue