diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 015baf9673563cd8732ce15493e80e23c19e0e6b..f18eadaf4c7e508236283dd19a128f30abcf0ce7 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 573bb2255ba2d65f828ad3900ff064e1fc471d13..e6b3d603f18ea543d9e6c031f9b65f7712f7b69c 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 }); }