From 6df3b82308fd08ff11c94d66b090e9a2b6819650 Mon Sep 17 00:00:00 2001 From: Thanapat Chotipun Date: Sun, 31 Mar 2024 14:06:52 +0700 Subject: [PATCH] feat: balancer --- apps/server/src/models/history.model.ts | 17 +++++++++++++---- apps/server/src/service/player.service.ts | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/server/src/models/history.model.ts b/apps/server/src/models/history.model.ts index 015df87..a99ce46 100644 --- a/apps/server/src/models/history.model.ts +++ b/apps/server/src/models/history.model.ts @@ -15,7 +15,8 @@ import { Game } from './game.model' export class GameHistory extends Model - implements GameHistoryAttributes { + implements GameHistoryAttributes +{ public game_id!: string public player_id!: string public key!: string @@ -58,7 +59,7 @@ export class GameHistoryRepository { RedisFunctions, RedisScripts >, - ) { } + ) {} async createHistory(game_id: string, key: string, vote: number) { const total = await this.redis.incrBy(`game::${game_id}::${key}`, vote) @@ -66,8 +67,16 @@ export class GameHistoryRepository { keys.forEach(async (k) => { if (k !== `game::${game_id}::${key}`) { const kTotal = parseInt((await this.redis.get(k)) || '0') - if (kTotal < total - vote) { - this.redis.incrBy(k, Math.round((0.5 - (kTotal / (total + kTotal))) * vote)) + if (kTotal < total) { + this.redis.incrBy( + k, + Math.round((0.5 - kTotal / (total + kTotal)) * vote), + ) + } else { + this.redis.incrBy( + `game::${game_id}::${key}`, + Math.round((0.5 - total / (total + kTotal)) * vote), + ) } } }) diff --git a/apps/server/src/service/player.service.ts b/apps/server/src/service/player.service.ts index 22c7ff1..be1059e 100644 --- a/apps/server/src/service/player.service.ts +++ b/apps/server/src/service/player.service.ts @@ -11,7 +11,7 @@ export class PlayerService { private readonly gameRepository: GameRepository, private readonly clientRepository: ClientRepository, private readonly gameHistoryRepository: GameHistoryRepository, - ) { } + ) {} async getGame(id: string) { return await this.gameRepository.getGameById(id).catch((err) => ({ err }))