Skip to content
Snippets Groups Projects
Commit aab14ff3 authored by Samuli Virtapohja's avatar Samuli Virtapohja
Browse files

Merge branch 'draw-update' into HEAD

parents df652e90 01d5aabc
No related branches found
No related tags found
3 merge requests!59Development to master,!44Development to testing,!41Draw update
import {
Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
Timestamp,
} from 'typeorm';
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Game_PersonEntity, GameEntity } from '../game/game.entity';
import { GameEntity } from '../game/game.entity';
import { FactionEntity } from '../faction/faction.entity';
@Entity('MapDrawing')
......@@ -25,27 +19,14 @@ export class MapDrawingEntity {
onDelete: 'CASCADE',
})
gameId: GameEntity;
}
@Entity('Game_Person_MapDrawing')
export class Game_Person_MapDrawingEntity {
@PrimaryGeneratedColumn('uuid') GPmapDrawingId: string;
@Column({ type: 'timestamp' }) GPCTimeStamp: Timestamp;
@ManyToOne(
type => Game_PersonEntity,
game_person => game_person.gamepersonId,
{
onDelete: 'CASCADE',
},
)
game_person: Game_PersonEntity;
@ManyToOne(
type => MapDrawingEntity,
map_drawing => map_drawing.mapDrawingId,
{
onDelete: 'CASCADE',
},
)
map_drawing: MapDrawingEntity;
async ownershipCheck(factionEntity, role) {
if (role === 'admin') {
return factionEntity == this.faction;
} else {
return this.faction && factionEntity.factionId === this.faction.factionId
? true
: false;
}
}
}
......@@ -7,12 +7,14 @@ import {
UsePipes,
ValidationPipe,
Body,
UseInterceptors,
ClassSerializerInterceptor,
} from '@nestjs/common';
import { AuthGuard } from '../shared/auth.guard';
import { DrawService } from './draw.service';
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
......@@ -29,14 +31,18 @@ export class DrawController {
@Roles('admin', 'factionleader')
@GameStates('CREATED', 'STARTED')
@UsePipes(new ValidationPipe())
async draw(@Param('id') gameId, @Body() data: MapDrawingDTO) {
return this.drawService.draw(gameId, data);
async draw(
@GamePerson() gameperson,
@Param('id') gameId,
@Body() data: MapDrawingDTO,
) {
return this.drawService.draw(gameperson, gameId, data);
}
@Get('map/:id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
async drawMap(@Param('id') id, @Body() data: ReturnDrawingsDTO) {
return this.drawService.drawMap(id, data);
@Roles('admin', 'factionleader', 'soldier', 'groupleader')
@UseInterceptors(ClassSerializerInterceptor)
async drawMap(@GamePerson() gameperson, @Param('id') gameId) {
return this.drawService.drawMap(gameperson, gameId);
}
}
import { Injectable } from '@nestjs/common';
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MapDrawingEntity } from '../draw/coordinate.entity';
import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto';
import { MapDrawingDTO } from './mapdrawing.dto';
import { NotificationGateway } from 'src/notifications/notifications.gateway';
@Injectable()
......@@ -14,38 +14,59 @@ export class DrawService {
private notificationGateway: NotificationGateway,
) {}
async draw(gameId, data: MapDrawingDTO) {
async draw(gameperson, gameId, data: MapDrawingDTO) {
data['gameId'] = gameId;
const drawing = await this.mapDrawingRepository.create(data);
this.notificationGateway.server.emit(gameId, {
type: 'drawing-update',
});
// create new instance if id is null
if (data.mapDrawingId == null || data.mapDrawingId == '') {
// luo uuden instanssin.
drawing.faction = gameperson.faction;
console.log(drawing);
const mapDrawing = await this.mapDrawingRepository.insert(drawing);
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);
}
throw new HttpException(
'Drawing is not from your faction!',
HttpStatus.BAD_REQUEST,
);
}
// draw map based on game and
async drawMap(id, data: ReturnDrawingsDTO) {
// draw map based on game and gameperson faction
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 await this.mapDrawingRepository.find({
where: [
{
gameId: id,
faction: data.factionId,
gameId: gameId,
faction: gameperson.faction,
drawingIsActive: true,
},
{
gameId: id,
gameId: gameId,
faction: null,
drawingIsActive: true,
},
],
relations: ['faction'],
});
}
}
......@@ -12,16 +12,7 @@ export class MapDrawingDTO {
@IsOptional()
@IsUUID('4')
gameId: GameEntity;
@IsOptional()
@IsUUID('4')
faction?: FactionEntity;
@IsBoolean()
drawingIsActive?: boolean;
drawingValidTill?: string;
}
export class ReturnDrawingsDTO {
@IsOptional()
@IsUUID('4')
factionId: FactionEntity;
}
......@@ -134,13 +134,16 @@ export class FactionService {
where: { person, game },
relations: ['faction'],
});
if (gameperson) {
if (gameperson.faction) {
return {
factionId: gameperson.faction.factionId,
factionName: gameperson.faction.factionName,
};
} else {
throw new HttpException('No faction was found', HttpStatus.BAD_REQUEST);
throw new HttpException(
gameperson ? 'You are admin for this game!' : 'No faction was found',
HttpStatus.BAD_REQUEST,
);
}
}
}
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