Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
tracking.service.ts 3.15 KiB
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';
import { FactionEntity } from '../faction/faction.entity';

@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({
      where: { game: gameId, person: personId },
      relations: ['faction'],
    });
    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['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['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;
    });
  }
}