diff --git a/src/events/HostGame.ts b/src/events/HostGame.ts index 4b1f33c..9d621d4 100644 --- a/src/events/HostGame.ts +++ b/src/events/HostGame.ts @@ -14,7 +14,7 @@ that the host is able to just send that to the other players import { Server, Socket } from 'socket.io'; import { Game } from '../utils/Game'; import { active_games, log } from '../main'; -import { generate_game_code } from '../utils/ids'; +import { generate_game_code, generate_user_id } from '../utils/ids'; export const HostGame = (io: Server, socket: Socket, data: HostGame) => { try { @@ -30,14 +30,16 @@ export const HostGame = (io: Server, socket: Socket, data: HostGame) => { // Create the game let game = new Game(game_code); - game.add_player(data.username, socket.id, true); + let uuid = generate_user_id(); + game.add_player(data.username, socket.id, uuid, true); active_games[game_code] = game; log.info(`${data.username} created a game. (Gamecode: ${game_code})`); // Respond to client socket.emit(`HostInformation`, { success: true, - game_code: game_code + game_code: game_code, + uuid: uuid, }); } catch (err) { log.prettyError(err); diff --git a/src/events/JoinGame.ts b/src/events/JoinGame.ts index 8cac21d..5895d7c 100644 --- a/src/events/JoinGame.ts +++ b/src/events/JoinGame.ts @@ -10,6 +10,7 @@ Emissions: */ import { Server, Socket } from 'socket.io'; import { active_games, log } from '../main'; +import { generate_user_id } from '../utils/ids'; export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => { try { // Check if it's an active game @@ -25,17 +26,17 @@ export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => { let game = active_games[data.game_code]; if (game.status !== `lobby`) { - log.error(`Cannot join the game ${game.code}. (state=${game.status})`) + log.warn(`Cannot join the game ${game.code}. (state=${game.status})`); socket.emit(`GameJoined`, { success: false, message: `That game cannot be joined because it is not in the lobby.`, }); return; - } + }; // Ensure username is not already taken if (Object.keys(game.players).includes(data.username)) { - log.error(`${data.username} tried joining game ${game.code} but the name is already taken.`); + log.warn(`${data.username} tried joining game ${game.code} but the name is already taken.`); socket.emit(`GameJoined`, { success: false, message: `That username is already taken, please try another.`, @@ -46,15 +47,21 @@ export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => { // Add to game room socket.join(game.code); + // Generate a UUID for the user + let uuid: string; + do { + uuid = generate_user_id() + } while (!game.UUID_free(uuid)); + // Add the player - game.add_player(data.username, socket.id); + game.add_player(data.username, socket.id, uuid); log.info(`${data.username} joined game ${game.code}`); // Alert player who joined socket.emit(`GameJoined`, { success: true, - players: game.players, - }) + uuid: uuid, + }); // Alert existing players about new player socket.to(game.code).emit(`NewPlayer`, { diff --git a/src/types/client_data.d.ts b/src/types/client_data.d.ts index 193de78..4ff71c8 100644 --- a/src/types/client_data.d.ts +++ b/src/types/client_data.d.ts @@ -6,6 +6,7 @@ interface response { interface HostInformation extends response { // properties depend on `success` being `true` game_code?: string; + uuid?: string; } // Event is only emitted when the JoinGame event is successful, so there is no @@ -16,7 +17,7 @@ interface NewPlayer { interface GameJoined extends response { // properties depend on `success` being `true` - players?: string[]; + uuid?: string; } interface PlayerList extends response { diff --git a/src/types/db.d.ts b/src/types/db.d.ts index 48fc867..ba1cebe 100644 --- a/src/types/db.d.ts +++ b/src/types/db.d.ts @@ -3,6 +3,7 @@ interface player { role: "liberal"|"fascist"|"hitler"|undefined; socket: string; host: boolean; + id: string; } type game_states = "lobby"; diff --git a/src/utils/Game.ts b/src/utils/Game.ts index 542bb8b..eafbde4 100644 --- a/src/utils/Game.ts +++ b/src/utils/Game.ts @@ -44,12 +44,13 @@ export class Game { return this.player_len; }; - public add_player(player: string, id: string, is_host:boolean=false): void { + public add_player(player: string, socket_id: string, id: string, is_host:boolean=false): void { this._players[player] = { position: undefined, + socket: socket_id, role: `liberal`, host: is_host, - socket: id, + id: id, }; this.player_len++; }; @@ -105,6 +106,15 @@ export class Game { }; }; + public UUID_free(uuid: string): boolean { + for (var player in this._players) { + if (this._players[player].id == uuid) { + return false; + }; + }; + return true; + } + public toString(): string { return `Game(code=${this.code},players=${this.player_len})`; };