diff --git a/src/app.module.ts b/src/app.module.ts index 9b6831233c530b9bb6fe89adfd79eaec4a387633..05685cd88382fd6e6e29caadfbab83b0c658d953 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 3fca76600bb3ca192b3e3e70e290a462dc72d0af..93068318edb8a99755c8cd44adaa795573888981 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 e119e492e4893267e3e1e3e671dd190abdd1db57..b30424ced696027a22d255e8618c90ad40209f29 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'; @@ -41,6 +40,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 e4bf8fac3275e3360b9a3eb658dcacd9f4db84ae..1567d30a1c5aa751de2ef9b0dff4d18af7af94d4 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 bea3525dbe2bf79df1b4cff15613e60abea13781..961e599e4cb9aca8a27dac015b83edb386e41e64 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 /// @@ -55,7 +54,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 0000000000000000000000000000000000000000..b07fa5fa1eca4c4357d6e551983edf1b5c3aebe6 --- /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 0bd2c413ff8cad00ee6d5d0e5fd97506e0c8e35e..3a5f7cc1dac03303eaf0febcba0cd603fdeaf45a 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); + }); }; }