From 603888bdc99530bd05c85ff46d5e8d6912d44dfc Mon Sep 17 00:00:00 2001 From: ssum1ra <101113206+ssum1ra@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:34:55 +0900 Subject: [PATCH] feat: Add player update functionality (#172) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 유미라 --- server/src/game/game.gateway.ts | 10 ++++++++++ server/src/game/game.repository.ts | 12 ++++++++++++ server/src/game/game.service.ts | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/server/src/game/game.gateway.ts b/server/src/game/game.gateway.ts index 7f9759b6..d8238345 100644 --- a/server/src/game/game.gateway.ts +++ b/server/src/game/game.gateway.ts @@ -79,6 +79,16 @@ export class GameGateway implements OnGatewayDisconnect { client.to(roomId).emit('settingsUpdated', { settings: updatedSettings }); } + @SubscribeMessage('updatePlayer') + async handle(@ConnectedSocket() client: Socket, @MessageBody() data: { player: Partial }) { + const { playerId, roomId } = client.data; + if (!roomId || !playerId) throw new BadRequestException('Room ID and Player ID are required'); + + const updatedPlayer = await this.gameService.updatePlayer(roomId, playerId, data.player); + + client.to(roomId).emit('playerUpdated', { player: updatedPlayer }); + } + @SubscribeMessage('gameStart') async handleGameStart(@ConnectedSocket() client: Socket) { const { playerId, roomId } = client.data; diff --git a/server/src/game/game.repository.ts b/server/src/game/game.repository.ts index 28aa5ed9..8d472e14 100644 --- a/server/src/game/game.repository.ts +++ b/server/src/game/game.repository.ts @@ -83,6 +83,18 @@ export class GameRepository { await multi.exec(); } + async getPlayer(roomId: string, playerId: string) { + const player = await this.redisService.hgetall(`room:${roomId}:players:${playerId}`); + if (!player || Object.keys(player).length === 0) return null; + + return { + ...player, + role: player.role === '' ? null : player.role, + profileImage: player.userImg === '' ? null : player.userImg, + score: parseInt(player.score, 10) || 0, + } as Player; + } + async updatePlayer(roomId: string, playerId: string, player: Partial) { await this.redisService.hset(`room:${roomId}:players:${playerId}`, player); } diff --git a/server/src/game/game.service.ts b/server/src/game/game.service.ts index 71e80c11..d5c9735a 100644 --- a/server/src/game/game.service.ts +++ b/server/src/game/game.service.ts @@ -184,6 +184,19 @@ export class GameService { return updatedSettings; } + async updatePlayer(roomId: string, playerId: string, data: Partial) { + const room = await this.gameRepository.getRoom(roomId); + if (!room) throw new RoomNotFoundException('Room not found'); + + const player = await this.gameRepository.getPlayer(roomId, playerId); + if (!player) throw new PlayerNotFoundException('Player not found'); + + const updatedPlayer = { ...player, ...data }; + await this.gameRepository.updatePlayer(roomId, playerId, updatedPlayer); + + return updatedPlayer; + } + async startGame(roomId: string, playerId: string) { const room = await this.gameRepository.getRoom(roomId); if (!room) throw new RoomNotFoundException('Room not found');