From 1b0e19afcf48b10c6a50fb447ac1d6636f5b5216 Mon Sep 17 00:00:00 2001 From: NewCodes7 Date: Tue, 3 Dec 2024 11:47:53 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=A3=BD=EC=9D=80=20=EC=9E=90=EB=81=BC?= =?UTF-8?q?=EB=A6=AC=EC=9D=98=20=EC=B1=84=ED=8C=85=EC=9D=B4=20=EC=95=88=20?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/game/service/game.chat.service.ts | 14 ++++++++++---- BE/src/game/service/game.room.service.ts | 9 ++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/BE/src/game/service/game.chat.service.ts b/BE/src/game/service/game.chat.service.ts index 1fd4bb0..10915b3 100644 --- a/BE/src/game/service/game.chat.service.ts +++ b/BE/src/game/service/game.chat.service.ts @@ -57,6 +57,7 @@ export class GameChatService { const playerKey = REDIS_KEY.PLAYER(chatMessage.playerId); const isAlivePlayer = await this.redis.hget(playerKey, 'isAlive'); + // 생존한 사람이라면 전체 브로드캐스팅 if (isAlivePlayer === SurvivalStatus.ALIVE) { server.to(gameId).emit(SocketEvents.CHAT_MESSAGE, chatMessage); return; @@ -66,11 +67,16 @@ export class GameChatService { const players = await this.redis.smembers(REDIS_KEY.ROOM_PLAYERS(gameId)); await Promise.all( players.map(async (playerId) => { - const playerKey = REDIS_KEY.PLAYER(playerId); - const isAlive = await this.redis.hget(playerKey, 'isAlive'); + const socketId = await this.redis.hget(REDIS_KEY.PLAYER(playerId), 'socketId'); + const socket = server.sockets.get(socketId); - if (isAlive === '0') { - server.to(playerId).emit(SocketEvents.CHAT_MESSAGE, chatMessage); + if (!socket) { + return; + } + + const isAlive = await this.redis.hget(REDIS_KEY.PLAYER(playerId), 'isAlive'); + if (isAlive === SurvivalStatus.DEAD) { + socket.emit(SocketEvents.CHAT_MESSAGE, chatMessage); } }) ); diff --git a/BE/src/game/service/game.room.service.ts b/BE/src/game/service/game.room.service.ts index 57037df..06cd8c1 100644 --- a/BE/src/game/service/game.room.service.ts +++ b/BE/src/game/service/game.room.service.ts @@ -90,6 +90,12 @@ export class GameRoomService { client.join(gameId); client.emit(SocketEvents.GET_SELF_ID, { playerId: clientId }); client.emit(SocketEvents.JOIN_ROOM, { players, isHost }); + + // 재접속한 플레이어의 socketId를 업데이트 + await this.redis.hset(REDIS_KEY.PLAYER(clientId), { + socketId: client.id + }); + return; } @@ -118,7 +124,8 @@ export class GameRoomService { positionY: positionY.toString(), disconnected: '0', gameId: gameId, - isAlive: SurvivalStatus.ALIVE + isAlive: SurvivalStatus.ALIVE, + socketId: client.id }); await this.redis.zadd(REDIS_KEY.ROOM_LEADERBOARD(gameId), 0, clientId);