diff --git a/src/game/game.module.ts b/src/game/game.module.ts index 1c9e86faf59645bf537e76a8720bb6ab741fe6bd..d08a79515052b1b3ee3f932f22d969c17bc3c74d 100644 --- a/src/game/game.module.ts +++ b/src/game/game.module.ts @@ -13,6 +13,9 @@ import { PersonEntity } from '../user/user.entity'; import { GameGroupEntity } from '../faction/faction.entity'; import { FactionEntity } from '../faction/faction.entity'; import { NotificationModule } from '../notifications/notifications.module'; +import { TickService } from './tick.service'; +import { ScoreService } from '../score/score.service'; +import { ScoreEntity } from 'src/score/score.entity'; @Module({ imports: [ @@ -24,10 +27,11 @@ import { NotificationModule } from '../notifications/notifications.module'; GameGroupEntity, ObjectivePointEntity, ObjectivePoint_HistoryEntity, + ScoreEntity, ]), NotificationModule, ], controllers: [GameController], - providers: [GameService], + providers: [GameService, TickService, ScoreService], }) export class GameModule {} diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 887d06e8ce4a2b9350da958cbe9532f9631eaee2..86bc61ef43c3255b83914cc08e2f1dcfae61bf90 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -12,7 +12,7 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO } from './game.dto'; import { PersonEntity } from '../user/user.entity'; import { FactionEntity } from '../faction/faction.entity'; import { NotificationGateway } from '../notifications/notifications.gateway'; -import { ScoreService } from 'src/score/score.service'; +import { TickService } from './tick.service'; @Injectable() export class GameService { @@ -30,11 +30,9 @@ export class GameService { ObjectivePoint_HistoryEntity >, private notificationGateway: NotificationGateway, - private scoreService: ScoreService, - ) { - //start timer when gameservice is loaded - this.scoreService.startTimer(); - } + + private tickService: TickService, + ) {} // create a new game async createNewGame(personId: PersonEntity, gameData: GameDTO) { // checks if a game with the same name exists already @@ -149,7 +147,16 @@ export class GameService { const updatedGame = await this.gameRepository.findOne({ id: game.id }); if (updatedGame) { updatedGame.state = game.state; + console.log(game.state); await this.gameRepository.save(updatedGame); + + //add or remove from scoretick based on gamestate + if (game.state == 'STARTED') { + this.tickService.addGameToTimer(game.id); + } else { + this.tickService.removeGameFromTimer(game.id); + } + // notify players about game state change this.notificationGateway.server.emit(game.id, { type: 'gamestate-update', diff --git a/src/game/tick.service.ts b/src/game/tick.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..8417fe8b0b3b11c464bd4d837743ec3cabdcbc33 --- /dev/null +++ b/src/game/tick.service.ts @@ -0,0 +1,49 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ScoreService } from '../score/score.service'; + +@Injectable() +export class TickService { + constructor(private scoreService: ScoreService) { + // 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 + */ + + this.startTimer(); + } + + 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); + } + + // 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); + } + } + }; +} diff --git a/src/main.ts b/src/main.ts index dec8e6ff6f5d37f0373c9b7b709b717212408500..ebd53e5906d44738a4775716b7165108fae426ca 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import { AppModule } from './app.module'; */ async function bootstrap() { - const app = await NestFactory.create(AppModule, { logger: console }); + const app = await NestFactory.create(AppModule); // Cors is needed for application/json POST app.enableCors(); await app.listen(5000); diff --git a/src/score/score.service.ts b/src/score/score.service.ts index 957780c2c8699f4791e11d495e2f6ce5973667f7..163c3ebfc2df4c11cee7e160159d5bc73b326b7f 100644 --- a/src/score/score.service.ts +++ b/src/score/score.service.ts @@ -116,32 +116,4 @@ export class ScoreService { ); return scores; } - - // tickinterval in milliseconds (10 minutes = 600 000) - private readonly tickInterval: number = 5000; - // dictionary to push gameId linked to start state - private ongoingGames = {}; - - // initializing timer - async startTimer() { - setInterval(this.Tick, this.tickInterval); - } - - async addToTimer(gameId: string) { - this.Tick(); - this.ongoingGames[gameId] = Date.now(); - } - - async removeFromTimer(gameId: string) { - delete this.ongoingGames[gameId]; - } - - // tick score for games with STARTED-status - Tick = () => { - if (this.ongoingGames != null) { - for (var game in this.ongoingGames) { - console.log(game); - } - } - }; }