Skip to content

Commit a283c85

Browse files
authored
Merge pull request #14 from boostcampwm-2024/refactor/#11
refactor: Add test:joinRoom event for Artillery load testing
2 parents 5665cb6 + 8cc8e3f commit a283c85

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

server/src/game/game.gateway.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ export class GameGateway implements OnGatewayDisconnect {
4747
this.server.to(client.id).emit('joinedRoom', { room, roomSettings, playerId: player.playerId, players });
4848
}
4949

50+
@SubscribeMessage('test:joinRoom')
51+
async handleTestJoinRoom(
52+
@ConnectedSocket() client: Socket,
53+
@MessageBody() data: { roomId: string; playerId: string },
54+
) {
55+
const { room, roomSettings, player, players } = await this.gameService.testJoinRoom(data.roomId, data.playerId);
56+
57+
client.data.playerId = player.playerId;
58+
client.data.roomId = room.roomId;
59+
60+
await client.join(room.roomId);
61+
62+
client.to(room.roomId).emit('playerJoined', { room, roomSettings, players });
63+
64+
this.server.to(client.id).emit('joinedRoom', { room, roomSettings, playerId: player.playerId, players });
65+
}
66+
5067
@SubscribeMessage('reconnect')
5168
async handleReconnect(@ConnectedSocket() client: Socket, @MessageBody() data: { roomId: string; playerId: string }) {
5269
const { roomId, playerId } = data;
@@ -90,6 +107,8 @@ export class GameGateway implements OnGatewayDisconnect {
90107

91108
@SubscribeMessage('gameStart')
92109
async handleGameStart(@ConnectedSocket() client: Socket) {
110+
console.log('Game Start');
111+
93112
const { playerId, roomId } = client.data;
94113
if (!playerId || !roomId) throw new BadRequestException('Room ID and Player ID are required');
95114

server/src/game/game.service.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,45 @@ export class GameService {
140140
return `${adj} ${noun}`;
141141
}
142142

143+
async testJoinRoom(roomId: string, playerId: string) {
144+
const [room, roomSettings, players] = await Promise.all([
145+
this.gameRepository.getRoom(roomId),
146+
this.gameRepository.getRoomSettings(roomId),
147+
this.gameRepository.getRoomPlayers(roomId),
148+
]);
149+
150+
if (!room) throw new RoomNotFoundException();
151+
if (room.status === RoomStatus.GUESSING || room.status === RoomStatus.DRAWING) {
152+
throw new GameAlreadyStartedException('Cannot join room while game is in progress');
153+
}
154+
if (!roomSettings) throw new RoomNotFoundException('Room settings not found');
155+
if (players.length >= roomSettings.maxPlayers) {
156+
throw new RoomFullException('Room is full');
157+
}
158+
159+
const nickname = this.generateNickname();
160+
const player: Player = {
161+
playerId,
162+
role: null,
163+
status: PlayerStatus.NOT_PLAYING,
164+
nickname,
165+
profileImage: `https://api.dicebear.com/9.x/pixel-art/svg?seed=${nickname}`,
166+
score: 0,
167+
};
168+
169+
const isFirstPlayer = players.length === 0;
170+
if (isFirstPlayer) {
171+
room.hostId = playerId;
172+
await this.gameRepository.updateRoom(roomId, { hostId: playerId });
173+
}
174+
175+
await this.gameRepository.addPlayerToRoom(roomId, playerId, player);
176+
177+
const updatedPlayers = [player, ...players].reverse();
178+
179+
return { room, roomSettings, player, players: updatedPlayers };
180+
}
181+
143182
async reconnect(roomId: string, playerId: string) {
144183
const [room, roomSettings, players] = await Promise.all([
145184
this.gameRepository.getRoom(roomId),

0 commit comments

Comments
 (0)