From 4cbbaeaef5f04fb1e8dbfaf4eb75bd2041bd68d2 Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Wed, 26 Jun 2019 17:02:35 +0300 Subject: [PATCH] merged branch piirto --- package.json | 1 - src/app.module.ts | 3 +++ src/draw/draw.controller.ts | 50 +++++++++++++++++++++++++++++++++++++ src/draw/draw.module.ts | 17 +++++++++++++ src/draw/draw.service.ts | 43 +++++++++++++++++++++++++++++++ src/draw/mapdrawing.dto.ts | 9 +++++++ src/game/faction.entity.ts | 4 +++ src/game/game.controller.ts | 25 +++++++++---------- src/game/game.dto.ts | 2 +- src/game/game.service.ts | 45 +++++++++++++++++++++++++++++++++ 10 files changed, 184 insertions(+), 15 deletions(-) create mode 100644 src/draw/draw.controller.ts create mode 100644 src/draw/draw.module.ts create mode 100644 src/draw/draw.service.ts create mode 100644 src/draw/mapdrawing.dto.ts diff --git a/package.json b/package.json index 9e44f92..5f2896f 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "reflect-metadata": "^0.1.12", "rimraf": "^2.6.2", "rxjs": "^6.3.3", - "sequelize": "^5.8.7", "socket.io-redis": "^5.2.0", "typeorm": "^0.2.17" }, diff --git a/src/app.module.ts b/src/app.module.ts index cb69a91..b773771 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -9,9 +9,11 @@ import { UserModule } from './user/user.module'; import { HttpErrorFilter } from './shared/http-error.filter'; import { LoggingInterceptor } from './shared/logging.interceptor'; import { NotificationModule } from './notifications/notifications.module'; + import { GameModule } from './game/game.module'; import { RolesGuard } from './shared/roles.guard'; import { TaskModule } from './task/task.module'; +import { DrawModule } from './draw/draw.module'; @Module({ imports: [ @@ -20,6 +22,7 @@ import { TaskModule } from './task/task.module'; GameModule, NotificationModule, TaskModule, + DrawModule, ], controllers: [AppController], providers: [ diff --git a/src/draw/draw.controller.ts b/src/draw/draw.controller.ts new file mode 100644 index 0000000..b271409 --- /dev/null +++ b/src/draw/draw.controller.ts @@ -0,0 +1,50 @@ +import { + Controller, + Put, + UseGuards, + Get, + Param, + UsePipes, + ValidationPipe, + Body, +} from '@nestjs/common'; +import { AuthGuard } from 'src/shared/auth.guard'; +import { DrawService } from './draw.service'; +import { MapDrawingDTO } from './mapdrawing.dto'; +import { Roles } from 'src/shared/roles.decorator'; +import { User } from 'src/user/user.decorator'; +import { FactionDTO } from 'src/game/game.dto'; +import { FactionEntity } from 'src/game/faction.entity'; +/* + DrawController + + Functions either insert or return MapDrawing data, + Insert functions require user to have proper role (either gm or commander) in the game to be able store the ddata: MapDrawingDTOata to database. + Data return functions require atleast spectator role. + */ +@Controller('draw') +export class DrawController { + constructor(private drawService: DrawService) {} + + @Put() + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async draw(@Body() data: MapDrawingDTO) { + try { + return this.drawService.draw(data); + } catch (error) { + return error.message; + } + } + + @Get(':id') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async drawMap(@Param('id') id: string, @Body() faction: FactionDTO) { + try { + return this.drawService.drawMap(id, faction); + } catch (error) { + return error.message; + } + } +} diff --git a/src/draw/draw.module.ts b/src/draw/draw.module.ts new file mode 100644 index 0000000..34390bf --- /dev/null +++ b/src/draw/draw.module.ts @@ -0,0 +1,17 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { DrawController } from './draw.controller'; +import { DrawService } from './draw.service'; +import { MapDrawingEntity } from 'src/game/coordinate.entity'; +import { FactionEntity } from 'src/game/faction.entity'; +/* +Draw +- contains everything to do with mapdrawing data. +*/ +@Module({ + imports: [TypeOrmModule.forFeature([MapDrawingEntity, FactionEntity])], + controllers: [DrawController], + providers: [DrawService], +}) +export class DrawModule {} diff --git a/src/draw/draw.service.ts b/src/draw/draw.service.ts new file mode 100644 index 0000000..f00c48d --- /dev/null +++ b/src/draw/draw.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { MapDrawingEntity } from 'src/game/coordinate.entity'; +import { Repository } from 'typeorm'; +import { MapDrawingDTO } from './mapdrawing.dto'; +import { Game_PersonEntity, GameEntity } from '../game/game.entity'; +import { GameDTO, FactionDTO } from 'src/game/game.dto'; +import { FactionEntity } from 'src/game/faction.entity'; + +@Injectable() +export class DrawService { + constructor( + @InjectRepository(MapDrawingEntity) + private mapDrawingRepository: Repository<MapDrawingEntity>, + @InjectRepository(FactionEntity) + private factionRepository: Repository<FactionEntity>, + ) {} + + async draw(data: MapDrawingDTO) { + try { + return this.mapDrawingRepository.insert(data); + } catch (error) { + return error; + } + } + + // draw map based on game and + async drawMap(id: string, faction: FactionDTO) { + try { + // get faction + const factionInDb = await this.factionRepository.findOne({ + where: { game: id, factionName: faction.factionName }, + }); + + // return mapdrawings with given faction and gameid + return this.mapDrawingRepository.find({ + where: { gameId: id, faction: factionInDb }, + }); + } catch (error) { + return error; + } + } +} diff --git a/src/draw/mapdrawing.dto.ts b/src/draw/mapdrawing.dto.ts new file mode 100644 index 0000000..f51937d --- /dev/null +++ b/src/draw/mapdrawing.dto.ts @@ -0,0 +1,9 @@ +import { GameDTO, FactionDTO } from '../game/game.dto'; + +export class MapDrawingDTO { + data: JSON; + gameId: GameDTO; + faction?: FactionDTO; + isActive?: boolean; + validUntil?: string; +} diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts index 78a3b80..5d541c1 100644 --- a/src/game/faction.entity.ts +++ b/src/game/faction.entity.ts @@ -30,6 +30,10 @@ export class FactionEntity { const { factionId, factionName, game } = this; return { factionId, factionName, game }; } + + passwordCheck(pass: string) { + return pass == this.factionPassword ? true : false; + } } @Entity('PowerUp') diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index edff9d8..363bcbd 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -27,7 +27,7 @@ export class GameController { return this.gameservice.createNewGame(person, body); } - @Put(':id') + @Put('edit/:id') @Roles('admin') @UsePipes(new ValidationPipe()) async editGame(@Param('id') id: string, @Body() body: GameDTO) { @@ -58,19 +58,18 @@ export class GameController { return this.gameservice.joinGroup(person, id); } - @Put('joinfaction') + // param game ID is passed to @Roles + @Put('promote/:id') @UseGuards(new AuthGuard()) - joinFaction( - @User('id') person, - @Param('id') gameId, - @Param('faction') faction: string, - @Body() password: string, - ) { - try { - // return this.gameservice.joinFaction(person, gameId, faction, password); - } catch (error) { - return error; - } + @Roles('admin') + promotePlayer(@Param('id') game, @Body() body) { + return this.gameservice.promotePlayer(body); + } + + @Put('join-faction') + @UseGuards(new AuthGuard()) + joinFaction(@User('id') person, @Body() faction) { + return this.gameservice.joinFaction(person, faction); } @Get('listgames') diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index 952956d..aa0bc49 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -48,7 +48,7 @@ export class FactionDTO { @Length(2, 15) factionName: string; factionPassword: string; - multiplier: number; + multiplier?: number; game: GameDTO; } diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 59c7005..dc1d929 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -234,4 +234,49 @@ export class GameService { // send flagbox event to flagbox subscribers this.notificationGateway.server.emit('flagbox', 'event update'); } + + async promotePlayer(body) { + const gamepersonId = body.player; + // get playerdata + const gameperson = await this.game_PersonRepository.findOne({ + where: { gamepersonId }, + }); + if (gameperson) { + const factionleader = await this.game_PersonRepository.create(gameperson); + factionleader.role = body.role; + return await this.game_PersonRepository.save(factionleader); + } + throw new HttpException('player does not exist', HttpStatus.BAD_REQUEST); + } + + async joinFaction(person, faction) { + const name = faction.factionName; + // get faction + const factionInDb = await this.factionRepository.findOne({ + where: { name }, + }); + if (!factionInDb) { + throw new HttpException('No factions exist!', HttpStatus.BAD_REQUEST); + } + //check if password is correct + if (factionInDb.passwordCheck(faction.factionPassword)) { + const gameperson = await this.game_PersonRepository.create({ + faction: faction.factionName, + game: faction.game, + role: 'soldier', + person: person, + }); + //check if user is already in a faction + if (await this.game_PersonRepository.findOne(gameperson)) { + throw new HttpException( + 'You have already joined faction!', + HttpStatus.BAD_REQUEST, + ); + } + // insert to database + return await this.game_PersonRepository.save(gameperson); + } else { + throw new HttpException('Invalid password!', HttpStatus.UNAUTHORIZED); + } + } } -- GitLab