Add socket ID to player's information and remove host from initializer
This commit is contained in:
parent
efa7540404
commit
f43f36eb46
4 changed files with 8 additions and 5 deletions
|
|
@ -29,7 +29,8 @@ export const HostGame = (socket: Socket, data: HostGame) => {
|
|||
socket.join(game_code);
|
||||
|
||||
// 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;
|
||||
log.info(`${data.username} created a game. (Gamecode: ${game_code})`);
|
||||
|
||||
|
|
@ -40,7 +41,6 @@ export const HostGame = (socket: Socket, data: HostGame) => {
|
|||
});
|
||||
} catch (err) {
|
||||
log.prettyError(err);
|
||||
// Let client know an error occured
|
||||
socket.emit(`HostInformation`, {
|
||||
success: false,
|
||||
error: `${err.name}: ${err.message}`
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ export const JoinGame = (socket: Socket, data: JoinGame) => {
|
|||
|
||||
game.add_player(data.username);
|
||||
|
||||
// Add the player
|
||||
game.add_player(data.username, socket.id);
|
||||
log.info(`${data.username} joined game ${game.code}`);
|
||||
|
||||
// Alert player who joined
|
||||
|
|
|
|||
1
src/types/db.d.ts
vendored
1
src/types/db.d.ts
vendored
|
|
@ -1,6 +1,7 @@
|
|||
interface player {
|
||||
position: "chancellor"|"president"|undefined;
|
||||
role: "liberal"|"fascist"|"hitler"|undefined;
|
||||
socket: string;
|
||||
host: boolean;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,12 +4,11 @@ export class Game {
|
|||
private banned_players: string[];
|
||||
private _players: players;
|
||||
|
||||
public constructor(code: string, host: string) {
|
||||
public constructor(code: string) {
|
||||
this.code = code;
|
||||
this._players = {};
|
||||
this.status = `lobby`;
|
||||
this.banned_players = [];
|
||||
this.add_player(host, true);
|
||||
};
|
||||
|
||||
get players(): players {
|
||||
|
|
@ -25,11 +24,12 @@ export class Game {
|
|||
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] = {
|
||||
position: undefined,
|
||||
role: undefined,
|
||||
host: is_host,
|
||||
socket: id,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue