From 351b1e6962ba1689f16fce46bf9d9b10c8f3bf6e Mon Sep 17 00:00:00 2001 From: Samuli Virtapohja <l4721@student.jamk.fi> Date: Thu, 25 Jul 2019 10:17:41 +0300 Subject: [PATCH] restructure score ticking --- src/app.module.ts | 8 ++++-- src/game/game.controller.ts | 9 +----- src/game/game.module.ts | 3 +- src/game/game.service.ts | 10 ------- src/replay/replay.module.ts | 6 ++-- src/tick/tick.module.ts | 32 ++++++++++++++++++++++ src/{game => tick}/tick.service.ts | 44 ++++++++++++++---------------- 7 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 src/tick/tick.module.ts rename src/{game => tick}/tick.service.ts (50%) diff --git a/src/app.module.ts b/src/app.module.ts index d3461d2..6ad2f64 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,15 +2,12 @@ import { Module } from '@nestjs/common'; import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Connection } from 'typeorm'; - import { AppController } from './app.controller'; import { AppService } from './app.service'; - import { RolesGuard } from './shared/roles.guard'; //import { LoggingInterceptor } from './shared/logging.interceptor'; import { StatesGuard } from './shared/states.guard'; import { HttpErrorFilter } from './shared/http-error.filter'; - import { NotificationModule } from './notifications/notifications.module'; import { TaskModule } from './task/task.module'; import { TrackingModule } from './tracking/tracking.module'; @@ -20,6 +17,10 @@ import { FactionModule } from './faction/faction.module'; import { GameModule } from './game/game.module'; import { ScoreModule } from './score/score.module'; import { ReplayModule } from './replay/replay.module'; +import { TickModule } from './tick/tick.module'; + + + /* Core of the server, @@ -50,6 +51,7 @@ import { ReplayModule } from './replay/replay.module'; TrackingModule, ScoreModule, ReplayModule, + TickModule, ], controllers: [AppController], providers: [ diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 3fca766..9306831 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -24,7 +24,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; import { ValidationPipe } from '../shared/validation.pipe'; import { Roles, GameStates } from '../shared/guard.decorator'; import { GameEntity } from './game.entity'; -import { TickService } from './tick.service'; ///////////////////////////////////////////////////////////////////////// /// GameController /// @@ -37,13 +36,7 @@ import { TickService } from './tick.service'; @Controller('game') export class GameController { - constructor( - private gameservice: GameService, - private tickservice: TickService, - ) { - // starts timer - this.tickservice.startTimer(); - } + constructor(private gameservice: GameService) {} //new game @Post('new') diff --git a/src/game/game.module.ts b/src/game/game.module.ts index 2c5ba6a..8e51415 100644 --- a/src/game/game.module.ts +++ b/src/game/game.module.ts @@ -13,7 +13,6 @@ 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 '../score/score.entity'; import { MulterModule } from '@nestjs/platform-express'; @@ -40,6 +39,6 @@ import { MulterModule } from '@nestjs/platform-express'; }), ], controllers: [GameController], - providers: [GameService, TickService, ScoreService], + providers: [GameService, ScoreService], }) export class GameModule {} diff --git a/src/game/game.service.ts b/src/game/game.service.ts index e4bf8fa..1567d30 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -12,7 +12,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; import { PersonEntity } from '../user/user.entity'; import { FactionEntity } from '../faction/faction.entity'; import { NotificationGateway } from '../notifications/notifications.gateway'; -import { TickService } from './tick.service'; @Injectable() export class GameService { @@ -30,8 +29,6 @@ export class GameService { ObjectivePoint_HistoryEntity >, private notificationGateway: NotificationGateway, - - private tickService: TickService, ) {} // create a new game async createNewGame(personId: PersonEntity, gameData: newGameDTO) { @@ -164,13 +161,6 @@ export class GameService { updatedGame.state = 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/replay/replay.module.ts b/src/replay/replay.module.ts index 6df1133..98ea779 100644 --- a/src/replay/replay.module.ts +++ b/src/replay/replay.module.ts @@ -21,9 +21,8 @@ import { } from '../draw/coordinate.entity'; import { ScoreService } from '../score/score.service'; import { ScoreEntity } from '../score/score.entity'; -import { NotificationModule } from 'src/notifications/notifications.module'; -import { GameService } from 'src/game/game.service'; -import { TickService } from 'src/game/tick.service'; +import { NotificationModule } from '../notifications/notifications.module'; +import { GameService } from '../game/game.service'; ///////////////////////////////////////////////////////////////////// /// Replay /// @@ -54,7 +53,6 @@ import { TickService } from 'src/game/tick.service'; TrackingService, ScoreService, GameService, - TickService, ], }) export class ReplayModule {} diff --git a/src/tick/tick.module.ts b/src/tick/tick.module.ts new file mode 100644 index 0000000..b07fa5f --- /dev/null +++ b/src/tick/tick.module.ts @@ -0,0 +1,32 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { + GameEntity, + ObjectivePointEntity, + ObjectivePoint_HistoryEntity, +} from '../game/game.entity'; +import { TickService } from './tick.service'; +import { ScoreService } from '../score/score.service'; +import { FactionEntity } from '../faction/faction.entity'; +import { ScoreEntity } from '../score/score.entity'; +import { NotificationModule } from '../notifications/notifications.module'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([ + GameEntity, + ScoreEntity, + ObjectivePointEntity, + ObjectivePoint_HistoryEntity, + FactionEntity, + ]), + NotificationModule, + ], + providers: [TickService, ScoreService], +}) +export class TickModule { + constructor(private tickService: TickService) { + this.tickService.startTimer(); + } +} diff --git a/src/game/tick.service.ts b/src/tick/tick.service.ts similarity index 50% rename from src/game/tick.service.ts rename to src/tick/tick.service.ts index 0bd2c41..3a5f7cc 100644 --- a/src/game/tick.service.ts +++ b/src/tick/tick.service.ts @@ -1,11 +1,15 @@ import { Injectable, Logger } from '@nestjs/common'; import { ScoreService } from '../score/score.service'; -import { GameService } from './game.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, //private gameService: GameService, + private scoreService: ScoreService, + @InjectRepository(GameEntity) + private gameRepository: Repository<GameEntity>, ) { // whenever Tickservice is called, it will start ticktimer /* @@ -17,38 +21,30 @@ export class TickService { 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 STARTED games to dictionary - 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]; + // 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 = () => { + Tick = async _ => { this.logger.log('Ticking'); - if (this.ongoingGames != null) { - for (var game in this.ongoingGames) { - this.scoreService.scoreTick(game); - } - } + let games = await this.listGames(); + games.map(game => { + this.logger.log(game); + this.scoreService.scoreTick(game.id); + }); }; } -- GitLab