From cee0c00c261c76dade7a84beffe6e9c2e959403c Mon Sep 17 00:00:00 2001
From: Ronnie Friman <L4168@student.jamk.fi>
Date: Sun, 21 Jul 2019 16:24:54 +0300
Subject: [PATCH] update get playerlocations

---
 src/tracking/tracking.module.ts  |  8 ++++-
 src/tracking/tracking.service.ts | 54 ++++++++++++++++++++------------
 2 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/src/tracking/tracking.module.ts b/src/tracking/tracking.module.ts
index b2a7312..98079e9 100644
--- a/src/tracking/tracking.module.ts
+++ b/src/tracking/tracking.module.ts
@@ -6,6 +6,7 @@ import { TrackingService } from './tracking.service';
 import { TrackingEntity } from './tracking.entity';
 import { Game_PersonEntity } from '../game/game.entity';
 import { PersonEntity } from '../user/user.entity';
+import { FactionEntity } from '../faction/faction.entity';
 
 /////////////////////////////////////////////////////////////////////
 /// Tracking                                                      ///
@@ -13,7 +14,12 @@ import { PersonEntity } from '../user/user.entity';
 /////////////////////////////////////////////////////////////////////
 @Module({
   imports: [
-    TypeOrmModule.forFeature([TrackingEntity, Game_PersonEntity, PersonEntity]),
+    TypeOrmModule.forFeature([
+      TrackingEntity,
+      Game_PersonEntity,
+      PersonEntity,
+      FactionEntity,
+    ]),
   ],
   controllers: [TrackingController],
   providers: [TrackingService],
diff --git a/src/tracking/tracking.service.ts b/src/tracking/tracking.service.ts
index b366784..c40d5f7 100644
--- a/src/tracking/tracking.service.ts
+++ b/src/tracking/tracking.service.ts
@@ -5,6 +5,7 @@ import { Repository } from 'typeorm';
 import { Game_PersonEntity } from '../game/game.entity';
 import { TrackingEntity } from './tracking.entity';
 import { GeoDTO } from './geo.dto';
+import { FactionEntity } from 'src/faction/faction.entity';
 
 @Injectable()
 export class TrackingService {
@@ -13,6 +14,8 @@ export class TrackingService {
     private trackingrepository: Repository<TrackingEntity>,
     @InjectRepository(Game_PersonEntity)
     private gamepersonrepository: Repository<Game_PersonEntity>,
+    @InjectRepository(FactionEntity)
+    private factionRepository: Repository<FactionEntity>,
   ) {}
 
   private icons = {
@@ -59,34 +62,45 @@ export class TrackingService {
 
   // get player data while game is running
   async getPlayers(gameperson, gameId) {
-    let playerdata;
+    let playerdata = [];
     // get playerdata
     if (gameperson.faction) {
-      playerdata = await this.trackingrepository.find({
-        where: { faction: gameperson.faction },
-        relations: ['faction', 'gamepersonId'],
-      });
+      // create an array of the response as frontend maps the response
+      // to create different clusters for factions
+      playerdata.push(
+        await this.trackingrepository.find({
+          where: { faction: gameperson.faction },
+          relations: ['faction', 'gamepersonId'],
+        }),
+      );
     } else {
-      playerdata = await this.trackingrepository.find({
-        where: { game: gameId },
-        relations: ['faction', 'gamepersonId'],
-      });
+      let factions = await this.factionRepository.find({ game: gameId });
+      playerdata = await Promise.all(
+        factions.map(async faction => {
+          return await this.trackingrepository.find({
+            where: { faction: faction.factionId },
+            relations: ['faction', 'gamepersonId'],
+          });
+        }),
+      );
     }
-
     // parse data
     const currentdata = await Promise.all(
-      playerdata.map(async player => {
-        return {
-          gamepersonId: player['gamepersonId']['gamepersonId'],
-          gamepersonRole: player['gamepersonId']['role'],
-          factionId: player['faction']['factionId'],
-          factionColour: player['faction']['colour'],
-          icon: player['icon'],
-          coordinates: player['data'].pop(),
-        };
+      await playerdata.map(async faction => {
+        return await Promise.all(
+          faction.map(async player => {
+            return await {
+              gamepersonId: player['gamepersonId']['gamepersonId'],
+              gamepersonRole: player['gamepersonId']['role'],
+              factionId: player['faction']['factionId'],
+              factionColour: player['faction']['colour'],
+              icon: player['icon'],
+              coordinates: player['data'].pop(),
+            };
+          }),
+        );
       }),
     );
-
     return currentdata;
   }
 
-- 
GitLab