From 02eeccde2614ffea3c62a8c6b0da1a569f3e2e9a Mon Sep 17 00:00:00 2001
From: Ronnie Friman <L4168@student.jamk.fi>
Date: Sun, 21 Jul 2019 08:48:57 +0300
Subject: [PATCH] service returns flagbox-events

---
 src/game/game.service.ts | 64 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 59 insertions(+), 5 deletions(-)

diff --git a/src/game/game.service.ts b/src/game/game.service.ts
index 86bc61e..57d0b74 100644
--- a/src/game/game.service.ts
+++ b/src/game/game.service.ts
@@ -83,6 +83,14 @@ export class GameService {
         HttpStatus.BAD_REQUEST,
       );
     }
+    // check that there's location data for each added objective point
+    gameData.objective_points.forEach(obj => {
+      if (!obj['data'])
+        throw new HttpException(
+          'Objective Point error. Add location for each Objective Point.',
+          HttpStatus.BAD_REQUEST,
+        );
+    });
 
     // get factions that have been added previously
     let factions = await this.factionRepository.find({ game: id });
@@ -121,8 +129,10 @@ export class GameService {
         ({ objectivePointId }) => objectivePointId,
       );
       flagboxes.map(async flagbox => {
-        if (!flagboxIds.includes(flagbox.objectivePointDescription)) {
-          await this.objectivePointRepository.delete(flagbox);
+        if (!flagboxIds.includes(flagbox.objectivePointId)) {
+          await this.objectivePointRepository.delete({
+            objectivePointId: flagbox.objectivePointId,
+          });
         }
       });
       gameData.objective_points.map(async flagbox => {
@@ -136,8 +146,6 @@ export class GameService {
       await this.objectivePointRepository.delete({ game: id });
     }
 
-    // TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY
-
     return {
       message: 'Game updated',
     };
@@ -209,7 +217,7 @@ export class GameService {
   }
 
   // returns information about a game identified by id
-  async returnGameInfo(id: string) {
+  async returnGameInfo(id) {
     const game = await this.gameRepository.findOne({
       where: { id: id },
       relations: ['factions', 'objective_points'],
@@ -221,6 +229,52 @@ export class GameService {
     return game;
   }
 
+  // returns information about game's flagboxes and their most recent event
+  async returnObjectivePointInfo(gameId) {
+    const info = await this.objectivePointRepository.find({
+      where: { game: gameId },
+      relations: ['history', 'history.owner', 'history.capture'],
+    });
+    let response = await Promise.all(
+      info.map(async obj => {
+        let history = obj.history.pop();
+        return await {
+          objectivePointId: obj.objectivePointId,
+          objectivePointDescription: obj.objectivePointDescription,
+          objectivePointMultiplier: obj.objectivePointMultiplier,
+          action: {
+            status: history.action,
+            message: {
+              0: 'No capture ongoing',
+              1: `Captured by ${
+                history.owner ? history.owner.factionName : 'neutral'
+              }`,
+              2: `Being captured by ${
+                history.capture ? history.capture.factionName : 'neutral'
+              }`,
+            }[history.action],
+          },
+          owner: await this.infoHelper(history.owner),
+          capture: await this.infoHelper(history.capture),
+          data: obj.data,
+        };
+      }),
+    );
+    return response;
+  }
+
+  private async infoHelper(obj) {
+    return (await obj)
+      ? {
+          factionName: obj.factionName,
+          colour: obj.colour,
+        }
+      : {
+          factionName: 'neutral',
+          colour: '#000000',
+        };
+  }
+
   // returns flagbox settings
   async flagboxQuery(gameId) {
     const game = await this.gameRepository.findOne({ id: gameId });
-- 
GitLab