Skip to content

Commit

Permalink
feat: scoreboard local emit
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChoDev committed Mar 31, 2024
1 parent 2b7aa39 commit d793eaa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions apps/server/src/models/history.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ 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 @@ -59,16 +58,16 @@ 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.decrBy(k, Math.floor((kTotal - total + vote) * 0.1))
if (kTotal < total - vote) {
this.redis.incrBy(k, Math.round((total - vote) * 0.01))
}
}
})
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 @@ -23,7 +23,7 @@ export class PlayerService {
const game = await this.gameHistoryRepository.getLastActiveGame()
if (game && game.id && game.status === 'playing') {
this.getScoreboard(game.id, game.actions).then((screen) => {
io.sockets.to('scoreboard').emit('scoreboard', screen)
io.local.to('scoreboard').emit('scoreboard', screen)
})
}
}, 100)
Expand Down

0 comments on commit d793eaa

Please sign in to comment.