From 4aa0c47ca783f5e6dd9311a663d0ff413c41d3a3 Mon Sep 17 00:00:00 2001
From: Ronnie Friman <L4168@student.jamk.fi>
Date: Sat, 6 Jul 2019 16:10:19 +0300
Subject: [PATCH] added socket-notifs

---
 src/game/game.service.ts   | 6 ++++--
 src/score/score.service.ts | 4 +++-
 src/task/task.service.ts   | 4 ++--
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/game/game.service.ts b/src/game/game.service.ts
index 64ae650..5027c6e 100644
--- a/src/game/game.service.ts
+++ b/src/game/game.service.ts
@@ -149,7 +149,9 @@ export class GameService {
       updatedGame.state = game.state;
       await this.gameRepository.save(updatedGame);
       // notify players about game state change
-      this.notificationGateway.server.emit(game.id, 'event update');
+      this.notificationGateway.server.emit(game.id, {
+        type: 'gamestate-update',
+      });
       return {
         code: 200,
         message: 'State was updated',
@@ -217,7 +219,7 @@ export class GameService {
     });
     await this.objectivePoint_HistoryRepository.insert(eventUpdate);
     // send flagbox event to flagbox subscribers
-    this.notificationGateway.server.emit('flagbox', 'event update');
+    this.notificationGateway.server.emit(gameId, { type: 'flagbox-event' });
     return {
       code: 201,
       message: 'OK',
diff --git a/src/score/score.service.ts b/src/score/score.service.ts
index 0ecea91..9dad4e9 100644
--- a/src/score/score.service.ts
+++ b/src/score/score.service.ts
@@ -10,7 +10,7 @@ import {
   GameEntity,
 } from '../game/game.entity';
 import { ScoreEntity } from './score.entity';
-import { map } from 'rxjs/operators';
+import { NotificationGateway } from '../notifications/notifications.gateway';
 
 @Injectable()
 export class ScoreService {
@@ -23,6 +23,7 @@ export class ScoreService {
     private flagHistoryRepository: Repository<ObjectivePoint_HistoryEntity>,
     @InjectRepository(FactionEntity)
     private factionRepository: Repository<FactionEntity>,
+    private notificationGateway: NotificationGateway,
   ) {}
 
   async addScore(scoreData: ScoreDTO, gameId: GameEntity) {
@@ -80,6 +81,7 @@ export class ScoreService {
     scoreData.map(async data => {
       await this.addScore(data, gameId);
     });
+    this.notificationGateway.server.emit(gameId, { type: 'score-update' });
     return {
       code: 200,
       message: 'Scores added',
diff --git a/src/task/task.service.ts b/src/task/task.service.ts
index cf4fe3f..eedef60 100644
--- a/src/task/task.service.ts
+++ b/src/task/task.service.ts
@@ -36,8 +36,8 @@ export class TaskService {
     // 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() : 'global',
-      'new task',
+      task.faction != null ? task.faction.toString() : task.taskGame.toString(),
+      { type: 'task-update' },
     );
     return {
       code: 201,
-- 
GitLab