Add event to get a specific player's info.
This commit is contained in:
parent
a8d0cb5b19
commit
647bdde1f1
4 changed files with 43 additions and 1 deletions
30
src/events/GetPlayerInfo.ts
Normal file
30
src/events/GetPlayerInfo.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { Server, Socket } from "socket.io";
|
||||
import { active_games, log } from "../main";
|
||||
export const GetPlayerInfo = (io: Server, socket: Socket, data: GetPlayerInfo) => {
|
||||
try {
|
||||
// Check if it's an active game
|
||||
if (active_games[data.game_code] == null) {
|
||||
log.debug(`Can't find an active game with code: ${data.game_code}`);
|
||||
socket.emit(`GameJoined`, {
|
||||
success: false,
|
||||
message: `Could not find an active game with that game code.`
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
let game = active_games[data.game_code];
|
||||
let player = game.players[data.user];
|
||||
|
||||
log.debug(`Sent player info to ${data.user}`);
|
||||
socket.emit(`PlayerInfo`, {
|
||||
success: true,
|
||||
role: player.role,
|
||||
});
|
||||
} catch (err) {
|
||||
log.prettyError(err);
|
||||
socket.emit(`PlayerInfo`, {
|
||||
success: false,
|
||||
message: `${err.name}: ${err.message}`,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@ import { RemovePlayer } from "./events/RemovePlayer";
|
|||
import { ExecutePlayer } from "./events/ExecutePlayer";
|
||||
import { GetPlayerList } from "./events/GetPlayerList";
|
||||
import { NextPresident } from "./events/NextPresident";
|
||||
import { GetPlayerInfo } from "./events/GetPlayerInfo";
|
||||
import { InvestigateParty } from "./events/InvestigateParty";
|
||||
import { ChancellorPolicy } from "./events/ChancellorPolicy";
|
||||
import { PresidentPolicies } from "./events/PresidentPolicies";
|
||||
|
|
@ -73,7 +74,7 @@ process.on('SIGINT', clean_up);
|
|||
const io = sio.listen(WSS_PORT);
|
||||
|
||||
io.on(`connection`, (socket: sio.Socket) => {
|
||||
log.info(`Client connected with id ${socket.id}`);
|
||||
log.silly(`Client connected with id ${socket.id}`);
|
||||
|
||||
// Game Management
|
||||
socket.on(`HostGame`, (data: HostGame) => HostGame(io, socket, data));
|
||||
|
|
@ -104,6 +105,7 @@ io.on(`connection`, (socket: sio.Socket) => {
|
|||
|
||||
// Utility Events
|
||||
socket.on(`GetPlayerList`, (data: GetPlayerList) => GetPlayerList(io, socket, data));
|
||||
socket.on(`GetPlayerInfo`, (data: GetPlayerInfo) => GetPlayerInfo(io, socket, data));
|
||||
});
|
||||
|
||||
io.on(`reconnect_attempt`, (socket: sio.Socket) => {
|
||||
|
|
|
|||
5
src/types/client_data.d.ts
vendored
5
src/types/client_data.d.ts
vendored
|
|
@ -20,6 +20,11 @@ interface GameJoined extends response {
|
|||
uuid?: string;
|
||||
}
|
||||
|
||||
interface PlayerInfo extends response {
|
||||
// properties depend on `success` being `true`
|
||||
role?: roles;
|
||||
}
|
||||
|
||||
interface PlayerList extends response {
|
||||
// properties depend on `success` being `true`
|
||||
players?: string[]
|
||||
|
|
|
|||
5
src/types/data.d.ts
vendored
5
src/types/data.d.ts
vendored
|
|
@ -26,6 +26,11 @@ interface ChancellorPolicy extends request {
|
|||
policy: policy;
|
||||
}
|
||||
|
||||
interface GetPlayerInfo extends request {
|
||||
success: boolean;
|
||||
role: roles;
|
||||
}
|
||||
|
||||
interface GetPlayerList {
|
||||
game_code: string;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue