From 9a4447d83c75aded5fbf983f7b92189a925a4936 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20Syd=C3=A4nmaa?= <L4072@student.jamk.fi>
Date: Tue, 11 Jun 2019 08:19:54 +0300
Subject: [PATCH] Adds game controller and updates database

---
 src/{coordinate => game}/coordinate.entity.ts |  4 +-
 src/game/faction.entity.ts                    |  6 +-
 src/game/game.controller.ts                   |  8 +-
 src/game/game.dto.ts                          | 84 ++++++++++++++++++-
 src/game/game.entity.ts                       |  9 +-
 src/game/game.service.ts                      | 34 ++++++++
 6 files changed, 132 insertions(+), 13 deletions(-)
 rename src/{coordinate => game}/coordinate.entity.ts (96%)
 create mode 100644 src/game/game.service.ts

diff --git a/src/coordinate/coordinate.entity.ts b/src/game/coordinate.entity.ts
similarity index 96%
rename from src/coordinate/coordinate.entity.ts
rename to src/game/coordinate.entity.ts
index 988b725..02f2c7d 100644
--- a/src/coordinate/coordinate.entity.ts
+++ b/src/game/coordinate.entity.ts
@@ -1,6 +1,6 @@
 import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, ManyToMany, OneToMany, ManyToOne, JoinTable } from 'typeorm';
-import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from '../game/game.entity'
-import { FactionEntity } from '../game/faction.entity'
+import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from './game.entity'
+import { FactionEntity } from './faction.entity'
 //import { MapEntity, MapDrawingEntity } from '../map/map.entity'
 
 @Entity('Coordinate')
diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts
index 0c79ee2..6782a6a 100644
--- a/src/game/faction.entity.ts
+++ b/src/game/faction.entity.ts
@@ -2,12 +2,14 @@ import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, Timestamp
 import {PersonEntity} from '../user/user.entity'
 import {GameEntity} from './game.entity'
 import {Game_PersonEntity} from './game.entity'
-import { MapDrawingEntity } from '../coordinate/coordinate.entity'
+import { MapDrawingEntity } from './coordinate.entity'
 
 @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[];
@@ -60,7 +62,7 @@ export class FP_HistoryEntity{
 @Entity('Score')
 export class ScoreEntity {
     @PrimaryGeneratedColumn('uuid') scoreId: string;
-    @Column({type: 'int'}) score: number;
+    @Column({type: 'float'}) score: number;
     @Column({type: 'timestamp'}) scoreTimeStamp: Timestamp;
 
     @ManyToOne(type => FactionEntity, factionName => factionName.scores)
diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts
index aec0681..5e1a521 100644
--- a/src/game/game.controller.ts
+++ b/src/game/game.controller.ts
@@ -1,8 +1,10 @@
 import { Controller, Post, Body } from '@nestjs/common';
 import {CreateGameDTO} from './game.dto'
 
-@Controller('Game')
+@Controller('game')
 export class GameController{
-    //@Post()
-    //create(@Body() )
+    @Post()
+    create(@Body() data: CreateGameDTO) {
+        return this//.gameService.register(data);
+    }
 }
\ No newline at end of file
diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts
index 664739c..64e062b 100644
--- a/src/game/game.dto.ts
+++ b/src/game/game.dto.ts
@@ -1,9 +1,85 @@
-import { IsNotEmpty, IsString } from 'class-validator';
+import { IsNotEmpty, IsString, IsDate, Length, IsInt, Min, Max, IsArray, IsJSON } from 'class-validator';
+import { Timestamp } from 'typeorm';
+import { FactionEntity } from './faction.entity';
+
 
 export class CreateGameDTO {
-    @IsString () @IsNotEmpty()
+    @IsString() @IsNotEmpty()
     gameId: string;
 
-    @IsString () @IsNotEmpty()
+    @IsString() @IsNotEmpty() @Length(3, 30)
     gameName: string;
-}
\ No newline at end of file
+
+    @IsString()
+    gameDescription?: string;
+
+    @IsDate() @IsNotEmpty()
+    startDate: string;
+
+    @IsDate() @IsNotEmpty()
+    endDate: string;
+
+    @IsArray() @IsNotEmpty()
+    factions?: FactionEntity[];
+
+    @IsArray() @IsNotEmpty()
+    powerUps?: PowerUpDTO[];
+
+    @IsArray() @IsNotEmpty()
+    objectivePoint?: ObjectivePointDTO[];
+
+    @IsJSON() @IsNotEmpty()
+    mapCoordinates: JSON;
+
+    @IsString() @IsNotEmpty() @Length(3, 255)
+    GM_Password?: string;
+}
+
+export class FactionDTO {
+    @IsString() @IsNotEmpty()
+    FactionID: string;
+
+    @IsString() @IsNotEmpty() @Length(3, 30)
+    FactionName: string;
+
+}
+
+export class PowerUpDTO {
+    @IsString() @IsNotEmpty()
+    powerUpName: string;
+
+    @IsString() @IsNotEmpty()
+    powerUpDescription?: string;
+
+    @IsInt() @IsNotEmpty()
+    amount: number;
+
+    @IsNotEmpty()
+    cooldown: string;
+}
+
+export class ObjectivePointDTO {
+    @IsString() @IsNotEmpty()
+    objectivePointID: string;
+
+    @IsString() @IsNotEmpty()
+    objectivePointDescription: string;
+
+    @IsJSON() @IsNotEmpty()
+    objectivePointCoordinates: JSON;
+
+    @IsArray() @IsNotEmpty()
+    objectivePointHistory: ObjectivePointHistoryDTO[];
+}
+
+export class ObjectivePointHistoryDTO {
+    @IsString() 
+    FactionId: string;
+
+    @IsNotEmpty() @IsInt() @Min(0) @Max(2)
+    oP_HistoryStatus: number;
+
+    @IsNotEmpty()
+    oP_HistoryTimestamp: Timestamp;
+}
+
diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts
index 26d09a4..6f04c34 100644
--- a/src/game/game.entity.ts
+++ b/src/game/game.entity.ts
@@ -1,8 +1,8 @@
 import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, ManyToMany, Timestamp, JoinTable } from 'typeorm';
 import {PersonEntity, PersonRoleEntity} from '../user/user.entity'
 import {FactionEntity} from './faction.entity'
