Skip to content
Snippets Groups Projects
Commit fa0faa53 authored by L4168's avatar L4168
Browse files

editgame faction bug wip

parent ecf9e6ea
No related branches found
No related tags found
3 merge requests!59Development to master,!36Development,!32Game state
......@@ -35,6 +35,7 @@ export class GameController {
@GameStates('CREATED')
@UsePipes(new ValidationPipe())
async editGame(@Param('id') id: string, @Body() body: GameDTO) {
body.id = id;
return this.gameservice.editGame(id, body);
}
......
......@@ -11,6 +11,7 @@ import {
Allow,
IsUUID,
IsIn,
IsOptional,
} from 'class-validator';
import { ObjectivePointEntity } from './game.entity';
......@@ -20,6 +21,8 @@ import { CenterDTO, NodeSettingsDTO } from './game.json.dto';
import { Type } from 'class-transformer';
export class GameDTO {
@IsOptional()
id: string;
@IsString()
@IsNotEmpty()
@Length(3, 30)
......
......@@ -55,32 +55,41 @@ export class GameService {
}
// edit already created game
async editGame(id: string, gameData: GameDTO) {
async editGame(id, gameData: GameDTO) {
// checks if a game with the same name exists already
if (
await this.gameRepository.findOne({ name: gameData.name, id: Not(id) })
) {
throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
throw new HttpException(
'Game with the same name already exists',
HttpStatus.BAD_REQUEST,
);
}
// update game entry in db
let factions = await this.factionRepository.find({ game: id });
const updatedGame = await this.gameRepository.create(gameData);
updatedGame['id'] = id;
const gameId = await this.gameRepository.save(updatedGame);
// get all the factions that are associated with the game to deny duplicate entries
const factions = await this.factionRepository.find({ game: gameId });
const factionNames = factions.map(({ factionName }) => factionName);
// add the factions to db
// delete removed factions
// update/insert factions
if (gameData.factions) {
gameData.factions.map(async faction => {
if (!Object.values(factionNames).includes(faction.factionName)) {
let name = await this.factionRepository.create({
...faction,
game: gameId,
});
await this.factionRepository.insert(name);
const factionNames = gameData.factions.map(
({ factionName }) => factionName,
);
const factionIds = gameData.factions.map(({ factionId }) => factionId);
factions.map(async faction => {
if (!factionNames.includes(faction.factionName)) {
await this.factionRepository.delete(faction);
}
});
gameData.factions.map(async faction => {
let name = await this.factionRepository.create({
...faction,
game: gameId,
});
await this.factionRepository.save(name);
});
} else {
await this.factionRepository.delete({ game: id });
}
// get old flagboxes to deny duplicate entries
......@@ -114,14 +123,18 @@ 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',
};
const updatedGame = await this.gameRepository.findOne({ id: game.id });
if (updatedGame) {
updatedGame.state = game.state;
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',
};
}
throw new HttpException("Game doesn't exist", HttpStatus.BAD_REQUEST);
}
async listFactions(game: GameEntity) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment