Skip to content
Snippets Groups Projects
Commit 903574f1 authored by L4168's avatar L4168
Browse files

merged Tracking

parent 27729f77
No related branches found
No related tags found
3 merge requests!59Development to master,!36Development,!32Game state
...@@ -18,7 +18,8 @@ export class DrawService { ...@@ -18,7 +18,8 @@ export class DrawService {
if (data.mapDrawingId == null || data.mapDrawingId == '') { if (data.mapDrawingId == null || data.mapDrawingId == '') {
// luo uuden instanssin. // luo uuden instanssin.
return this.mapDrawingRepository.insert(drawing)[0]; const mapDrawing = await this.mapDrawingRepository.insert(drawing);
return mapDrawing.identifiers;
} else { } else {
//päivittää mapDrawingin //päivittää mapDrawingin
return await this.mapDrawingRepository.save(drawing); return await this.mapDrawingRepository.save(drawing);
......
import {
Controller,
Post,
Param,
UseGuards,
UsePipes,
Body,
} from '@nestjs/common';
import { TrackingService } from './tracking.service';
import { TrackingDTO } from './tracking.dto';
import { User } from '../user/user.decorator';
import { Roles, GameStates } from '../shared/guard.decorator';
import { ValidationPipe } from '../shared/validation.pipe';
@Controller('tracking')
export class TrackingController {
constructor(private trackingservice: TrackingService) {}
// inserts tracking data to the database
// :id is the id of the game
@Post('location/:id')
@Roles('soldier')
@GameStates('started')
@UsePipes(new ValidationPipe())
async trackLocation(
@User('id') userId,
@Param('id') id,
@Body() trackdata: TrackingDTO,
) {
return this.trackingservice.trackLocation(userId, id, trackdata);
}
}
import { Game_PersonEntity } from '../game/game.entity';
import { Allow } from 'class-validator';
export class TrackingDTO {
@Allow()
data: JSON;
gamepersonId: Game_PersonEntity;
}
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Game_PersonEntity } from '../game/game.entity';
@Entity('Tracking')
export class TrackingEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column({ type: 'json', nullable: true }) data: JSON;
@ManyToOne(type => Game_PersonEntity, person => person.gamepersonId)
gamepersonId: Game_PersonEntity;
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TrackingController } from './tracking.controller';
import { TrackingService } from './tracking.service';
import { TrackingEntity } from './tracking.entity';
import { Game_PersonEntity } from '../game/game.entity';
@Module({
imports: [TypeOrmModule.forFeature([TrackingEntity, Game_PersonEntity])],
controllers: [TrackingController],
providers: [TrackingService],
})
export class TrackingModule {}
import { Injectable, HttpStatus, HttpException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Game_PersonEntity } from '../game/game.entity';
import { TrackingEntity } from './tracking.entity';
import { TrackingDTO } from './tracking.dto';
@Injectable()
export class TrackingService {
constructor(
@InjectRepository(TrackingEntity)
private trackingrepository: Repository<TrackingEntity>,
@InjectRepository(Game_PersonEntity)
private gamepersonrepository: Repository<Game_PersonEntity>,
) {}
async trackLocation(personId, gameId, trackdata: TrackingDTO) {
// find player
let gameperson = await this.gamepersonrepository.findOne({
game: gameId,
person: personId,
});
if (!gameperson) {
throw new HttpException(
'You have not joined this game',
HttpStatus.BAD_REQUEST,
);
}
let trackedperson = await this.trackingrepository.findOne({
gamepersonId: gameperson,
});
// if player has pushed tracking data, update entry
if (trackedperson) {
//add coordinates
trackedperson.data['geometry']['coordinates'].push(
await this.mapFunction(trackdata.data['geometry']['coordinates']),
);
//add timestamp
trackedperson.data['geometry']['properties']['time'].push(
new Date(Date.now()),
);
return await this.trackingrepository.save(trackedperson);
} else {
// first entry will be empty
// initialize coordinates
trackdata.data['geometry']['coordinates'] = [];
// initialize timestamp
trackdata.data['geometry']['properties']['time'] = [];
trackedperson = await this.trackingrepository.create(trackdata);
trackedperson.gamepersonId = gameperson;
return await this.trackingrepository.save(trackedperson);
}
}
private async mapFunction(data): Promise<Number> {
return await data.map(type => {
return type;
});
}
}
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