From 54ba28391004c3ebf999389555be2affa29c1311 Mon Sep 17 00:00:00 2001
From: L4168 <L4168@student.jamk.fi>
Date: Thu, 20 Jun 2019 09:22:26 +0300
Subject: [PATCH] refactoring database

---
 src/game/coordinate.entity.ts      | 30 ++++++++++
 src/game/faction.entity.ts         | 95 ++++++++++++++++++++++++++++++
 src/game/game.entity.ts            | 67 ++++++++++++++-------
 src/game/game.module.ts            |  3 +-
 src/game/group.entity.ts           | 10 +++-
 src/mapmarkers/mapmarker.entity.ts | 43 ++++++++------
 6 files changed, 204 insertions(+), 44 deletions(-)
 create mode 100644 src/game/coordinate.entity.ts
 create mode 100644 src/game/faction.entity.ts

diff --git a/src/game/coordinate.entity.ts b/src/game/coordinate.entity.ts
new file mode 100644
index 0000000..65ad654
--- /dev/null
+++ b/src/game/coordinate.entity.ts
@@ -0,0 +1,30 @@
+import {
+    Entity,
+    Column,
+    PrimaryGeneratedColumn,
+    OneToMany,
+    ManyToOne,
+    PrimaryColumn,
+    Timestamp,
+  } from 'typeorm';
+  import {
+    Game_PersonEntity,
+    ObjectivePointEntity,
+    GameEntity,
+  } from './game.entity';
+  import { FactionEntity } from './faction.entity';
+  
+  @Entity('MapDrawing')
+  export class MapDrawingEntity {
+    @PrimaryGeneratedColumn('uuid') mapDrawingId: string;
+    @Column({ type: 'bool' }) drawingIsActive: boolean;
+    @Column({ type: 'time' }) drawingValidTill: string;
+  
+    @Column({ type: 'json', nullable: true }) data: JSON;
+  
+    @ManyToOne(type => FactionEntity, faction => faction.mapDrawings)
+    faction: FactionEntity;
+    @ManyToOne(type => GameEntity, gameEntity => gameEntity.id)
+    gameId: GameEntity;
+  }
+  
\ No newline at end of file
diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts
new file mode 100644
index 0000000..891421d
--- /dev/null
+++ b/src/game/faction.entity.ts
@@ -0,0 +1,95 @@
+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)
+    person: PersonEntity; */
+  }
+  
\ No newline at end of file
diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts
index 08c46a6..33b0a3a 100644
--- a/src/game/game.entity.ts
+++ b/src/game/game.entity.ts
@@ -6,10 +6,13 @@ import {
   OneToMany,
   Timestamp,
   OneToOne,
+  JoinColumn,
 } from 'typeorm';
 
 import { PersonEntity } from '../user/user.entity';
 import { GameGroupEntity } from './group.entity';
+import { FactionEntity } from './faction.entity';
+import { MapDrawingEntity } from './coordinate.entity';
 
 // table that stores all created games
 @Entity('Game')
@@ -27,6 +30,12 @@ export class GameEntity {
   game_persons: Game_PersonEntity[];
   @OneToMany(type => GameGroupEntity, group => group.game)
   groups: GameGroupEntity[];
+  @OneToMany(
+    type => ObjectivePointEntity,
+    objective_points => objective_points.game,
+  )
+  objective_points: ObjectivePointEntity[];
+
 
   gameObject() {
     const { id, name } = this;
@@ -34,20 +43,6 @@ export class GameEntity {
   }
 }
 
-// table that stores all factions created for games
-@Entity('Faction')
-export class FactionEntity {
-  @PrimaryGeneratedColumn('uuid') id: string;
-  @Column('text') name: string;
-  @Column('text') password: string;
-  @ManyToOne(type => GameEntity, game => game.factions)
-  game: GameEntity;
-  @OneToMany(type => Game_PersonEntity, game_persons => game_persons.faction, {
-    onUpdate: 'CASCADE',
-  })
-  game_persons: Game_PersonEntity[];
-}
-
 // table that stores players associated with particular game
 @Entity('Game_Person')
 export class Game_PersonEntity {
@@ -59,14 +54,44 @@ export class Game_PersonEntity {
   game: GameEntity;
   @ManyToOne(type => PersonEntity, person => person.game_persons)
   person: PersonEntity;
-  @OneToOne(type => GameGroupEntity, group => group.leader)
+  @OneToOne(type => GameGroupEntity, group => group.leader, {
+    onDelete: 'CASCADE'
+  })
+  @JoinColumn({name: 'leaderGroup'})
   leaderGroup: GameGroupEntity;
-  @ManyToOne(type => GameGroupEntity, group => group.players)
+  @ManyToOne(type => GameGroupEntity, group => group.players, {
+    onDelete: 'CASCADE'
+  })
+  @JoinColumn({name: 'group'})
   group: GameGroupEntity;
-  /*
-    @ManyToOne(type => PersonRoleEntity, person_role => person_role.game_persons)
-    person_role: PersonRoleEntity;
-    @ManyToMany(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons)
-    game_person_coordinates: CoordinateEntity[]; */
 }
 
+@Entity('ObjectivePoint')
+export class ObjectivePointEntity {
+  @PrimaryGeneratedColumn('uuid') objectivePointId: string;
+  @Column({ type: 'text' }) objectivePointDescription: string;
+  @Column({ type: 'float' }) objectivePointMultiplier: number;
+
+  @ManyToOne(type => MapDrawingEntity, coordinate => coordinate.data)
+  coordinate: MapDrawingEntity;
+  @ManyToOne(type => GameEntity, game => game.objective_points)
+  game: GameEntity;
+  @OneToMany(
+    type => ObjectivePoint_HistoryEntity,
+    op_history => op_history.objective_point,
+  )
+  op_history: Game_PersonEntity[];
+}
+
+@Entity('ObjectivePoint_History')
+export class ObjectivePoint_HistoryEntity {
+  @PrimaryGeneratedColumn('uuid') oP_HistoryId: string;
+  @Column({ type: 'timestamp' }) oP_HistoryTimestamp: Timestamp;
+  @Column({}) oP_HistoryStatus: number;
+
+  @ManyToOne(
+    type => ObjectivePointEntity,
+    objective_point => objective_point.op_history,
+  )
+  objective_point: ObjectivePointEntity;
+}
diff --git a/src/game/game.module.ts b/src/game/game.module.ts
index 6851ea3..4b56620 100644
--- a/src/game/game.module.ts
+++ b/src/game/game.module.ts
@@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
 
 import { GameController } from './game.controller';
 import { GameService } from './game.service';
-import { GameEntity, FactionEntity, Game_PersonEntity } from './game.entity';
+import { GameEntity, Game_PersonEntity } from './game.entity';
 import { PersonEntity } from '../user/user.entity';
 import { GameGroupEntity } from './group.entity';
+import { FactionEntity } from './faction.entity';
 
 @Module({
   imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity, Game_PersonEntity, PersonEntity, GameGroupEntity])],
diff --git a/src/game/group.entity.ts b/src/game/group.entity.ts
index 66d2a85..0b9f00e 100644
--- a/src/game/group.entity.ts
+++ b/src/game/group.entity.ts
@@ -14,10 +14,14 @@ import { Game_PersonEntity, GameEntity } from './game.entity';
 export class GameGroupEntity {
   @PrimaryGeneratedColumn('uuid') id: string;
   @Column('text') name: string;
-  @OneToOne(type => Game_PersonEntity, person => person.leaderGroup)
-  @JoinColumn({name:'leader'})
+  @OneToOne(type => Game_PersonEntity, person => person.leaderGroup, {
+      onDelete: 'CASCADE'
+  })
+  //@JoinColumn({name:'leader'})
   leader: Game_PersonEntity;
-  @OneToMany(type => Game_PersonEntity, person => person.group)
+  @OneToMany(type => Game_PersonEntity, person => person.group, {
+      onDelete: 'CASCADE'
+  })
   players: Game_PersonEntity[];
   @ManyToOne(type => GameEntity, game => game.groups)
   game: GameEntity;
diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts
index dd2caae..b83b1d9 100644
--- a/src/mapmarkers/mapmarker.entity.ts
+++ b/src/mapmarkers/mapmarker.entity.ts
@@ -1,21 +1,26 @@
-import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'typeorm';
-
-import { PersonEntity } from '../user/user.entity'
-import { FactionEntity } from '../game/game.entity';
-
-/*
-Entity: MapMarker 
-- represents data that database contains on mapmarker
-*/
-
-@Entity('MapMarker')
-export class MapMarkerEntity {
+import {
+    Entity,
+    Column,
+    PrimaryGeneratedColumn,
+    Timestamp,
+    ManyToOne,
+  } from 'typeorm';
+  
+  import { PersonEntity } from '../user/user.entity';
+  
+  /*
+  Entity: MapMarker 
+  - represents data that database contains on mapmarker
+  */
+  
+  @Entity('MapMarker')
+  export class MapMarkerEntity {
     @PrimaryGeneratedColumn('uuid') id: string;
-    @Column({type: 'text'}) latitude: string;
-    @Column({type: 'text'}) longitude: string;
-    @Column({type: 'timestamp'}) timestamp: Timestamp;
-    @Column({type: 'json', nullable: true}) features: JSON;
+    @Column({ type: 'text', nullable: true }) latitude: string;
+    @Column({ type: 'text', nullable: true }) longitude: string;
+    @Column({ type: 'timestamp' }) timestamp: Timestamp;
+    @Column({ type: 'json', nullable: true }) features: JSON;
     @ManyToOne(type => PersonEntity, player => player.markers)
-    player?: PersonEntity;
-    //@ManyToOne(type => FactionEntity)
-}
\ No newline at end of file
+    player: PersonEntity;
+  }
+  
\ No newline at end of file
-- 
GitLab