Skip to content

Commit

Permalink
fix: 서바이벌 모드 디버깅
Browse files Browse the repository at this point in the history
- 서바이벌 모드 끝난 이후 이동 안 되는 이슈 해결
- 다 죽거나 1등만 남았을 때 게임 끝나게 처리
- '1', '0' 상수화
  • Loading branch information
NewCodes7 committed Dec 3, 2024
1 parent 51c0ce2 commit fb89693
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export enum GameMode {
RANKING = 'RANKING',
SURVIVAL = 'SURVIVAL',
}

export enum SurvivalStatus {
ALIVE = '1',
DEAD = '0',
}
2 changes: 1 addition & 1 deletion BE/src/game/dto/create-game.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IsIn, IsInt, IsString, Max, MaxLength, Min, MinLength } from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { WsException } from '@nestjs/websockets';
import { GameMode } from '../../common/constants/game-mode';
import { GameMode } from '../../common/constants/game';

export class CreateGameDto {
@IsString()
Expand Down
2 changes: 1 addition & 1 deletion BE/src/game/dto/update-room-option.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IsIn, IsInt, IsString, Length, Max, MaxLength, Min, MinLength } from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { WsException } from '@nestjs/websockets';
import { GameMode } from '../../common/constants/game-mode';
import { GameMode } from '../../common/constants/game';

export class UpdateRoomOptionDto {
@IsString()
Expand Down
9 changes: 3 additions & 6 deletions BE/src/game/redis/subscribers/player.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Redis from 'ioredis';
import { Namespace } from 'socket.io';
import SocketEvents from '../../../common/constants/socket-events';
import { REDIS_KEY } from '../../../common/constants/redis-key.constant';
import { SurvivalStatus } from '../../../common/constants/game';

@Injectable()
export class PlayerSubscriber extends RedisSubscriber {
Expand Down Expand Up @@ -84,9 +85,9 @@ export class PlayerSubscriber extends RedisSubscriber {

const isAlivePlayer = await this.redis.hget(REDIS_KEY.PLAYER(playerId), 'isAlive');

if (isAlivePlayer === '1') {
if (isAlivePlayer === SurvivalStatus.ALIVE) {
server.to(gameId).emit(SocketEvents.UPDATE_POSITION, updateData);
} else if (isAlivePlayer === '0') {
} else if (isAlivePlayer === SurvivalStatus.DEAD) {
const players = await this.redis.smembers(REDIS_KEY.ROOM_PLAYERS(gameId));
const deadPlayers = await Promise.all(
players.map(async (id) => {
Expand All @@ -101,10 +102,6 @@ export class PlayerSubscriber extends RedisSubscriber {
server.to(player.id).emit(SocketEvents.UPDATE_POSITION, updateData);
});
}

this.logger.verbose(
`[updatePosition] RoomId: ${gameId} | playerId: ${playerId} | isAlive: ${isAlivePlayer === '1' ? '생존자' : '관전자'} | position: [${positionX}, ${positionY}]`
);
}

private async handlePlayerDisconnect(playerId: string, playerData: any, server: Namespace) {
Expand Down
27 changes: 21 additions & 6 deletions BE/src/game/redis/subscribers/timer.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Redis from 'ioredis';
import { Namespace } from 'socket.io';
import { REDIS_KEY } from '../../../common/constants/redis-key.constant';
import SocketEvents from '../../../common/constants/socket-events';
import { GameMode } from '../../../common/constants/game-mode';
import { GameMode, SurvivalStatus } from '../../../common/constants/game';

@Injectable()
export class TimerSubscriber extends RedisSubscriber {
Expand All @@ -28,6 +28,7 @@ export class TimerSubscriber extends RedisSubscriber {
const currentQuiz = await this.redis.get(REDIS_KEY.ROOM_CURRENT_QUIZ(gameId));
const [quizNum, state] = currentQuiz.split(':');

// REFACTOR: start, end 상수화
if (state === 'start') {
await this.handleQuizScoring(gameId, parseInt(quizNum), server);
} else {
Expand Down Expand Up @@ -113,12 +114,18 @@ export class TimerSubscriber extends RedisSubscriber {

// 생존 모드에서 모두 탈락하진 않았는지 체크
const players = await this.redis.smembers(REDIS_KEY.ROOM_PLAYERS(gameId));
const alivePlayers = players.filter(async (id) => {
const isAlive = await this.redis.hget(REDIS_KEY.PLAYER(id), 'isAlive');
return isAlive === '1';
});
const aliveCount = (await Promise.all(
players.map(id => this.redis.hget(REDIS_KEY.PLAYER(id), 'isAlive'))
)).filter(isAlive => isAlive === SurvivalStatus.ALIVE).length;

// 게임 끝을 알림
if (this.hasNoMoreQuiz(quizList, newQuizNum) || this.checkSurvivalEnd(aliveCount)) {
// 모든 플레이어를 생존자로 변경
const players = await this.redis.smembers(REDIS_KEY.ROOM_PLAYERS(gameId));
players.forEach((id) => {
this.redis.hset(REDIS_KEY.PLAYER(id), { isAlive: SurvivalStatus.ALIVE });
});

if (quizList.length <= newQuizNum || alivePlayers.length === 0) {
const leaderboard = await this.redis.zrange(
REDIS_KEY.ROOM_LEADERBOARD(gameId),
0,
Expand Down Expand Up @@ -185,4 +192,12 @@ export class TimerSubscriber extends RedisSubscriber {
// 실제 선택지 범위를 벗어나지 않도록 보정
return Math.min(answer, quizLen);
}

private hasNoMoreQuiz(quizList, newQuizNum: number) {
return quizList.length <= newQuizNum;
}

private checkSurvivalEnd(aliveCount: number) {
return aliveCount <= 1;
}
}
3 changes: 2 additions & 1 deletion BE/src/game/service/game.chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { REDIS_KEY } from '../../common/constants/redis-key.constant';
import SocketEvents from '../../common/constants/socket-events';
import { Namespace } from 'socket.io';
import { TraceClass } from '../../common/interceptor/SocketEventLoggerInterceptor';
import { SurvivalStatus } from '../../common/constants/game';

@TraceClass()
@Injectable()
Expand Down Expand Up @@ -56,7 +57,7 @@ export class GameChatService {
const playerKey = REDIS_KEY.PLAYER(chatMessage.playerId);
const isAlivePlayer = await this.redis.hget(playerKey, 'isAlive');

if (isAlivePlayer === '1') {
if (isAlivePlayer === SurvivalStatus.ALIVE) {
server.to(gameId).emit(SocketEvents.CHAT_MESSAGE, chatMessage);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion BE/src/game/service/game.room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UpdateRoomQuizsetDto } from '../dto/update-room-quizset.dto';
import { Socket } from 'socket.io';
import { KickRoomDto } from '../dto/kick-room.dto';
import { TraceClass } from '../../common/interceptor/SocketEventLoggerInterceptor';
import { SurvivalStatus } from '../../common/constants/game';

@TraceClass()
@Injectable()
Expand Down Expand Up @@ -117,7 +118,7 @@ export class GameRoomService {
positionY: positionY.toString(),
disconnected: '0',
gameId: gameId,
isAlive: '1'
isAlive: SurvivalStatus.ALIVE
});

await this.redis.zadd(REDIS_KEY.ROOM_LEADERBOARD(gameId), 0, clientId);
Expand Down
2 changes: 1 addition & 1 deletion BE/src/user/entities/user-quiz-archive.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { BaseModel } from '../../common/entity/base.entity';
import { QuizSetModel } from '../../quiz-set/entities/quiz-set.entity';
import { UserModel } from './user.entity';
import { GameMode } from '../../common/constants/game-mode';
import { GameMode } from '../../common/constants/game';

@Entity('user_quiz_archive')
export class UserQuizArchiveModel extends BaseModel {
Expand Down

0 comments on commit fb89693

Please sign in to comment.