-import {CoordinateEntity} from '../coordinate/coordinate.entity'
-import { MapEntity } from '../coordinate/coordinate.entity'
+import {CoordinateEntity} from './coordinate.entity'
+import { MapEntity } from './coordinate.entity'
 
 @Entity('Game')
 export class GameEntity {
@@ -11,6 +11,7 @@ export class GameEntity {
     @Column({type: 'text'}) gameDescription: string;
     @Column({type: 'date'}) startDate: string;
     @Column({type: 'date'}) endDate: string;
+    @Column({type: 'text'}) GM_Password: string;
 
     @ManyToOne(type => MapEntity, map => map.games)
     map: MapEntity;
@@ -21,6 +22,10 @@ export class GameEntity {
     @OneToMany(type => ObjectivePointEntity, objective_points => objective_points.game)
     objective_points: ObjectivePointEntity[];
     
+    gameObject() {
+        const {gameId, gameName, gameDescription, startDate, endDate, GM_Password} = this;
+        return this;
+    }
 }
 
 @Entity('Game_Person')
diff --git a/src/game/game.service.ts b/src/game/game.service.ts
new file mode 100644
index 0000000..9ea147d
--- /dev/null
+++ b/src/game/game.service.ts
@@ -0,0 +1,34 @@
+import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
+import { Repository } from "typeorm";
+import { InjectRepository } from "@nestjs/typeorm";
+
+import { GameEntity } from './game.entity';
+import { CreateGameDTO } from './game.dto';
+
+@Injectable()
+export class UserService {
+    constructor(@InjectRepository(GameEntity) private userRepository: Repository<GameEntity>){}
+
+    async register(data: CreateGameDTO) {
+        const { gameName } = data;
+        let user = await this.userRepository.findOne({where: {name}});
+        if (user) {
+            throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
+        }
+        user = await this.userRepository.create(data);
+        await this.userRepository.save(user);
+        return user.gameObject();
+    }
+
+    /*async login(data: UserDTO) {
+        const { name, password } = data;
+        const user = await this.userRepository.findOne({ where: { name }});
+        if (!user || !(await user.comparePassword(password))) {
+            throw new HttpException(
+                'Invalid username/password',
+                HttpStatus.BAD_REQUEST,
+            );
+        }
+        return user.tokenObject();
+    }*/
+}
-- 
GitLab