Code owners
Assign users and groups as approvers for specific file changes. Learn more.
draw.service.ts 3.32 KiB
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {
MapDrawingEntity,
MapDrawingHistoryEntity,
} from '../draw/coordinate.entity';
import { MapDrawingDTO } from './mapdrawing.dto';
import { NotificationGateway } from '../notifications/notifications.gateway';
///////////////////////////////////////////////////////////////////////////
/// DrawService contains handling of MapDrawings and MapDrawinghistory ///
///////////////////////////////////////////////////////////////////////////
@Injectable()
export class DrawService {
constructor(
@InjectRepository(MapDrawingEntity)
private mapDrawingRepository: Repository<MapDrawingEntity>,
@InjectRepository(MapDrawingHistoryEntity)
private mapDrawHistoryRepository: Repository<MapDrawingHistoryEntity>,
private notificationGateway: NotificationGateway,
) {}
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 == '') {
drawing.faction = gameperson.faction;
const mapDrawing = await this.mapDrawingRepository.insert(drawing);
// create a history entity and insert it
await this.createHistory(data, gameperson, mapDrawing);
return mapDrawing.identifiers;
}
// 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
await this.createHistory(data, gameperson);
return await this.mapDrawingRepository.save(drawing);
}
throw new HttpException(
'Drawing is not from your faction!',
HttpStatus.BAD_REQUEST,
);
}
// used to create mapDrawing history entity entry
private async createHistory(data, gameperson, mapDrawing?) {
// create a history entity and insert it
const history = await this.mapDrawHistoryRepository.create({
data: data.data,
drawingIsActive: data.drawingIsActive,
mapdrawing:
data.mapDrawingId || mapDrawing.identifiers[0]['mapDrawingId'],
timestamp: Date.now(),
});
history.data['faction'] = gameperson.faction
? gameperson.faction.factionName
: 'admin';
await this.mapDrawHistoryRepository.insert(history);
}
// 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: gameId,
faction: gameperson.faction,
drawingIsActive: true,
},
{
gameId: gameId,
faction: null,
drawingIsActive: true,
},
],
relations: ['faction'],
});
}
}