Broadcast update lobby event to the whole room
parent
496c68550b
commit
e982f782cb
|
|
@ -14,6 +14,7 @@ import {
|
||||||
CreateLobbyEvent,
|
CreateLobbyEvent,
|
||||||
emitUpdateLobbyEvent,
|
emitUpdateLobbyEvent,
|
||||||
JoinLobbyEvent,
|
JoinLobbyEvent,
|
||||||
|
ServerEvent,
|
||||||
} from 'interface';
|
} from 'interface';
|
||||||
import { createWsExceptionFilter } from './websocket-exception-filter';
|
import { createWsExceptionFilter } from './websocket-exception-filter';
|
||||||
import {
|
import {
|
||||||
|
|
@ -31,7 +32,7 @@ export class AppService implements OnGatewayConnection {
|
||||||
private readonly gameService: GameService,
|
private readonly gameService: GameService,
|
||||||
) {}
|
) {}
|
||||||
handleConnection(client: Socket) {
|
handleConnection(client: Socket) {
|
||||||
this.playerService.createPlayer(client.id);
|
this.playerService.createPlayer(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseFilters(
|
@UseFilters(
|
||||||
|
|
@ -71,9 +72,12 @@ export class AppService implements OnGatewayConnection {
|
||||||
this.playerService.getPlayer(client.id),
|
this.playerService.getPlayer(client.id),
|
||||||
event.lobbyId,
|
event.lobbyId,
|
||||||
);
|
);
|
||||||
emitUpdateLobbyEvent(client, {
|
const playerNames = game.players.map((player) => player.userName);
|
||||||
playerNames: game.players.map((player) => player.userName),
|
game.players.forEach((player) =>
|
||||||
gameCode: game.gameCode,
|
emitUpdateLobbyEvent(player.socket, {
|
||||||
});
|
playerNames: playerNames,
|
||||||
|
gameCode: game.gameCode,
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,16 @@ import {
|
||||||
MissingPlayerNameException,
|
MissingPlayerNameException,
|
||||||
PlayerNotFoundException,
|
PlayerNotFoundException,
|
||||||
} from 'src/exceptions';
|
} from 'src/exceptions';
|
||||||
|
import { Socket } from 'socket.io/dist/socket';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PlayerService {
|
export class PlayerService {
|
||||||
private readonly logger = new Logger(PlayerService.name);
|
private readonly logger = new Logger(PlayerService.name);
|
||||||
private readonly players: Map<string, Player> = new Map();
|
private readonly players: Map<string, Player> = new Map();
|
||||||
|
|
||||||
createPlayer(socketId: string) {
|
createPlayer(socket: Socket) {
|
||||||
const player: Player = new Player(socketId);
|
const player: Player = new Player(socket);
|
||||||
this.players.set(socketId, player);
|
this.players.set(player.socketId, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
addUserName(socketId: string, userName: string) {
|
addUserName(socketId: string, userName: string) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
|
import { Socket } from "socket.io";
|
||||||
|
|
||||||
export class Player {
|
export class Player {
|
||||||
socketId: string;
|
socketId: string;
|
||||||
|
socket: Socket;
|
||||||
userName?: string;
|
userName?: string;
|
||||||
constructor(socketId: string) {
|
|
||||||
this.socketId = socketId;
|
constructor(socket: Socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
this.socketId = socket.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue