From c522aefcd5125c578f4f08414f258b29a58050af Mon Sep 17 00:00:00 2001
From: Samuli Virtapohja <l4721@student.jamk.fi>
Date: Wed, 24 Jul 2019 13:39:34 +0300
Subject: [PATCH] replay audited

---
 src/game/game.controller.ts     |  8 +++++++-
 src/game/tick.service.ts        |  2 --
 src/replay/replay.controller.ts |  6 ++++++
 src/replay/replay.service.ts    | 22 ++++++++--------------
 src/task/task.service.ts        | 14 ++++++++++----
 5 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts
index b79699c..95df172 100644
--- a/src/game/game.controller.ts
+++ b/src/game/game.controller.ts
@@ -24,6 +24,7 @@ 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                                                ///
@@ -36,7 +37,12 @@ import { GameEntity } from './game.entity';
 
 @Controller('game')
 export class GameController {
-  constructor(private gameservice: GameService) {}
+  constructor(
+    private gameservice: GameService,
+    private tickservice: TickService,
+  ) {
+    this.tickservice.startTimer();
+  }
 
   @Post('new')
   @UseGuards(new AuthGuard())
diff --git a/src/game/tick.service.ts b/src/game/tick.service.ts
index 8417fe8..a12c452 100644
--- a/src/game/tick.service.ts
+++ b/src/game/tick.service.ts
@@ -9,8 +9,6 @@ export class TickService {
     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');
diff --git a/src/replay/replay.controller.ts b/src/replay/replay.controller.ts
index 1b42439..a2634a4 100644
--- a/src/replay/replay.controller.ts
+++ b/src/replay/replay.controller.ts
@@ -7,6 +7,11 @@ import {
   ClassSerializerInterceptor,
 } from '@nestjs/common';
 import { ReplayService } from './replay.service';
+import { Roles } from 'src/shared/guard.decorator';
+
+/*
+POST mockdata is mainly used for development, it can be removed from the code when needed, remember to remove service & test relations.
+*/
 
 @Controller('replay')
 export class ReplayController {
@@ -21,6 +26,7 @@ export class ReplayController {
 
   // gets mockdata for specified Game
   @Post('mockdata/:id')
+  @Roles('admin')
   async mockData(@Param('id') gameId) {
     return this.replayservice.mockdata(gameId);
   }
diff --git a/src/replay/replay.service.ts b/src/replay/replay.service.ts
index 330029b..be42349 100644
--- a/src/replay/replay.service.ts
+++ b/src/replay/replay.service.ts
@@ -46,17 +46,13 @@ export class ReplayService {
   ) {}
 
   async replayData(gameId) {
-    //
     // this block returns game's initial location
-    //
     let gameObj = await this.gameRepository.findOne({
       where: { id: gameId },
       select: ['center'],
     });
     let gamelocation = [gameObj.center.lat, gameObj.center.lng];
-    //
     // this block returns all player data from the game
-    //
     let playerdata = await this.trackingRepository.find({
       where: { game: gameId },
       relations: ['faction', 'gamepersonId', 'gamepersonId.person'],
@@ -75,9 +71,9 @@ export class ReplayService {
         return player['data'];
       }),
     );
-    //
+
     // this block returns all faction data from the game
-    //
+
     let factions = await this.factionRepository.find({ game: gameId });
     let currentFactions = factions.map(faction => {
       return {
@@ -87,10 +83,8 @@ export class ReplayService {
         active: true,
       };
     });
-    let factionIds = factions.map(faction => faction.factionId);
-    //
+
     // this block returns all score data from the game
-    //
     let currentScore = [];
     await Promise.all(
       factions.map(async faction => {
@@ -109,9 +103,9 @@ export class ReplayService {
         );
       }),
     );
-    //
+
     // this block returns all map drawings from the game
-    //
+
     let refs = await this.mapdrawingRepository.find({
       where: { gameId: gameId },
       select: ['mapDrawingId'],
@@ -124,9 +118,9 @@ export class ReplayService {
         });
       }),
     );
-    //
+
     // this function returns all flagbox-events from the game
-    //
+
     let objectivepoints = await this.returnObjectivePointInfo(gameId);
 
     return {
@@ -204,7 +198,7 @@ export class ReplayService {
   // insert x amont of flagbox events
   // insert x amount of score ticks for score mockdata
   // use the game's initial geojson to draw game area
-  //
+
   async mockdata(gameId) {
     // initial settings for mockdata
     // set the x amount of users to be created
diff --git a/src/task/task.service.ts b/src/task/task.service.ts
index 478a6c1..d09eb1c 100644
--- a/src/task/task.service.ts
+++ b/src/task/task.service.ts
@@ -32,10 +32,7 @@ export class TaskService {
     await this.taskRepository.insert(createdTask);
     // notify subscribers about a new task
     // if faction was set it notifies only faction members, else everyone
-    this.notificationGateway.server.emit(
-      task.faction != null ? task.faction.toString() : task.taskGame.toString(),
-      { type: 'task-update' },
-    );
+    await this.triggerTasks(task);
     return {
       message: 'Task added',
     };
@@ -59,15 +56,24 @@ export class TaskService {
     task.taskWinner = data.taskWinner;
     task.taskIsActive = false;
     await this.taskRepository.save(task);
+    await this.triggerTasks(task);
     return {
       message: 'Task updated and closed',
     };
   }
 
+  private async triggerTasks(task) {
+    this.notificationGateway.server.emit(
+      task.faction != null ? task.faction.toString() : task.taskGame.toString(),
+      { type: 'task-update' },
+    );
+  }
+
   async deleteTask(data: DeleteTaskDTO) {
     const task = await this.taskRepository.findOne({ taskId: data.taskId });
     if (task) {
       await this.taskRepository.delete({ taskId: task.taskId });
+      await this.triggerTasks(task);
       return {
         message: 'Task deleted',
       };
-- 
GitLab