Add socket ID to player's information and remove host from initializer

This commit is contained in:
Oliver-Akins 2020-10-08 21:19:23 -06:00
parent efa7540404
commit f43f36eb46
4 changed files with 8 additions and 5 deletions

View file

@ -29,7 +29,8 @@ export const HostGame = (socket: Socket, data: HostGame) => {
socket.join(game_code); socket.join(game_code);
// Create the game // Create the game
let game = new Game(game_code, data.username) let game = new Game(game_code);
game.add_player(data.username, socket.id, true);
active_games[game_code] = game; active_games[game_code] = game;
log.info(`${data.username} created a game. (Gamecode: ${game_code})`); log.info(`${data.username} created a game. (Gamecode: ${game_code})`);
@ -40,7 +41,6 @@ export const HostGame = (socket: Socket, data: HostGame) => {
}); });
} catch (err) { } catch (err) {
log.prettyError(err); log.prettyError(err);
// Let client know an error occured
socket.emit(`HostInformation`, { socket.emit(`HostInformation`, {
success: false, success: false,
error: `${err.name}: ${err.message}` error: `${err.name}: ${err.message}`

View file

@ -45,6 +45,8 @@ export const JoinGame = (socket: Socket, data: JoinGame) => {
game.add_player(data.username); game.add_player(data.username);
// Add the player
game.add_player(data.username, socket.id);
log.info(`${data.username} joined game ${game.code}`); log.info(`${data.username} joined game ${game.code}`);
// Alert player who joined // Alert player who joined

1
src/types/db.d.ts vendored
View file

@ -1,6 +1,7 @@
interface player { interface player {
position: "chancellor"|"president"|undefined; position: "chancellor"|"president"|undefined;
role: "liberal"|"fascist"|"hitler"|undefined; role: "liberal"|"fascist"|"hitler"|undefined;
socket: string;
host: boolean; host: boolean;
} }

View file

@ -4,12 +4,11 @@ export class Game {
private banned_players: string[]; private banned_players: string[];
private _players: players; private _players: players;
public constructor(code: string, host: string) { public constructor(code: string) {
this.code = code; this.code = code;
this._players = {}; this._players = {};
this.status = `lobby`; this.status = `lobby`;
this.banned_players = []; this.banned_players = [];
this.add_player(host, true);
}; };
get players(): players { get players(): players {
@ -25,11 +24,12 @@ export class Game {
return null; return null;
} }
public add_player(player: string, is_host:boolean=false): void { public add_player(player: string, id: string, is_host:boolean=false): void {
this._players[player] = { this._players[player] = {
position: undefined, position: undefined,
role: undefined, role: undefined,
host: is_host, host: is_host,
socket: id,
}; };
}; };