Update KickPlayer to RemovePlayer

This commit is contained in:
Oliver-Akins 2020-10-21 21:43:44 -06:00
parent 5888f5e2a9
commit c2e670c5f8
5 changed files with 27 additions and 20 deletions

View file

@ -79,7 +79,8 @@
Fail: Origin Fail: Origin
</td> </td>
<td colspan="2"> <td colspan="2">
This is just to get the list of players, there's nothing else to it. This is just to get the list of players, there's nothing
else to it.
</td> </td>
</tr> </tr>
<tr> <tr>
@ -123,31 +124,37 @@
Fail: Not Sent Fail: Not Sent
</td> </td>
<td colspan="2"> <td colspan="2">
Alerts players already in the game of the newly joined player. Alerts players already in the game of the newly joined
player.
</td> </td>
</tr> </tr>
<tr> <tr>
<td><code><a href="#">KickPlayer</a></code></td> <td><code><a href="#">RemovePlayer</a></code></td>
<td><code><a href="#">PlayerKicked</a></code></td> <td><code><a href="#">PlayerRemoved</a></code></td>
<td> <td>
Success: Room Success: Room
<br> <br>
Fail: Origin Fail: Origin
</td> </td>
<td colspan="2"> <td colspan="2">
When the host kicks a player from the lobby this event is sent out to alert all the players of the player's removal so the UI can be updated as needed. When the host kicks a player from the lobby this event is
sent out to alert all the players of the player's removal
so the UI can be updated as needed.
</td> </td>
</tr> </tr>
<tr> <tr>
<td><code><a href="#">KillGame</a></code></td> <td><code><a href="#">KillGame</a></code></td>
<td><code><a href="#"></a></code></td> <td><code><a href="#">GameKilled</a></code></td>
<td> <td>
Success: Success: Room
<br> <br>
Fail: Fail: Origin
</td>
<td colspan="2">
This event is sent out to everyone in the room when the host
leaves the game.
</td> </td>
<td colspan="2"></td>
</tr> </tr>
<tr> <tr>
<td><code><a href="#">NextPresident</a></code></td> <td><code><a href="#">NextPresident</a></code></td>

View file

@ -4,19 +4,19 @@ kicked, as well as can prevent them from re-joining the game again if the ban
property was set to true. property was set to true.
Emissions: Emissions:
PlayerKicked->All - This event is sent to everyone to indicate that the PlayerRemoved->All - This event is sent to everyone to indicate that the
player was removed from the game. If the client recognizes the player 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 name to be itself, it kicks the player back to he main page. Otherwise
it just removes the player from the display list. it just removes the player from the display list.
*/ */
import { Server, Socket } from 'socket.io'; import { Server, Socket } from 'socket.io';
import { active_games, log } from '../main'; import { active_games, log } from '../main';
export const KickPlayer = (io: Server, socket: Socket, data: KickPlayer) => { export const RemovePlayer = (io: Server, socket: Socket, data: RemovePlayer) => {
try { try {
// Check if it's an active game // Check if it's an active game
if (active_games[data.game_code] == null) { if (active_games[data.game_code] == null) {
log.debug(`Can't find an active game with code: ${data.game_code}`); log.debug(`Can't find an active game with code: ${data.game_code}`);
socket.emit(`PlayerKicked`, { socket.emit(`PlayerRemoved`, {
success: false, success: false,
message: `Error: The supplied game code could not be found.` message: `Error: The supplied game code could not be found.`
}); });
@ -28,7 +28,7 @@ export const KickPlayer = (io: Server, socket: Socket, data: KickPlayer) => {
// Ensure it's the host kicking the player // Ensure it's the host kicking the player
if (game.host !== data.user && data.user !== data.target) { if (game.host !== data.user && data.user !== data.target) {
log.info(`${data.user} attempted to kick ${data.target} from the game`); log.info(`${data.user} attempted to kick ${data.target} from the game`);
socket.emit(`PlayerKicked`, { socket.emit(`PlayerRemoved`, {
success: false, success: false,
message: `Cannot kick players when you are not the host.`, message: `Cannot kick players when you are not the host.`,
}); });
@ -38,7 +38,7 @@ export const KickPlayer = (io: Server, socket: Socket, data: KickPlayer) => {
// Ensure the user can be kicked during the state // Ensure the user can be kicked during the state
if (game.status !== `lobby`) { if (game.status !== `lobby`) {
log.warn(`Cannot kick a player from the game when not in the lobby. (state=${game.status})`); log.warn(`Cannot kick a player from the game when not in the lobby. (state=${game.status})`);
socket.emit(`PlayerKicked`, { socket.emit(`PlayerRemoved`, {
success: false, success: false,
message: `Cannot kick a player when the game is in progress.`, message: `Cannot kick a player when the game is in progress.`,
}); });
@ -52,13 +52,13 @@ export const KickPlayer = (io: Server, socket: Socket, data: KickPlayer) => {
game.remove_player(data.target, data.ban); game.remove_player(data.target, data.ban);
log.info(`${data.user} removed ${data.target} from ${data.game_code}`); log.info(`${data.user} removed ${data.target} from ${data.game_code}`);
io.to(game.code).to(target_socket).emit(`PlayerKicked`, { io.to(game.code).to(target_socket).emit(`PlayerRemoved`, {
success: true, success: true,
player: data.target, player: data.target,
}); });
} catch (err) { } catch (err) {
log.prettyError(err); log.prettyError(err);
socket.emit(`PlayerKicked`, { socket.emit(`PlayerRemoved`, {
success: false, success: false,
message: `${err.name}: ${err.message}`, message: `${err.name}: ${err.message}`,
}); });

View file

@ -7,9 +7,9 @@ import { KillGame } from "./events/KillGame";
import { JoinGame } from "./events/JoinGame"; import { JoinGame } from "./events/JoinGame";
import { HostGame } from "./events/HostGame"; import { HostGame } from "./events/HostGame";
import { StartGame } from "./events/StartGame"; import { StartGame } from "./events/StartGame";
import { KickPlayer } from "./events/KickPlayer";
import { VetoConfirm } from "./events/VetoConfirm"; import { VetoConfirm } from "./events/VetoConfirm";
import { VetoRequest } from "./events/VetoRequest"; import { VetoRequest } from "./events/VetoRequest";
import { RemovePlayer } from "./events/RemovePlayer";
import { ExecutePlayer } from "./events/ExecutePlayer"; import { ExecutePlayer } from "./events/ExecutePlayer";
import { GetPlayerList } from "./events/GetPlayerList"; import { GetPlayerList } from "./events/GetPlayerList";
import { NextPresident } from "./events/NextPresident"; import { NextPresident } from "./events/NextPresident";
@ -61,7 +61,7 @@ io.on(`connection`, (socket: sio.Socket) => {
socket.on(`HostGame`, (data: HostGame) => HostGame(io, socket, data)); socket.on(`HostGame`, (data: HostGame) => HostGame(io, socket, data));
socket.on(`JoinGame`, (data: JoinGame) => JoinGame(io, socket, data)); socket.on(`JoinGame`, (data: JoinGame) => JoinGame(io, socket, data));
socket.on(`KillGame`, (data: KillGame) => KillGame(io, socket, data)); socket.on(`KillGame`, (data: KillGame) => KillGame(io, socket, data));
socket.on(`KickPlayer`, (data: KickPlayer) => KickPlayer(io, socket, data)); socket.on(`RemovePlayer`, (data: RemovePlayer) => RemovePlayer(io, socket, data));
// Join a game // Join a game

View file

@ -25,7 +25,7 @@ interface PlayerList extends response {
players?: string[] players?: string[]
} }
interface PlayerKicked extends response { interface PlayerRemoved extends response {
// properties depend on `success` being `true` // properties depend on `success` being `true`
player?: string; player?: string;
} }

2
src/types/data.d.ts vendored
View file

@ -39,7 +39,7 @@ interface JoinGame {
username: string; username: string;
} }
interface KickPlayer extends request { interface RemovePlayer extends request {
target: string; target: string;
ban: boolean; ban: boolean;
} }