import { Injectable, Logger } from '@nestjs/common'; import { ScoreService } from '../score/score.service'; import { InjectRepository } from '@nestjs/typeorm'; import { GameEntity } from 'src/game/game.entity'; import { Repository } from 'typeorm'; @Injectable() export class TickService { constructor( private scoreService: ScoreService, @InjectRepository(GameEntity) private gameRepository: Repository<GameEntity>, ) { // 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; // initializing timer async startTimer() { this.logger.log('Started timer'); setInterval(this.Tick, this.tickInterval); } // returns name and id of each game async listGames() { const games = await this.gameRepository.find({ where: { state: 'STARTED' }, }); return games.map(game => { return game.gameObject(); }); } // tick score for games with STARTED-status Tick = async _ => { this.logger.log('Ticking'); let games = await this.listGames(); games.map(game => { this.scoreService.scoreTick(game.id); }); }; }