Broadcast update lobby event to the whole room

landing-page-layout
MiguelMLorente 2025-02-09 20:33:24 +01:00
parent 496c68550b
commit e982f782cb
3 changed files with 20 additions and 10 deletions

View File

@ -14,6 +14,7 @@ import {
CreateLobbyEvent,
emitUpdateLobbyEvent,
JoinLobbyEvent,
ServerEvent,
} from 'interface';
import { createWsExceptionFilter } from './websocket-exception-filter';
import {
@ -31,7 +32,7 @@ export class AppService implements OnGatewayConnection {
private readonly gameService: GameService,
) {}
handleConnection(client: Socket) {
this.playerService.createPlayer(client.id);
this.playerService.createPlayer(client);
}
@UseFilters(
@ -71,9 +72,12 @@ export class AppService implements OnGatewayConnection {
this.playerService.getPlayer(client.id),
event.lobbyId,
);
emitUpdateLobbyEvent(client, {
playerNames: game.players.map((player) => player.userName),
const playerNames = game.players.map((player) => player.userName);
game.players.forEach((player) =>
emitUpdateLobbyEvent(player.socket, {
playerNames: playerNames,
gameCode: game.gameCode,
});
}),
);
}
}

View File

@ -4,15 +4,16 @@ import {
MissingPlayerNameException,
PlayerNotFoundException,
} from 'src/exceptions';
import { Socket } from 'socket.io/dist/socket';
@Injectable()
export class PlayerService {
private readonly logger = new Logger(PlayerService.name);
private readonly players: Map<string, Player> = new Map();
createPlayer(socketId: string) {
const player: Player = new Player(socketId);
this.players.set(socketId, player);
createPlayer(socket: Socket) {
const player: Player = new Player(socket);
this.players.set(player.socketId, player);
}
addUserName(socketId: string, userName: string) {

View File

@ -1,7 +1,12 @@
import { Socket } from "socket.io";
export class Player {
socketId: string;
socket: Socket;
userName?: string;
constructor(socketId: string) {
this.socketId = socketId;
constructor(socket: Socket) {
this.socket = socket;
this.socketId = socket.id;
}
}