-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgame.gateway.ts
More file actions
97 lines (84 loc) · 2.82 KB
/
game.gateway.ts
File metadata and controls
97 lines (84 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
WsResponse,
SubscribeMessage,
WebSocketGateway,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { ClientEvents } from '@shared/client/ClientEvents';
import { ServerEvents } from '@shared/server/ServerEvents';
import { LobbyManager } from '@app/game/lobby/lobby.manager';
import { Logger, UsePipes } from '@nestjs/common';
import { AuthenticatedSocket } from '@app/game/types';
import { ServerException } from '@app/game/server.exception';
import { SocketExceptions } from '@shared/server/SocketExceptions';
import { ServerPayloads } from '@shared/server/ServerPayloads';
import { LobbyCreateDto, LobbyJoinDto, RevealCardDto } from '@app/game/dtos';
import { WsValidationPipe } from '@app/websocket/ws.validation-pipe';
@UsePipes(new WsValidationPipe())
@WebSocketGateway()
export class GameGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
{
private readonly logger: Logger = new Logger(GameGateway.name);
constructor(
private readonly lobbyManager: LobbyManager,
)
{
}
afterInit(server: Server): any
{
// Pass server instance to managers
this.lobbyManager.server = server;
this.logger.log('Game server initialized !');
}
async handleConnection(client: Socket, ...args: any[]): Promise<void>
{
// Call initializers to set up socket
this.lobbyManager.initializeSocket(client as AuthenticatedSocket);
}
async handleDisconnect(client: AuthenticatedSocket): Promise<void>
{
// Handle termination of socket
this.lobbyManager.terminateSocket(client);
}
@SubscribeMessage(ClientEvents.Ping)
onPing(client: AuthenticatedSocket): void
{
client.emit(ServerEvents.Pong, {
message: 'pong',
});
}
@SubscribeMessage(ClientEvents.LobbyCreate)
onLobbyCreate(client: AuthenticatedSocket, data: LobbyCreateDto): WsResponse<ServerPayloads[ServerEvents.GameMessage]>
{
const lobby = this.lobbyManager.createLobby(data.mode, data.delayBetweenRounds);
lobby.addClient(client);
return {
event: ServerEvents.GameMessage,
data: {
color: 'green',
message: 'Lobby created',
},
};
}
@SubscribeMessage(ClientEvents.LobbyJoin)
onLobbyJoin(client: AuthenticatedSocket, data: LobbyJoinDto): void
{
this.lobbyManager.joinLobby(data.lobbyId, client);
}
@SubscribeMessage(ClientEvents.LobbyLeave)
onLobbyLeave(client: AuthenticatedSocket): void
{
client.data.lobby?.removeClient(client);
}
@SubscribeMessage(ClientEvents.GameRevealCard)
onRevealCard(client: AuthenticatedSocket, data: RevealCardDto): void
{
if (!client.data.lobby) {
throw new ServerException(SocketExceptions.LobbyError, 'You are not in a lobby');
}
client.data.lobby.instance.revealCard(data.cardIndex, client);
}
}