From 21cbd659143dfb48a9228ff71b6dbebd2e03366d Mon Sep 17 00:00:00 2001
From: L4168 <L4168@student.jamk.fi>
Date: Thu, 4 Jul 2019 11:01:26 +0300
Subject: [PATCH] added path&service for modifying gamestate

---
 src/game/game.controller.ts | 12 ++++++++++--
 src/game/game.service.ts    | 14 +++++++++++++-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts
index 015baf9..f18eada 100644
--- a/src/game/game.controller.ts
+++ b/src/game/game.controller.ts
@@ -14,9 +14,9 @@ import {
 import { GameService } from './game.service';
 import { AuthGuard } from '../shared/auth.guard';
 import { User } from '../user/user.decorator';
-import { GameDTO, FlagboxEventDTO } from './game.dto';
+import { GameDTO, FlagboxEventDTO, GameStateDTO } from './game.dto';
 import { ValidationPipe } from '../shared/validation.pipe';
-import { Roles } from '../shared/roles.decorator';
+import { Roles, GameStates } from '../shared/guard.decorator';
 import { GameEntity } from './game.entity';
 
 @Controller('game')
@@ -32,11 +32,19 @@ export class GameController {
 
   @Put('edit/:id')
   @Roles('admin')
+  @GameStates('CREATED')
   @UsePipes(new ValidationPipe())
   async editGame(@Param('id') id: string, @Body() body: GameDTO) {
     return this.gameservice.editGame(id, body);
   }
 
+  @Put('edit-state/:id')
+  @Roles('admin')
+  @UsePipes(new ValidationPipe())
+  async updateGameState(@Param('id') id: string, @Body() body: GameStateDTO) {
+    return this.gameservice.updateGameStatus(body);
+  }
+
   @Get('listgames')
   async listGames() {
     return this.gameservice.listGames();
diff --git a/src/game/game.service.ts b/src/game/game.service.ts
index 573bb22..e6b3d60 100644
--- a/src/game/game.service.ts
+++ b/src/game/game.service.ts
@@ -8,7 +8,7 @@ import {
   ObjectivePointEntity,
   ObjectivePoint_HistoryEntity,
 } from './game.entity';
-import { GameDTO, FlagboxEventDTO } from './game.dto';
+import { GameDTO, FlagboxEventDTO, GameStateDTO } from './game.dto';
 import { PersonEntity } from '../user/user.entity';
 import { FactionEntity } from '../faction/faction.entity';
 import { NotificationGateway } from '../notifications/notifications.gateway';
@@ -39,6 +39,7 @@ export class GameService {
     }
     // else add the game to the database
     const game = await this.gameRepository.create(gameData);
+    game.state = 'CREATED';
     await this.gameRepository.insert(game);
     // add gamePerson with role admin to the game
     const gamePerson = await this.game_PersonRepository.create({
@@ -112,6 +113,17 @@ export class GameService {
     };
   }
 
+  async updateGameStatus(game: GameStateDTO) {
+    const updatedGame = await this.gameRepository.create(game);
+    await this.gameRepository.save(updatedGame);
+    // notify players about game state change
+    this.notificationGateway.server.emit(game.id, 'event update');
+    return {
+      code: 200,
+      message: 'State was updated',
+    };
+  }
+
   async listFactions(game: GameEntity) {
     return this.factionRepository.find({ game });
   }
-- 
GitLab