Skip to content
Snippets Groups Projects
game.entity.ts 3.93 KiB
Newer Older
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  OneToMany,
  Timestamp,
L4168's avatar
L4168 committed
  JoinColumn,
} from 'typeorm';

import { PersonEntity } from '../user/user.entity';
Samuli Virtapohja's avatar
Samuli Virtapohja committed
import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from '../faction/faction.entity';
L4168's avatar
L4168 committed
import { TaskEntity } from '../task/task.entity';
Ronnie Friman's avatar
Ronnie Friman committed
import { CenterDTO, NodeSettingsDTO } from './game.dto';
Samuli Virtapohja's avatar
Samuli Virtapohja committed

// table that stores all created games
@Entity('Game')
export class GameEntity {
  @PrimaryGeneratedColumn('uuid') id: string;
  @Column('text') name: string;
  @Column('text') desc: string;
L4168's avatar
L4168 committed
  @Column('json') center: CenterDTO;
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  @Column({ type: 'json', nullable: true }) map: JSON;
  @Column({ type: 'json', nullable: true }) nodesettings?: NodeSettingsDTO;
L4168's avatar
L4168 committed
  @Column('text') state: string;
  @Column('timestamp') startdate: Timestamp;
  @Column('timestamp') enddate: Timestamp;
  @Column('text') image: string;
L4168's avatar
L4168 committed
  @OneToMany(type => FactionEntity, factions => factions.game)
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  factions: FactionEntity[];
  @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game)
  game_persons: Game_PersonEntity[];
L4168's avatar
L4168 committed
  @OneToMany(
    type => ObjectivePointEntity,
    objective_points => objective_points.game,
  )
  objective_points: ObjectivePointEntity[];
L4168's avatar
L4168 committed
  @OneToMany(type => TaskEntity, tasks => tasks.taskGame)
  tasks: TaskEntity[];
L4168's avatar
L4168 committed

L4168's avatar
L4168 committed
  gameObject() {
    const { id, name } = this;
    return { id, name };
  }
// table that stores players associated with particular game
Ronnie Friman's avatar
Ronnie Friman committed
@Entity('Game_Person', {
  orderBy: {
    person: 'ASC',
  },
})
export class Game_PersonEntity {
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  @PrimaryGeneratedColumn('uuid') gamepersonId: string;
  @Column({ type: 'text', nullable: true }) role: string;
L4072's avatar
L4072 committed
  // If a Faction or Game where the GamePerson was in is deleted, the GamePerson is also deleted
L4168's avatar
L4168 committed
  @ManyToOne(type => FactionEntity, faction => faction.game_persons, {
Ronnie Friman's avatar
Ronnie Friman committed
    onDelete: 'CASCADE',
L4168's avatar
L4168 committed
  })
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  faction: FactionEntity;
Ronnie Friman's avatar
Ronnie Friman committed
  @ManyToOne(type => GameEntity, game => game.id, {
    onDelete: 'CASCADE',
  })
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  game: GameEntity;
  @ManyToOne(type => PersonEntity, person => person.id)
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  person: PersonEntity;
Ronnie Friman's avatar
Ronnie Friman committed
  @OneToOne(type => GameGroupEntity, group => group.leader)
  leaderGroup: GameGroupEntity;
L4072's avatar
L4072 committed

  // When a Group where GamePerson is is deleted, nothing happens to the GamePerson
L4168's avatar
L4168 committed
  @ManyToOne(type => GameGroupEntity, group => group.players, {
Ronnie Friman's avatar
Ronnie Friman committed
    onDelete: 'NO ACTION',
L4168's avatar
L4168 committed
  })
Samuli Virtapohja's avatar
Samuli Virtapohja committed
  @JoinColumn({ name: 'group' })
  group: GameGroupEntity;
Samuli Virtapohja's avatar
Samuli Virtapohja committed
}
L4168's avatar
L4168 committed
@Entity('ObjectivePoint')
export class ObjectivePointEntity {
  @PrimaryGeneratedColumn('uuid') objectivePointId: string;
  @Column({ type: 'text' }) objectivePointDescription: string;
  @Column({ type: 'float' }) objectivePointMultiplier: number;
  @Column({ type: 'json' }) data: JSON;
L4168's avatar
L4168 committed

  // If the Game where the ObjectivePoint was in is deleted, the ObjectivePoint is also deleted
Ronnie Friman's avatar
Ronnie Friman committed
  @ManyToOne(type => GameEntity, game => game.objective_points, {
    onDelete: 'CASCADE',
  })
L4168's avatar
L4168 committed
  game: GameEntity;
  @OneToMany(
    () => ObjectivePoint_HistoryEntity,
    history => history.objective_point,
    {
      onDelete: 'NO ACTION',
    },
  )
  history: ObjectivePoint_HistoryEntity[];
L4168's avatar
L4168 committed
}

@Entity('ObjectivePoint_History')
export class ObjectivePoint_HistoryEntity {
  @PrimaryGeneratedColumn('uuid') oP_HistoryId: string;
  @Column({ type: 'float' }) oP_HistoryTimestamp: number;
L4168's avatar
L4168 committed
  @Column('float') action: number;
L4072's avatar
L4072 committed

  // If the owner Faction, capturer Faction or ObjectivePoint, that has, is trying to have or is the point where
L4072's avatar
L4072 committed
  // ObjectivePointHistory points to is deleted, the ObjectivePointHistory is also deleted
L4168's avatar
L4168 committed
  @ManyToOne(type => FactionEntity, factionEntity => factionEntity.factionId, {
    onDelete: 'CASCADE',
  })
L4168's avatar
L4168 committed
  capture: FactionEntity;
L4168's avatar
L4168 committed
  @ManyToOne(type => FactionEntity, factionentity => factionentity.factionId, {
    onDelete: 'CASCADE',
  })
L4168's avatar
L4168 committed
  owner: FactionEntity;
L4168's avatar
L4168 committed
  @ManyToOne(
    type => ObjectivePointEntity,
    objective_point => objective_point.objectivePointId,
Ronnie Friman's avatar
Ronnie Friman committed
    {
      onDelete: 'CASCADE',
    },
L4168's avatar
L4168 committed
  )
  objective_point: string;
L4168's avatar
L4168 committed
}