Skip to content
Snippets Groups Projects
Commit b88c2a0d authored by Ronnie Friman's avatar Ronnie Friman
Browse files

fixed gamedit bug with duplicate factions/flagboxes

parent 35885d31
No related branches found
No related tags found
3 merge requests!59Development to master,!36Development,!32Game state
......@@ -65,22 +65,27 @@ export class GameService {
HttpStatus.BAD_REQUEST,
);
}
// update game entry in db
// get factions that have been added previously
let factions = await this.factionRepository.find({ game: id });
// get flagboxes that have been added previously
let flagboxes = await this.objectivePointRepository.find({
game: id,
});
console.log(flagboxes);
// update game entry in db
const updatedGame = await this.gameRepository.create(gameData);
const gameId = await this.gameRepository.save(updatedGame);
// delete removed factions
// update/insert factions
// iterate factions if any were added
if (gameData.factions) {
const factionNames = gameData.factions.map(
({ factionName }) => factionName,
);
const factionIds = gameData.factions.map(({ factionId }) => factionId);
// delete all existing factions that are not in submitted data
factions.map(async faction => {
if (!factionNames.includes(faction.factionName)) {
if (!factionIds.includes(faction.factionId)) {
await this.factionRepository.delete(faction);
}
});
// create / update factions present in the submitted data
gameData.factions.map(async faction => {
let name = await this.factionRepository.create({
...faction,
......@@ -89,29 +94,29 @@ export class GameService {
await this.factionRepository.save(name);
});
} else {
// if no factions are present in data, delete all factions associated with the game
await this.factionRepository.delete({ game: id });
}
// get old flagboxes to deny duplicate entries
const flagboxes = await this.objectivePointRepository.find({
game: gameId,
});
const flagboxIds = flagboxes.map(
({ objectivePointDescription }) => objectivePointDescription,
);
// insert the flagboxes to db
if (gameData.objective_points) {
gameData.objective_points.map(async flagbox => {
if (
!Object.values(flagboxIds).includes(flagbox.objectivePointDescription)
) {
let newFlagbox = await this.objectivePointRepository.create({
...flagbox,
game: gameId,
});
await this.objectivePointRepository.insert(newFlagbox);
const flagboxIds = gameData.objective_points.map(
({ objectivePointId }) => objectivePointId,
);
flagboxes.map(async flagbox => {
if (!flagboxIds.includes(flagbox.objectivePointDescription)) {
await this.objectivePointRepository.delete(flagbox);
}
});
gameData.objective_points.map(async flagbox => {
let newFlagbox = await this.objectivePointRepository.create({
...flagbox,
game: gameId,
});
await this.objectivePointRepository.save(newFlagbox);
});
} else {
await this.objectivePointRepository.delete({ game: id });
}
// TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY
......
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