Skip to content

Commit

Permalink
Merge pull request #240 from NewCodes7/fix-be-multi-process
Browse files Browse the repository at this point in the history
[BE] fix#239 ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์Šค๋กœ ์‹คํ–‰์‹œ endQuizTime์ด ๋ฐœ์ƒ ์•ˆ ๋˜๋Š” ์ด์Šˆ ํ•ด๊ฒฐ
  • Loading branch information
NewCodes7 authored Nov 21, 2024
2 parents 78de22a + 3727c2f commit a4cf563
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions BE/src/common/constants/redis-key.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const REDIS_KEY = {
ROOM_CURRENT_QUIZ: (gameId: string) => `Room:${gameId}:CurrentQuiz`,
ROOM_TIMER: (gameId: string) => `Room:${gameId}:Timer`,
ROOM_QUIZ_SET: (gameId: string) => `Room:${gameId}:QuizSet`,
ROOM_SCORING_COUNT: (gameId: string) => `Room:${gameId}:ScoringCount`,
PLAYER: (playerId: string) => `Player:${playerId}`,
QUIZSET_ID: (quizSetId: number) => `Quizset:${quizSetId}`,

Expand Down
15 changes: 9 additions & 6 deletions BE/src/game/redis/subscribers/scoring.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import SocketEvents from '../../../common/constants/socket-events';

@Injectable()
export class ScoringSubscriber extends RedisSubscriber {
private scoringMap = new Map<string, number>();

constructor(@InjectRedis() redis: Redis) {
super(redis);
}
Expand All @@ -25,14 +23,19 @@ export class ScoringSubscriber extends RedisSubscriber {
}

private async handleScoring(gameId: string, completeClientsCount: number, server: Server) {
if (!this.scoringMap.has(gameId)) {
this.scoringMap[gameId] = 0;
const scoringKey = REDIS_KEY.ROOM_SCORING_COUNT(gameId);

if (!this.redis.exists(scoringKey)) {
this.redis.set(scoringKey, 0);
}
this.scoringMap[gameId] += completeClientsCount;
this.redis.incrby(scoringKey, completeClientsCount);

const playersCount = await this.redis.scard(REDIS_KEY.ROOM_PLAYERS(gameId));
if (this.scoringMap[gameId] >= playersCount) {
const scoringCount = await this.redis.get(scoringKey);

if (parseInt(scoringCount) >= playersCount) {
await this.completeScoring(gameId, server);
this.redis.set(scoringKey, 0);
}
}

Expand Down
1 change: 0 additions & 1 deletion BE/src/game/service/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { RedisSubscriberService } from '../redis/redis-subscriber.service';
@Injectable()
export class GameService {
private readonly logger = new Logger(GameService.name);
private scoringMap = new Map<string, number>();

constructor(
@InjectRedis() private readonly redis: Redis,
Expand Down

0 comments on commit a4cf563

Please sign in to comment.