-
Ronnie Friman authoredRonnie Friman authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
tracking.service.ts 4.07 KiB
import { Injectable, HttpException, HttpStatus } 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 { GeoDTO } from './geo.dto';
import { FactionEntity } from 'src/faction/faction.entity';
@Injectable()
export class TrackingService {
constructor(
@InjectRepository(TrackingEntity)
private trackingrepository: Repository<TrackingEntity>,
@InjectRepository(Game_PersonEntity)
private gamepersonrepository: Repository<Game_PersonEntity>,
@InjectRepository(FactionEntity)
private factionRepository: Repository<FactionEntity>,
) {}
private icons = {
infantry: 'infantry.svg',
recon: 'recon.svg',
mechanized: 'mechanized.svg',
};
async trackLocation(
gameperson: Game_PersonEntity,
gameId,
trackdata: GeoDTO,
) {
// find ref to gameperson's tracking data
let trackedperson = await this.trackingrepository.findOne({
gamepersonId: gameperson,
});
if (trackedperson) {
trackdata['time'] = Date.now();
//add coordinates
trackedperson.data.push(trackdata);
//add timestamp
await this.trackingrepository.save(trackedperson);
return { message: 'Location updated!' };
} else {
// first entry will be empty
trackdata['time'] = Date.now();
// initialize data
trackedperson = await this.trackingrepository.create(trackedperson);
// if group exists, add icon based on that, else add default icon
trackedperson.icon = gameperson.group
? this.icons[gameperson.group.class]
: 'infantry.svg';
trackedperson.data = [trackdata];
trackedperson.faction = gameperson.faction;
trackedperson.game = gameId;
trackedperson.gamepersonId = gameperson;
await this.trackingrepository.save(trackedperson);
return { message: 'Entry Created!' };
}
}
// get player data while game is running
async getPlayers(gameperson, gameId) {
let playerdata = [];
// get playerdata
if (gameperson.faction) {
// create an array of the response as frontend maps the response
// to create different clusters for factions
playerdata.push(
await this.trackingrepository.find({
where: { faction: gameperson.faction },
relations: ['faction', 'gamepersonId'],
}),
);
} else {
let factions = await this.factionRepository.find({ game: gameId });
playerdata = await Promise.all(
factions.map(async faction => {
return await this.trackingrepository.find({
where: { faction: faction.factionId },
relations: ['faction', 'gamepersonId'],
});
}),
);
}
// parse data
const currentdata = await Promise.all(
await playerdata.map(async faction => {
return await Promise.all(
faction.map(async player => {
return await {
gamepersonId: player['gamepersonId']['gamepersonId'],
gamepersonRole: player['gamepersonId']['role'],
factionId: player['faction']['factionId'],
factionColour: player['faction']['colour'],
icon: player['icon'],
coordinates: player['data'].pop(),
};
}),
);
}),
);
return currentdata;
}
// get selected player data
async getPlayerData(person) {
const gameperson = await this.gamepersonrepository.findOne({
where: { gamepersonId: person.gamepersonId },
relations: ['person', 'leaderGroup', 'group', 'faction'],
});
if (!gameperson) {
throw new HttpException('No player found!', HttpStatus.BAD_REQUEST);
}
return {
gamepersonId: gameperson.gamepersonId,
name: gameperson.person.name,
role: gameperson.role,
group: gameperson.group,
faction: {
factionId: gameperson.faction.factionId,
factionName: gameperson.faction.factionName,
colour: gameperson.faction.colour,
},
};
}
}