Skip to content
Snippets Groups Projects
Commit 01d5aabc authored by L4168's avatar L4168
Browse files

reworked draw services

parent a737daee
No related branches found
No related tags found
3 merge requests!59Development to master,!44Development to testing,!41Draw update
...@@ -7,12 +7,14 @@ import { ...@@ -7,12 +7,14 @@ import {
UsePipes, UsePipes,
ValidationPipe, ValidationPipe,
Body, Body,
UseInterceptors,
ClassSerializerInterceptor,
} from '@nestjs/common'; } from '@nestjs/common';
import { AuthGuard } from '../shared/auth.guard';
import { DrawService } from './draw.service'; import { DrawService } from './draw.service';
import { Roles, GameStates } from '../shared/guard.decorator'; import { Roles, GameStates } from '../shared/guard.decorator';
import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; import { MapDrawingDTO } from './mapdrawing.dto';
import { GamePerson } from 'src/game/gameperson.decorator';
/* /*
DrawController DrawController
...@@ -29,14 +31,18 @@ export class DrawController { ...@@ -29,14 +31,18 @@ export class DrawController {
@Roles('admin', 'factionleader') @Roles('admin', 'factionleader')
@GameStates('CREATED', 'STARTED') @GameStates('CREATED', 'STARTED')
@UsePipes(new ValidationPipe()) @UsePipes(new ValidationPipe())
async draw(@Param('id') gameId, @Body() data: MapDrawingDTO) { async draw(
return this.drawService.draw(gameId, data); @GamePerson() gameperson,
@Param('id') gameId,
@Body() data: MapDrawingDTO,
) {
return this.drawService.draw(gameperson, gameId, data);
} }
@Get('map/:id') @Get('map/:id')
@UseGuards(new AuthGuard()) @Roles('admin', 'factionleader', 'soldier', 'groupleader')
@UsePipes(new ValidationPipe()) @UseInterceptors(ClassSerializerInterceptor)
async drawMap(@Param('id') id, @Body() data: ReturnDrawingsDTO) { async drawMap(@GamePerson() gameperson, @Param('id') gameId) {
return this.drawService.drawMap(id, data); return this.drawService.drawMap(gameperson, gameId);
} }
} }
import { Injectable } from '@nestjs/common'; import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { MapDrawingEntity } from '../draw/coordinate.entity'; import { MapDrawingEntity } from '../draw/coordinate.entity';
import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; import { MapDrawingDTO } from './mapdrawing.dto';
import { NotificationGateway } from 'src/notifications/notifications.gateway'; import { NotificationGateway } from 'src/notifications/notifications.gateway';
@Injectable() @Injectable()
...@@ -14,38 +14,59 @@ export class DrawService { ...@@ -14,38 +14,59 @@ export class DrawService {
private notificationGateway: NotificationGateway, private notificationGateway: NotificationGateway,
) {} ) {}
async draw(gameId, data: MapDrawingDTO) { async draw(gameperson, gameId, data: MapDrawingDTO) {
data['gameId'] = gameId; data['gameId'] = gameId;
const drawing = await this.mapDrawingRepository.create(data); const drawing = await this.mapDrawingRepository.create(data);
this.notificationGateway.server.emit(gameId, { this.notificationGateway.server.emit(gameId, {
type: 'drawing-update', type: 'drawing-update',
}); });
// create new instance if id is null
if (data.mapDrawingId == null || data.mapDrawingId == '') { if (data.mapDrawingId == null || data.mapDrawingId == '') {
// luo uuden instanssin. drawing.faction = gameperson.faction;
console.log(drawing);
const mapDrawing = await this.mapDrawingRepository.insert(drawing); const mapDrawing = await this.mapDrawingRepository.insert(drawing);
return mapDrawing.identifiers; return mapDrawing.identifiers;
} else { }
//päivittää mapDrawingin // get ref from db
const draw = await this.mapDrawingRepository.findOne({
where: { mapDrawingId: data.mapDrawingId },
relations: ['faction'],
});
if (await draw.ownershipCheck(gameperson.faction, gameperson.role)) {
// else update the existing instance
return await this.mapDrawingRepository.save(drawing); return await this.mapDrawingRepository.save(drawing);
} }
throw new HttpException(
'Drawing is not from your faction!',
HttpStatus.BAD_REQUEST,
);
} }
// draw map based on game and // draw map based on game and gameperson faction
async drawMap(id, data: ReturnDrawingsDTO) { async drawMap(gameperson, gameId) {
// return all active drawings if admin
if (gameperson.role === 'admin') {
return await this.mapDrawingRepository.find({
where: { gameId: gameId, drawingIsActive: true },
relations: ['faction'],
});
}
// return mapdrawings with given faction and gameid // return mapdrawings with given faction and gameid
return await this.mapDrawingRepository.find({ return await this.mapDrawingRepository.find({
where: [ where: [
{ {
gameId: id, gameId: gameId,
faction: data.factionId, faction: gameperson.faction,
drawingIsActive: true, drawingIsActive: true,
}, },
{ {
gameId: id, gameId: gameId,
faction: null, faction: null,
drawingIsActive: true, drawingIsActive: true,
}, },
], ],
relations: ['faction'],
}); });
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment