From 65a0e7c7c314e74621a6068f5b0b7fba0e6fa1d6 Mon Sep 17 00:00:00 2001
From: Samuli Virtapohja <l4721@student.jamk.fi>
Date: Thu, 18 Jul 2019 17:03:21 +0300
Subject: [PATCH] ticking works

---
 src/game/game.service.ts   |  3 +--
 src/main.ts                |  2 +-
 src/score/score.service.ts | 47 +++++++++++++++++++++++++++++++-------
 3 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/src/game/game.service.ts b/src/game/game.service.ts
index 84a8a97..e4288a4 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';
 
@@ -30,7 +30,6 @@ export class GameService {
     >,
     private notificationGateway: NotificationGateway,
   ) {}
-
   // create a new game
   async createNewGame(personId: PersonEntity, gameData: GameDTO) {
     // checks if a game with the same name exists already
diff --git a/src/main.ts b/src/main.ts
index ebd53e5..dec8e6f 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);
+  const app = await NestFactory.create(AppModule, { logger: console });
   // 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 90716c4..1a62576 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';
 
@@ -24,7 +30,10 @@ export class ScoreService {
     @InjectRepository(FactionEntity)
     private factionRepository: Repository<FactionEntity>,
     private notificationGateway: NotificationGateway,
-  ) {}
+  ) {
+    // initialize timer when service is loaded
+    this.startTimer();
+  }
 
   async addScore(scoreData: ScoreDTO, gameId: GameEntity) {
     // check if faction exists
@@ -110,10 +119,32 @@ 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
+  // 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);
+      }
+    }
+  };
+}
-- 
GitLab