diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts index 7574d822c5aef8ef5c350ab349dc0801f916b753..da7e5950626bbcd5385d80755d208e59be589b48 100644 --- a/src/faction/faction.controller.ts +++ b/src/faction/faction.controller.ts @@ -79,7 +79,7 @@ export class FactionController { // :id is the id of the game, and is needed for GameStates to check the state of the game @Put('join-faction/:id') @UseGuards(new AuthGuard()) - @GameStates('CREATED') + @GameStates('CREATED', 'STARTED') @UsePipes(new ValidationPipe()) joinFaction( @User('id') person, diff --git a/src/tracking/tracking.controller.ts b/src/tracking/tracking.controller.ts index 9451cdc366f0847389067bd5b9093903a46a1633..8049173b8cb8b5147a306965d9fb3836a9fae619 100644 --- a/src/tracking/tracking.controller.ts +++ b/src/tracking/tracking.controller.ts @@ -5,6 +5,7 @@ import { UseGuards, UsePipes, Body, + Get, } from '@nestjs/common'; import { TrackingService } from './tracking.service'; @@ -30,4 +31,11 @@ export class TrackingController { ) { return this.trackingservice.trackLocation(userId, id, trackdata); } + + @Get('players/:id') + @Roles('admin', 'factionleader') + @GameStates('STARTED', 'PAUSED') + async getPlayerLocations(@User('id') userId, @Param('id') gameId) { + return this.trackingservice.getPlayers(userId, gameId); + } } diff --git a/src/tracking/tracking.entity.ts b/src/tracking/tracking.entity.ts index cabe141a42bcfe076588e320a6eed6c20c6ceaa9..7af1ca40806131b1a9517ae5489dc649184d56b4 100644 --- a/src/tracking/tracking.entity.ts +++ b/src/tracking/tracking.entity.ts @@ -1,5 +1,6 @@ import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; -import { Game_PersonEntity } from '../game/game.entity'; +import { Game_PersonEntity, GameEntity } from '../game/game.entity'; +import { FactionEntity } from 'src/faction/faction.entity'; @Entity('Tracking') export class TrackingEntity { @@ -9,4 +10,8 @@ export class TrackingEntity { onDelete: 'CASCADE', }) gamepersonId: Game_PersonEntity; + @ManyToOne(type => FactionEntity, faction => faction.factionId) + faction: FactionEntity; + @ManyToOne(type => GameEntity, game => game.id) + game: GameEntity; } diff --git a/src/tracking/tracking.service.ts b/src/tracking/tracking.service.ts index 6dc01c1d9c6dbfcf7336750602d2c90c5092f81b..fb4eab054b0a53d4fcc2481c21e28a1adcf257a0 100644 --- a/src/tracking/tracking.service.ts +++ b/src/tracking/tracking.service.ts @@ -5,6 +5,7 @@ import { Repository } from 'typeorm'; import { Game_PersonEntity } from '../game/game.entity'; import { TrackingEntity } from './tracking.entity'; import { TrackingDTO } from './tracking.dto'; +import { FactionEntity } from '../faction/faction.entity'; @Injectable() export class TrackingService { @@ -18,8 +19,8 @@ export class TrackingService { async trackLocation(personId, gameId, trackdata: TrackingDTO) { // find player let gameperson = await this.gamepersonrepository.findOne({ - game: gameId, - person: personId, + where: { game: gameId, person: personId }, + relations: ['faction'], }); if (!gameperson) { throw new HttpException( @@ -50,11 +51,50 @@ export class TrackingService { // initialize timestamp trackdata.data['geometry']['properties']['time'] = []; trackedperson = await this.trackingrepository.create(trackdata); + trackedperson.faction = gameperson.faction; + trackedperson.game = gameId; trackedperson.gamepersonId = gameperson; return await this.trackingrepository.save(trackedperson); } } + // get player data while game is running + async getPlayers(userId, gameId) { + // get gameperson + const gameperson = await this.gamepersonrepository.findOne({ + where: { person: userId, game: gameId }, + relations: ['faction'], + }); + + let playerdata; + // get playerdata + if (gameperson.faction) { + playerdata = await this.trackingrepository.find({ + where: { faction: gameperson.faction }, + relations: ['faction', 'gamepersonId'], + }); + } else { + playerdata = await this.trackingrepository.find({ + where: { game: gameId }, + relations: ['faction', 'gamepersonId'], + }); + } + + // parse data + const currentdata = await Promise.all( + playerdata.map(async player => { + return { + gamepersonId: player['gamepersonId']['gamepersonId'], + gamepersonRole: player['gamepersonId']['role'], + factionId: player['faction']['factionId'], + coordinates: player['data']['geometry']['coordinates'].pop(), + }; + }), + ); + + return currentdata; + } + private async mapFunction(data): Promise<Number> { return await data.map(type => { return type;