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 84a8a97a30b53b094912d153ec9736c3fed1e59b..86bc61ef43c3255b83914cc08e2f1dcfae61bf90 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -1,4 +1,4 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { Injectable, HttpException, HttpStatus, Inject } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Not } from 'typeorm'; @@ -12,6 +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 { TickService } from './tick.service'; @Injectable() export class GameService { @@ -29,8 +30,9 @@ export class GameService { ObjectivePoint_HistoryEntity >, private notificationGateway: NotificationGateway, - ) {} + private tickService: TickService, + ) {} // create a new game async createNewGame(personId: PersonEntity, gameData: GameDTO) { // checks if a game with the same name exists already @@ -145,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/score/score.controller.ts b/src/score/score.controller.ts index dd26dd24daaec51422557339c8d1a5939ca58ffc..a9989a06b77d8ec08b1c84db9dbf24ad87fe9377 100644 --- a/src/score/score.controller.ts +++ b/src/score/score.controller.ts @@ -29,13 +29,6 @@ export class ScoreController { return this.scoreService.addScore(data, gameId); } - // temporary scoreTick path, :id is gameId - @Get('tick-score/:id') - @GameStates('STARTED') - async scoreTick(@Param('id') gameId: GameEntity) { - return this.scoreService.scoreTick(gameId); - } - // shows scores, :id is gameId @Get('get-score/:id') @UseInterceptors(ClassSerializerInterceptor) diff --git a/src/score/score.service.ts b/src/score/score.service.ts index a0ff983f527dba7bb6d51160743be20dc37b5e0b..7ef613d0f554bc61ffe9106e64845eaefe43428f 100644 --- a/src/score/score.service.ts +++ b/src/score/score.service.ts @@ -1,4 +1,10 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { + Injectable, + HttpException, + HttpStatus, + Logger, + OnModuleInit, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -111,10 +117,4 @@ export class ScoreService { ); return scores; } -} // - -// Hae kaikki Objective pointit -// aja map funktio pelin objective pointteihin -// jokaisella objective point ID:llä hae historystä -// relaatio, missä uusin timestamp -// katso uusimmista history entrystä omistaja +}