diff --git a/src/game/coordinate.entity.ts b/src/game/coordinate.entity.ts index 9a996e1ff25025673895e59af64fecd26f8d6054..ecaf6dd6ffa8953931747e32da5cc113fd04fa17 100644 --- a/src/game/coordinate.entity.ts +++ b/src/game/coordinate.entity.ts @@ -1,18 +1,14 @@ -import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, ManyToMany, OneToMany, ManyToOne, JoinTable, Timestamp } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, PrimaryColumn } from 'typeorm'; import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from './game.entity' import { FactionEntity } from './faction.entity' -//import { MapEntity, MapDrawingEntity } from '../map/map.entity' @Entity('Coordinate') export class CoordinateEntity { - @PrimaryGeneratedColumn('uuid') coordinateId: string; - @Column({type: 'geometry'}) longitude: string; - @Column({type: 'geometry'}) latitude: string; - @Column() coordinateTimestamp: Timestamp; + @PrimaryColumn({type: 'json', nullable: true}) features: JSON; - @ManyToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.mapDrawings_coordinates) + @OneToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.mapDrawings_coordinates) mapDrawings: MapDrawingEntity[]; - @ManyToMany(type => Game_PersonEntity, game_persons => game_persons.game_person_coordinates) + @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game_person_coordinates) game_persons: Game_PersonEntity[]; @OneToMany(type => ObjectivePointEntity, objective_points => objective_points.coordinate) objective_points: ObjectivePointEntity[]; @@ -39,9 +35,8 @@ export class MapDrawingEntity { @Column({type: 'bool'}) drawingIsActive: boolean; @Column({type: 'time'}) drawingValidTill: string; - @ManyToMany(type => CoordinateEntity, mapDrawings_coordinates => mapDrawings_coordinates.mapDrawings) - @JoinTable() - mapDrawings_coordinates: CoordinateEntity[]; + @ManyToOne(type => CoordinateEntity, mapDrawings_coordinates => mapDrawings_coordinates.mapDrawings) + mapDrawings_coordinates: CoordinateEntity; @ManyToOne(type => MapEntity, map => map.mapDrawings) map: MapEntity; @ManyToOne(type => MapDrawingTypeEntity, mapDrawingType => mapDrawingType.mapDrawings) diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts index 6782a6a5c774a35a89540e56fd6853680ee0d731..e5d8a740eea4e1e9e2a3fd30c5f42613d1a33c51 100644 --- a/src/game/faction.entity.ts +++ b/src/game/faction.entity.ts @@ -4,6 +4,8 @@ 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; diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 13ccb8652c46e8b6dc42f11372e555ed303ce044..c79f96232e2dc336ff71a89bff848ea2d0ff7a89 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -1,6 +1,49 @@ -import { Controller } from '@nestjs/common'; +import { Controller, Post, UsePipes, Body, Get, UseGuards, Param, Delete, Put } from '@nestjs/common'; + +import { GameService } from './game.service'; +import { CreateGameDTO/*, EditGameDTO */} from './game.dto'; +import { AuthGuard } from '../shared/auth.guard'; +import { ValidationPipe } from '../shared/validation.pipe'; +import { User } from 'src/user/user.decorator'; @Controller('game') export class GameController { - + constructor(private gameservice: GameService) { } + + @Post('new') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async newGame(@User('id') person, @Body() body: CreateGameDTO ) { + return this.gameservice.createNewGame(person, body); + } + + @Put(':id') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async editGame(@Param('id') id: string, @Body() body: CreateGameDTO) { + return this.gameservice.editGame(id, body); + } + + @Delete(':id') + @UseGuards(new AuthGuard()) + async deleteGame(@Param('id') id) { + return this.gameservice.deleteGame(id); + } + + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + @Post(':id') + async joinGame(@User('id') person, @Param('id') id: string, @Body() password: string) { + return this.gameservice.joinGame(person, id, password); + } + + @Get('listgames') + async listGames() { + return this.gameservice.listGames(); + } + + @Get(':id') + async returnGameInfo(@Param('id') id: string) { + return this.gameservice.returnGameInfo(id); + } } diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index 058479d781da25298431746202f08a7714673cb5..d0d19dd65db2dcbaa855be70d45c334632be6579 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -1,12 +1,7 @@ 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() - gameId: string; - @IsString() @IsNotEmpty() @Length(3, 30) gameName: string; @@ -20,7 +15,7 @@ export class CreateGameDTO { endDate: string; @IsArray() @IsNotEmpty() - factions?: FactionEntity[]; + factions?: FactionDTO[]; @IsArray() @IsNotEmpty() objectivePoint?: ObjectivePointDTO[]; @@ -32,12 +27,38 @@ export class CreateGameDTO { GM_Password?: string; } -export class FactionDTO { - @IsString() @IsNotEmpty() - FactionID: string; +/*export class EditGameDTO { + @IsString() @IsNotEmpty() @Length(3, 30) + gameName?: string; + + @IsString() + gameDescription?: string; + + @IsDate() @IsNotEmpty() + startDate: string; + + @IsDate() @IsNotEmpty() + endDate: string; + + @IsArray() @IsNotEmpty() + factions?: FactionDTO[]; + + @IsArray() @IsNotEmpty() + objectivePoint?: ObjectivePointDTO[]; + + @IsJSON() @IsNotEmpty() + mapCoordinates: JSON; + + @IsString() @IsNotEmpty() @Length(3, 255) + GM_Password?: string; +}*/ +export class FactionDTO { @IsString() @IsNotEmpty() @Length(3, 30) - FactionName: string; + factionName: string; + + @IsString() @IsNotEmpty() @Length(3, 255) + faction_Password?: string; } @@ -56,9 +77,6 @@ export class FactionDTO { }*/ export class ObjectivePointDTO { - @IsString() @IsNotEmpty() - objectivePointID: string; - @IsString() @IsNotEmpty() objectivePointDescription: string; @@ -71,7 +89,7 @@ export class ObjectivePointDTO { export class ObjectivePointHistoryDTO { @IsString() - FactionId: string; + factionId: string; @IsNotEmpty() @IsInt() @Min(0) @Max(2) oP_HistoryStatus: number; diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts index 8f839ba586b6e713b47787360b0b4597f740c3ac..3db7f242880b3559add149fe48372735faf6ff01 100644 --- a/src/game/game.entity.ts +++ b/src/game/game.entity.ts @@ -1,9 +1,11 @@ -import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, ManyToMany, Timestamp, JoinTable } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, Timestamp} from 'typeorm'; import * as bcrypt from 'bcryptjs'; import {PersonEntity, PersonRoleEntity} from '../user/user.entity' import {FactionEntity} from './faction.entity' import {CoordinateEntity, MapEntity} from './coordinate.entity' +//Game, Game_Person, ObjectivePoint, ObjectivePoint_History + @Entity('Game') export class GameEntity { @PrimaryGeneratedColumn('uuid') gameId: string; @@ -44,9 +46,8 @@ export class Game_PersonEntity { person: PersonEntity; @ManyToOne(type => PersonRoleEntity, person_role => person_role.game_persons) person_role: PersonRoleEntity; - @ManyToMany(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons) - @JoinTable() - game_person_coordinates: CoordinateEntity[]; + @ManyToOne(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons) + game_person_coordinates: CoordinateEntity; } @Entity('ObjectivePoint') diff --git a/src/game/game.module.ts b/src/game/game.module.ts index 737305fc00cdecd37e6a74dd2b86f2cc977dee75..babb8ea476fae11504f3e3088196a69e4ba09bf2 100644 --- a/src/game/game.module.ts +++ b/src/game/game.module.ts @@ -3,10 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { GameController } from './game.controller'; import { GameService } from './game.service'; -import { GameEntity } from './game.entity'; +import { GameEntity, Game_PersonEntity } from './game.entity'; +import { FactionEntity } from './faction.entity'; +import { PersonEntity } from 'src/user/user.entity'; @Module({ - imports: [TypeOrmModule.forFeature([GameEntity])], + imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity, Game_PersonEntity, PersonEntity])], controllers: [GameController], providers: [GameService] }) diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 3d91055e3f9cf02fb11da18b0ae11cecadfee458..9a5cd1c1905bf8f8981bfb92b6aa66580526236d 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -1,34 +1,109 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { Repository } from "typeorm"; -import { InjectRepository } from "@nestjs/typeorm"; +import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository, In } from 'typeorm'; -import { GameEntity } from './game.entity'; +import { GameEntity, Game_PersonEntity } from './game.entity'; import { CreateGameDTO } from './game.dto'; +import { PersonEntity } from '../user/user.entity'; +import { FactionEntity } from './faction.entity'; @Injectable() export class GameService { - constructor(@InjectRepository(GameEntity) private userRepository: Repository<GameEntity>){} - - async register(data: CreateGameDTO) { - const { gameName } = data; - let game = await this.userRepository.findOne({where: {gameName}}); - if (game) { - throw new HttpException('Game with this name already exists', HttpStatus.BAD_REQUEST); - } - game = await this.userRepository.create(data); - await this.userRepository.save(game); - return game.gameObject(); + constructor( + @InjectRepository(GameEntity) + private gameRepository: Repository<GameEntity>, + @InjectRepository(FactionEntity) + private factionRepository: Repository<FactionEntity>, + @InjectRepository(PersonEntity) + private personRepository: Repository<PersonEntity>, + @InjectRepository(Game_PersonEntity) + private game_PersonRepository: Repository<Game_PersonEntity>, + ) {} + + // create a new game + async createNewGame(personId: string, gameData: CreateGameDTO) { + // checks if a game with the same name exists already + const { gameName } = gameData; + if (await this.gameRepository.findOne({ where: { name } })) { + throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST); } + // else add the game to the database + const game = await this.gameRepository.create({ + ...gameData, + factions: gameData.factions, + }); + await this.gameRepository.insert(game); + return { + message: 'New game added', + }; + } + + // edit already created game + async editGame(gameId: string, gameData: Partial<CreateGameDTO>) { + try { + // update game entry in db + let update = await this.gameRepository.create({ + ...gameData, + factions: gameData.factions + }) + await this.gameRepository.update({ gameId }, update); + // add the factions to db + if (gameData.factions) { + gameData.factions.map(async faction => { + let name = await this.factionRepository.create({ + ...faction, + game: gameData, + }); + await this.factionRepository.insert(name); + }); + } + return { + message: 'Game updated', + }; + } catch (error) {console.log(error)} + } + + async deleteGame(gameId) { + // TODO: Delete factions from Faction table associated with the deleted game + await this.gameRepository.delete({ gameId }); + return { + message: 'Game deleted', + }; + } + + // checks the password, creates an entry in GamePerson table with associated role&faction + async joinGame(person, gameId, json) { + const user = await this.personRepository.findOne({ + where: { id: person }, + }); + const game = await this.gameRepository.findOne({ where: { id: gameId } }); + + const index = game.GM_Password.indexOf(json.password); + + // create game_Person entry + /* const gamePerson = await this.game_PersonRepository.create({ + faction, + gameId, + person, + }); + */ + return 'WIP'; +} + +// returns name and id of each game +async listGames() { + const games = await this.gameRepository.find({ relations: ['factions'] }); + return games.map(game => { + return game.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(); - }*/ +// returns information about a game identified by id +async returnGameInfo(id: string) { + const game = await this.gameRepository.findOne({ + where: { id: id }, + relations: ['factions'], + }); + return game; } +} \ No newline at end of file diff --git a/src/mapmarkers/mapmarkers.controller.ts b/src/mapmarkers/mapmarkers.controller.ts index f6c2e0ba8b3242be35f0f99bbdeb020a08e68f55..a6ac31b814d7b213d3717937fbb914bfaa63876a 100644 --- a/src/mapmarkers/mapmarkers.controller.ts +++ b/src/mapmarkers/mapmarkers.controller.ts @@ -2,15 +2,15 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerDTO } from './mapmarker.dto'; -import { AuthGuard } from 'dist/shared/auth.guard'; -import { User } from 'src/user/user.decorator'; +import { AuthGuard } from '../shared/auth.guard'; +import { User } from '../user/user.decorator'; @Controller('mapmarkers') export class MapMarkersController { constructor(private mapmarkerservice: MapMarkerService){} // Insert figure location, needs "authorization" header with valid Bearer token and content-type json - @Put('insertLocation') + @Put('insert-location') @UseGuards(new AuthGuard()) async insertLocation(@User('id') person, @Body() data: MapMarkerDTO): Promise<string>{ try { diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 363e894a9e7fd0cb34df6dbd8a81b496f623bdea..b5ef4b244ef844f6739d95edbeac832e0eb6242c 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -1,9 +1,9 @@ import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common'; -import { UserService } from './user.service'; -import { UserDTO } from './user.dto'; -import { AuthGuard } from 'src/shared/auth.guard'; -import { ValidationPipe } from 'src/shared/validation.pipe'; + import { UserService } from './user.service'; + import { UserDTO } from './user.dto'; + import { AuthGuard } from '../shared/auth.guard'; + import { ValidationPipe } from '../shared/validation.pipe'; @Controller('user') export class UserController {