diff --git a/package-lock.json b/package-lock.json index 05c4c11f6797b7d67226fa03b30c6327b6fe9c69..c6bfaf63ff60d9ba6ef0d81078edfef578544910 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3146,12 +3146,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3166,17 +3168,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3293,7 +3298,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3305,6 +3311,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3319,6 +3326,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3326,12 +3334,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -3350,6 +3360,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3430,7 +3441,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3442,6 +3454,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3563,6 +3576,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts index 891421d3a41315549a9e589ef87ce1f22a9abc84..b0a99df8f152d4572ba528cc8149a8d0021beac7 100644 --- a/src/game/faction.entity.ts +++ b/src/game/faction.entity.ts @@ -1,95 +1,94 @@ import { - Entity, - Column, - PrimaryGeneratedColumn, - OneToMany, - ManyToOne, - Timestamp, - } from 'typeorm'; - import { GameEntity } from './game.entity'; - import { Game_PersonEntity } from './game.entity'; - import { MapDrawingEntity } from './coordinate.entity'; - - //Faction, PowerUp, Faction_powerUp, FP_History, Score, Task - - @Entity('Faction') - export class FactionEntity { - @PrimaryGeneratedColumn('uuid') factionId: string; - @Column({ type: 'text', unique: true }) factionName: string; - @Column({ type: 'text' }) factionPassword: string; - @Column({ type: 'float' }) multiplier: number; - - @OneToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.faction) - mapDrawings: MapDrawingEntity[]; - @OneToMany(type => ScoreEntity, scores => scores.faction) - scores: ScoreEntity[]; - @OneToMany(type => PowerUpEntity, powerUps => powerUps.factions) - powerUps: Faction_PowerUpEntity[]; - @OneToMany(type => TaskEntity, tasks => tasks.faction) - tasks: TaskEntity[]; - @OneToMany(type => Game_PersonEntity, game_persons => game_persons.faction) - game_persons: Game_PersonEntity[]; - @ManyToOne(type => GameEntity, game => game.factions) - game: GameEntity; - } - - @Entity('PowerUp') - export class PowerUpEntity { - @PrimaryGeneratedColumn('uuid') powerUpId: string; - @Column({ type: 'text' }) powerUpName: string; - @Column({ type: 'text' }) powerUpDescription: string; - @Column({ type: 'int' }) amount: number; - @Column({ type: 'time' }) cooldown: string; - - @OneToMany(type => FactionEntity, factions => factions.powerUps) - factions: Faction_PowerUpEntity[]; - } - - @Entity('Faction_PowerUp') - export class Faction_PowerUpEntity { - @PrimaryGeneratedColumn('uuid') faction_powerUpId: string; - - @ManyToOne(type => FactionEntity, faction => faction.powerUps) - faction: FactionEntity; - @ManyToOne(type => PowerUpEntity, powerUp => powerUp.factions) - powerUp: PowerUpEntity; - @OneToMany(type => FP_HistoryEntity, histories => histories.faction_PowerUp) - histories: FP_HistoryEntity[]; - } - - @Entity('FP_History') - export class FP_HistoryEntity { - @PrimaryGeneratedColumn('uuid') historyId: string; - @Column({ type: 'timestamp' }) historyTimeStamp: Timestamp; - - @ManyToOne( - type => Faction_PowerUpEntity, - faction_PowerUp => faction_PowerUp.histories, - ) - faction_PowerUp: Faction_PowerUpEntity; - } - - @Entity('Score') - export class ScoreEntity { - @PrimaryGeneratedColumn('uuid') scoreId: string; - @Column({ type: 'float' }) score: number; - @Column({ type: 'timestamp' }) scoreTimeStamp: Timestamp; - - @ManyToOne(type => FactionEntity, factionName => factionName.scores) - faction: FactionEntity; - } - - @Entity('Task') - export class TaskEntity { - @PrimaryGeneratedColumn('uuid') taskId: string; - @Column({ type: 'text' }) taskName: string; - @Column({ type: 'text' }) taskDescription: string; - @Column({ type: 'text' }) taskWinner: string; - @Column({ type: 'bool' }) taskIsActive: boolean; - - @ManyToOne(type => FactionEntity, faction => faction.tasks) - faction: FactionEntity; -/* @ManyToOne(type => PersonEntity, person => person.tasks) + Entity, + Column, + PrimaryGeneratedColumn, + OneToMany, + ManyToOne, + Timestamp, +} from 'typeorm'; +import { GameEntity } from './game.entity'; +import { Game_PersonEntity } from './game.entity'; +import { MapDrawingEntity } from './coordinate.entity'; + +//Faction, PowerUp, Faction_powerUp, FP_History, Score, Task + +@Entity('Faction') +export class FactionEntity { + @PrimaryGeneratedColumn('uuid') factionId: string; + @Column('text') factionName: string; + @Column({ type: 'text' }) factionPassword: string; + @Column({ type: 'float' }) multiplier: number; + + @OneToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.faction) + mapDrawings: MapDrawingEntity[]; + @OneToMany(type => ScoreEntity, scores => scores.faction) + scores: ScoreEntity[]; + @OneToMany(type => PowerUpEntity, powerUps => powerUps.factions) + powerUps: Faction_PowerUpEntity[]; + @OneToMany(type => TaskEntity, tasks => tasks.faction) + tasks: TaskEntity[]; + @OneToMany(type => Game_PersonEntity, game_persons => game_persons.faction) + game_persons: Game_PersonEntity[]; + @ManyToOne(type => GameEntity, game => game.factions) + game: GameEntity; +} + +@Entity('PowerUp') +export class PowerUpEntity { + @PrimaryGeneratedColumn('uuid') powerUpId: string; + @Column({ type: 'text' }) powerUpName: string; + @Column({ type: 'text' }) powerUpDescription: string; + @Column({ type: 'int' }) amount: number; + @Column({ type: 'time' }) cooldown: string; + + @OneToMany(type => FactionEntity, factions => factions.powerUps) + factions: Faction_PowerUpEntity[]; +} + +@Entity('Faction_PowerUp') +export class Faction_PowerUpEntity { + @PrimaryGeneratedColumn('uuid') faction_powerUpId: string; + + @ManyToOne(type => FactionEntity, faction => faction.powerUps) + faction: FactionEntity; + @ManyToOne(type => PowerUpEntity, powerUp => powerUp.factions) + powerUp: PowerUpEntity; + @OneToMany(type => FP_HistoryEntity, histories => histories.faction_PowerUp) + histories: FP_HistoryEntity[]; +} + +@Entity('FP_History') +export class FP_HistoryEntity { + @PrimaryGeneratedColumn('uuid') historyId: string; + @Column({ type: 'timestamp' }) historyTimeStamp: Timestamp; + + @ManyToOne( + type => Faction_PowerUpEntity, + faction_PowerUp => faction_PowerUp.histories, + ) + faction_PowerUp: Faction_PowerUpEntity; +} + +@Entity('Score') +export class ScoreEntity { + @PrimaryGeneratedColumn('uuid') scoreId: string; + @Column({ type: 'float' }) score: number; + @Column({ type: 'timestamp' }) scoreTimeStamp: Timestamp; + + @ManyToOne(type => FactionEntity, factionName => factionName.scores) + faction: FactionEntity; +} + +@Entity('Task') +export class TaskEntity { + @PrimaryGeneratedColumn('uuid') taskId: string; + @Column({ type: 'text' }) taskName: string; + @Column({ type: 'text' }) taskDescription: string; + @Column({ type: 'text' }) taskWinner: string; + @Column({ type: 'bool' }) taskIsActive: boolean; + + @ManyToOne(type => FactionEntity, faction => faction.tasks) + faction: FactionEntity; + /* @ManyToOne(type => PersonEntity, person => person.tasks) person: PersonEntity; */ - } - \ No newline at end of file +} diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index a5d43f5d624a7ff690d2a5e6fa6e2db4d45eaed1..ea5c2e9dcdc1156eceb7c73791db06448443d1a5 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -8,19 +8,16 @@ import { Max, IsArray, IsJSON, + IsDateString, } from 'class-validator'; import { Timestamp } from 'typeorm'; +import { ObjectivePointEntity } from './game.entity'; export class GameDTO { @IsString() @IsNotEmpty() @Length(3, 30) name: string; - - @IsString() - gameDescription?: string; - - @IsDate() @IsNotEmpty() @Length(1, 255) desc: string; @@ -40,24 +37,16 @@ export class GameDTO { // custom validation for array length (arr>min, arr<max) //@Validate(ArrayLength, [4, 8]) factions?: FactionDTO[]; - - @IsArray() - @IsNotEmpty() - objectivePoint?: ObjectivePointDTO[]; - - @IsJSON() - @IsNotEmpty() - mapCoordinates: JSON; } export class FactionDTO { @IsString() @IsNotEmpty() @Length(2, 15) - name: string; - id: string; + factionName: string; + factionPassword: string; + multiplier: number; game: GameDTO; - password: string; } export class GameGroupDTO { diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts index 33b0a3acb8961a30dfad185fe42ca7903f2b2a47..0ef442c04c6d4b2cf6b6f6089c045fb1b88aad51 100644 --- a/src/game/game.entity.ts +++ b/src/game/game.entity.ts @@ -36,7 +36,6 @@ export class GameEntity { ) objective_points: ObjectivePointEntity[]; - gameObject() { const { id, name } = this; return { id, name }; @@ -55,14 +54,14 @@ export class Game_PersonEntity { @ManyToOne(type => PersonEntity, person => person.game_persons) person: PersonEntity; @OneToOne(type => GameGroupEntity, group => group.leader, { - onDelete: 'CASCADE' + onDelete: 'CASCADE', }) - @JoinColumn({name: 'leaderGroup'}) + @JoinColumn({ name: 'leaderGroup' }) leaderGroup: GameGroupEntity; @ManyToOne(type => GameGroupEntity, group => group.players, { - onDelete: 'CASCADE' + onDelete: 'CASCADE', }) - @JoinColumn({name: 'group'}) + @JoinColumn({ name: 'group' }) group: GameGroupEntity; } diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 8dbc824e5d91b2ec9d9e1da54cbdd90a53e1ea96..ec89d51b81276bc07ef1a282b2f4329428013049 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -54,42 +54,43 @@ export class GameService { // edit already created game async editGame(id: string, gameData: GameDTO) { - // checks if a game with the same name exists already - const { name } = gameData; - if (await this.gameRepository.findOne({ name: name, id: Not(id) })) { - throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST); - } - // update game entry in db - const updatedGame = await this.gameRepository.create({ - ...gameData, - factions: gameData.factions, - }); - 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({ - select: ['factionName'], - where: { game: gameId }, - }); - const factionNames = factions.map(({ factionName }) => factionName); - - // add the factions to db - if (gameData.factions) { - gameData.factions.map(async faction => { - if (!Object.values(factionNames).includes(faction.name)) { - let name = await this.factionRepository.create({ - ...faction, - game: gameId, - }); - await this.factionRepository.insert(name); - } + try { + // checks if a game with the same name exists already + const { name } = gameData; + if (await this.gameRepository.findOne({ name: name, id: Not(id) })) { + throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST); + } + // update game entry in db + const updatedGame = await this.gameRepository.create({ + ...gameData, + factions: null, }); - } + 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(gameId); + const factionNames = factions.map(({ factionName }) => factionName); + console.log(factionNames); + // add the factions to db + 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); + } + }); + } - return { - message: 'Game updated', - }; + return { + message: 'Game updated', + }; + } catch (error) { + return error; + } } async deleteGame(id) {