Skip to content

Commit

Permalink
feat: balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChoDev committed Mar 31, 2024
1 parent d74614a commit 6df3b82
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions apps/server/src/models/history.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { Game } from './game.model'

export class GameHistory
extends Model<GameHistoryAttributes, GameHistoryInput>
implements GameHistoryAttributes {
implements GameHistoryAttributes
{
public game_id!: string
public player_id!: string
public key!: string
Expand Down Expand Up @@ -58,16 +59,24 @@ 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)
this.redis.keys(`game::${game_id}::*`).then(async (keys) => {
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),
)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/service/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down

0 comments on commit 6df3b82

Please sign in to comment.