Skip to content

Commit

Permalink
Merge branch 'dev-be' into fix-be-room-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
DongHoonYu96 authored Dec 2, 2024
2 parents 7e4ebb4 + 7e75160 commit 538dcea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
3 changes: 2 additions & 1 deletion BE/src/game/redis/game-redis-memory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class GameRedisMemoryService {
* TTL ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์Šค์ผ€์ค„๋Ÿฌ
* ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋กœ ๋ธ”๋กœํ‚น ์ตœ์†Œํ™”
*/
// @Cron(CronExpression.EVERY_SECOND)

// @Cron(CronExpression.EVERY_MINUTE)
async manageTTL(): Promise<void> {
try {
// SCAN์œผ๋กœ ํ™œ์„ฑ ๋ฐฉ ๋ชฉ๋ก์„ ๋ฐฐ์น˜๋กœ ์ฒ˜๋ฆฌ
Expand Down
17 changes: 4 additions & 13 deletions BE/src/game/redis/subscribers/timer.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class TimerSubscriber extends RedisSubscriber {
const selectAnswer = this.calculateAnswer(
player.positionX,
player.positionY,
quizList.length
parseInt(quiz.choiceCount)
);
// this.logger.verbose(selectAnswer);

Expand Down Expand Up @@ -167,8 +167,8 @@ export class TimerSubscriber extends RedisSubscriber {
}

private calculateAnswer(positionX: string, positionY: string, quizLen: number): number {
const x = parseFloat(positionX);
const y = parseFloat(positionY);
const x = parseFloat(positionY);
const y = parseFloat(positionX);

// ํ–‰์˜ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ (2์—ด ๊ณ ์ •์ด๋ฏ€๋กœ ์ด ๊ฐœ์ˆ˜์˜ ์ ˆ๋ฐ˜์„ ์˜ฌ๋ฆผ)
const rows = Math.ceil(quizLen / 2);
Expand All @@ -177,21 +177,12 @@ export class TimerSubscriber extends RedisSubscriber {
const rowIndex = Math.floor(y * rows);

// X ์ขŒํ‘œ๋กœ ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ ๊ฒฐ์ •
const colIndex = x < 0.5 ? 0 : 1;
const colIndex = Math.round(x);

// ์ตœ์ข… ์„ ํƒ์ง€ ๋ฒˆํ˜ธ ๊ณ„์‚ฐ
const answer = rowIndex * 2 + colIndex + 1;

// ์‹ค์ œ ์„ ํƒ์ง€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋„๋ก ๋ณด์ •
return Math.min(answer, quizLen);
// return (
// Math.round(
// parseFloat(positionX) + Math.floor(parseFloat(positionY) * Math.ceil(quizLen / 2))
// ) * 2
// );
// if (parseFloat(positionY) < 0.5) {
// return parseFloat(positionX) < 0.5 ? 1 : 2;
// }
// return parseFloat(positionX) < 0.5 ? 3 : 4;
}
}
42 changes: 21 additions & 21 deletions BE/src/game/service/game.room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { TraceClass } from '../../common/interceptor/SocketEventLoggerIntercepto
@Injectable()
export class GameRoomService {
private readonly logger = new Logger(GameRoomService.name);
private readonly PLAYER_GRACE_PERIOD = 10; // 10์ดˆ
private readonly INACTIVE_THRESHOLD = 30 * 60 * 1000; // 30๋ถ„ 30 * 60 * 1000;

constructor(
@InjectRedis() private readonly redis: Redis,
Expand Down Expand Up @@ -213,7 +213,7 @@ export class GameRoomService {
disconnected: '1',
disconnectedAt: Date.now().toString()
});
pipeline.expire(REDIS_KEY.PLAYER(clientId), this.PLAYER_GRACE_PERIOD);
// pipeline.expire(REDIS_KEY.PLAYER(clientId), this.PLAYER_GRACE_PERIOD);

await pipeline.exec();

Expand All @@ -231,7 +231,7 @@ export class GameRoomService {
const remainingPlayers = await this.redis.scard(roomPlayersKey);

// 4. ํ”Œ๋ ˆ์ด์–ด ๊ด€๋ จ ๋ชจ๋“  ํ‚ค์— TTL ์„ค์ •
await this.setTTLForPlayerKeys(clientId);
// await this.setTTLForPlayerKeys(clientId);

if (remainingPlayers === 0) {
// ๋งˆ์ง€๋ง‰ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋‚˜๊ฐ„ ๊ฒฝ์šฐ
Expand All @@ -256,24 +256,24 @@ export class GameRoomService {
/**
* ํ”Œ๋ ˆ์ด์–ด ๊ด€๋ จ ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— TTL ์„ค์ •
*/
private async setTTLForPlayerKeys(clientId: string): Promise<void> {
let cursor = '0';
const pattern = `Player:${clientId}:*`;
const pipeline = this.redis.pipeline();

do {
// SCAN์œผ๋กœ ํ”Œ๋ ˆ์ด์–ด ๊ด€๋ จ ํ‚ค๋“ค์„ ๋ฐฐ์น˜๋กœ ์ฐพ๊ธฐ
const [nextCursor, keys] = await this.redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
cursor = nextCursor;

// ์ฐพ์€ ๋ชจ๋“  ํ‚ค์— TTL ์„ค์ •
for (const key of keys) {
pipeline.expire(key, this.PLAYER_GRACE_PERIOD);
}
} while (cursor !== '0');

await pipeline.exec();
}
// private async setTTLForPlayerKeys(clientId: string): Promise<void> {
// let cursor = '0';
// const pattern = `Player:${clientId}:*`;
// const pipeline = this.redis.pipeline();
//
// do {
// // SCAN์œผ๋กœ ํ”Œ๋ ˆ์ด์–ด ๊ด€๋ จ ํ‚ค๋“ค์„ ๋ฐฐ์น˜๋กœ ์ฐพ๊ธฐ
// const [nextCursor, keys] = await this.redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
// cursor = nextCursor;
//
// // ์ฐพ์€ ๋ชจ๋“  ํ‚ค์— TTL ์„ค์ •
// for (const key of keys) {
// pipeline.expire(key, this.PLAYER_GRACE_PERIOD);
// }
// } while (cursor !== '0');
//
// await pipeline.exec();
// }

async kickRoom(kickRoomDto: KickRoomDto, clientId: string) {
const { gameId, kickPlayerId } = kickRoomDto;
Expand Down

0 comments on commit 538dcea

Please sign in to comment.