Newer
Older
import { Injectable, Logger } from '@nestjs/common';
import { ScoreService } from '../score/score.service';
import { GameService } from './game.service';
@Injectable()
export class TickService {
private scoreService: ScoreService, //private gameService: GameService,
// whenever Tickservice is called, it will start ticktimer
/*
WARNING: multiple calls start multiple timers,
if you need to use this service somewhere else remember to call startTimer method from somewhere else
*/
}
private logger: Logger = new Logger('TickLogger');
// tickinterval in milliseconds (10 minutes = 600 000)
private readonly tickInterval: number = 60000;
// dictionary to push gameId linked to start state
private ongoingGames = {};
// initializing timer
async startTimer() {
this.logger.log('Started timer');
setInterval(this.Tick, this.tickInterval);
games.map(game => {
this.ongoingGames[game.id] = Date.now();
}
// add the game to tick queue
async addGameToTimer(gameId: string) {
this.logger.log('Added: ' + gameId);
this.ongoingGames[gameId] = Date.now();
}
// remove game if the setting is set to pause
async removeGameFromTimer(gameId: string) {
this.logger.log('Deleted: ' + gameId);
delete this.ongoingGames[gameId];
}
// tick score for games with STARTED-status
Tick = () => {
this.logger.log('Ticking');
if (this.ongoingGames != null) {
for (var game in this.ongoingGames) {
this.scoreService.scoreTick(game);
}
}
};
}