Store user's id and send it to them.
Right now this is isn't used for anything, it will be eventually be used to allow the same person to reconnect to the same game after a disconnect.
This commit is contained in:
parent
aca7d25526
commit
7a3e50d522
5 changed files with 33 additions and 12 deletions
|
|
@ -14,7 +14,7 @@ that the host is able to just send that to the other players
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
import { Game } from '../utils/Game';
|
import { Game } from '../utils/Game';
|
||||||
import { active_games, log } from '../main';
|
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) => {
|
export const HostGame = (io: Server, socket: Socket, data: HostGame) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -30,14 +30,16 @@ export const HostGame = (io: Server, socket: Socket, data: HostGame) => {
|
||||||
|
|
||||||
// Create the game
|
// Create the game
|
||||||
let game = new Game(game_code);
|
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;
|
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})`);
|
||||||
|
|
||||||
// Respond to client
|
// Respond to client
|
||||||
socket.emit(`HostInformation`, {
|
socket.emit(`HostInformation`, {
|
||||||
success: true,
|
success: true,
|
||||||
game_code: game_code
|
game_code: game_code,
|
||||||
|
uuid: uuid,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.prettyError(err);
|
log.prettyError(err);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ Emissions:
|
||||||
*/
|
*/
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
import { active_games, log } from '../main';
|
import { active_games, log } from '../main';
|
||||||
|
import { generate_user_id } from '../utils/ids';
|
||||||
export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => {
|
export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => {
|
||||||
try {
|
try {
|
||||||
// Check if it's an active game
|
// 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];
|
let game = active_games[data.game_code];
|
||||||
|
|
||||||
if (game.status !== `lobby`) {
|
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`, {
|
socket.emit(`GameJoined`, {
|
||||||
success: false,
|
success: false,
|
||||||
message: `That game cannot be joined because it is not in the lobby.`,
|
message: `That game cannot be joined because it is not in the lobby.`,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Ensure username is not already taken
|
// Ensure username is not already taken
|
||||||
if (Object.keys(game.players).includes(data.username)) {
|
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`, {
|
socket.emit(`GameJoined`, {
|
||||||
success: false,
|
success: false,
|
||||||
message: `That username is already taken, please try another.`,
|
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
|
// Add to game room
|
||||||
socket.join(game.code);
|
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
|
// 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}`);
|
log.info(`${data.username} joined game ${game.code}`);
|
||||||
|
|
||||||
// Alert player who joined
|
// Alert player who joined
|
||||||
socket.emit(`GameJoined`, {
|
socket.emit(`GameJoined`, {
|
||||||
success: true,
|
success: true,
|
||||||
players: game.players,
|
uuid: uuid,
|
||||||
})
|
});
|
||||||
|
|
||||||
// Alert existing players about new player
|
// Alert existing players about new player
|
||||||
socket.to(game.code).emit(`NewPlayer`, {
|
socket.to(game.code).emit(`NewPlayer`, {
|
||||||
|
|
|
||||||
3
src/types/client_data.d.ts
vendored
3
src/types/client_data.d.ts
vendored
|
|
@ -6,6 +6,7 @@ interface response {
|
||||||
interface HostInformation extends response {
|
interface HostInformation extends response {
|
||||||
// properties depend on `success` being `true`
|
// properties depend on `success` being `true`
|
||||||
game_code?: string;
|
game_code?: string;
|
||||||
|
uuid?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event is only emitted when the JoinGame event is successful, so there is no
|
// Event is only emitted when the JoinGame event is successful, so there is no
|
||||||
|
|
@ -16,7 +17,7 @@ interface NewPlayer {
|
||||||
|
|
||||||
interface GameJoined extends response {
|
interface GameJoined extends response {
|
||||||
// properties depend on `success` being `true`
|
// properties depend on `success` being `true`
|
||||||
players?: string[];
|
uuid?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PlayerList extends response {
|
interface PlayerList extends response {
|
||||||
|
|
|
||||||
1
src/types/db.d.ts
vendored
1
src/types/db.d.ts
vendored
|
|
@ -3,6 +3,7 @@ interface player {
|
||||||
role: "liberal"|"fascist"|"hitler"|undefined;
|
role: "liberal"|"fascist"|"hitler"|undefined;
|
||||||
socket: string;
|
socket: string;
|
||||||
host: boolean;
|
host: boolean;
|
||||||
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type game_states = "lobby";
|
type game_states = "lobby";
|
||||||
|
|
|
||||||
|
|
@ -44,12 +44,13 @@ export class Game {
|
||||||
return this.player_len;
|
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] = {
|
this._players[player] = {
|
||||||
position: undefined,
|
position: undefined,
|
||||||
|
socket: socket_id,
|
||||||
role: `liberal`,
|
role: `liberal`,
|
||||||
host: is_host,
|
host: is_host,
|
||||||
socket: id,
|
id: id,
|
||||||
};
|
};
|
||||||
this.player_len++;
|
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 {
|
public toString(): string {
|
||||||
return `Game(code=${this.code},players=${this.player_len})`;
|
return `Game(code=${this.code},players=${this.player_len})`;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue