diff --git a/package.json b/package.json index 9e44f929c2267e5ed2e143931c9b6a6bf908ff0e..5f2896f72bc42063a94fa79f6bfa0d91ad75ea02 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 cb69a91fd60f1fb0fccb3b37452bb28f988e4cf5..b773771cd0c40e19e6c35a23bf27c7f2e18f158a 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 0000000000000000000000000000000000000000..b271409d1b553cb9806cd8b706ee62f3e1958691 --- /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 0000000000000000000000000000000000000000..34390bfefbdfc55285ffd6e809126878562bbb12 --- /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 0000000000000000000000000000000000000000..f00c48d10b6437f5a1c3e3936254ac02949a670b --- /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 0000000000000000000000000000000000000000..f51937de0f8eb90e93869d9f3ef098c2ab84e993 --- /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 78a3b80180f35c87f64c254a16497e6cb9256b49..5d541c1783387bae995a09ff230b3f7c277d30b9 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 edff9d8e21ae4bd667b1eecb7162cacede906678..363bcbda70113b092f2fb9273ca65a0dda7129ae 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 952956dd30a375bd1c6be4c417757d69288fbc3c..aa0bc49851e117567637aeaeb035caa93234e657 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 59c7005ff488a2354b957ad5f56ae1f87af4de59..dc1d92982afc59066f31f16e3153e7361e75aead 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); + } + } }