Skip to content

Commit

Permalink
fix: 죽은 자끼리의 채팅이 안 되는 버그 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
NewCodes7 committed Dec 3, 2024
1 parent fb89693 commit 1b0e19a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 10 additions & 4 deletions BE/src/game/service/game.chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
})
);
Expand Down
9 changes: 8 additions & 1 deletion BE/src/game/service/game.room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1b0e19a

Please sign in to comment.