From 8e92a18ed6c34d3770c0907a55598d22d4ebde63 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20Syd=C3=A4nmaa?= <L4072@student.jamk.fi>
Date: Tue, 9 Jul 2019 08:12:47 +0300
Subject: [PATCH] getScore works

---
 src/score/score.controller.ts |  7 +++++++
 src/score/score.service.ts    | 25 +++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/score/score.controller.ts b/src/score/score.controller.ts
index 554e061..73fda22 100644
--- a/src/score/score.controller.ts
+++ b/src/score/score.controller.ts
@@ -26,4 +26,11 @@ export class ScoreController {
   async scoreTick(@Param('id') gameId: GameEntity) {
     return this.scoreService.scoreTick(gameId);
   }
+
+  // shows scores, :id is gameId 
+  @Get('get-score/:id')
+  @GameStates('STARTED')
+  async getScores(@Param('id') gameId: GameEntity) {
+    return this.scoreService.getScores(gameId);
+  }
 }
diff --git a/src/score/score.service.ts b/src/score/score.service.ts
index 9dad4e9..625ca7a 100644
--- a/src/score/score.service.ts
+++ b/src/score/score.service.ts
@@ -87,6 +87,31 @@ export class ScoreService {
       message: 'Scores added',
     };
   }
+
+  async getScores(gameId: GameEntity) {
+    // find games factions
+    const factions = await this.factionRepository.find({
+      where: {game: gameId,},
+      relations: ['game'],
+    });
+    let scores = [];
+    await Promise.all(
+      factions.map(async factionNow => {
+        let score = await this.scoreRepository.findOne({
+          where: {faction: factionNow},
+          relations: ['faction'],
+          order: {scoreTimeStamp: 'DESC'},
+        });
+        //if score was found, put info to scores array
+        if (score.faction) {
+            let index = await scores.findIndex(
+              i => i.faction === score.faction,
+            );
+          scores.push(score);
+        }
+      }))
+      return scores;
+  }
 } //
 
 // Hae kaikki Objective pointit
-- 
GitLab