Skip to content
Snippets Groups Projects
Commit f2ea5595 authored by L4721's avatar L4721
Browse files

Merge branch 'mockdata-replay' into 'Development'

Mockdata replay

See merge request !50
parents f647667e 8763a879
No related branches found
No related tags found
3 merge requests!59Development to master,!54Development to testing,!50Mockdata replay
......@@ -102,12 +102,11 @@ export class FactionService {
game: gameId,
});
// check if the authenticated person already belongs to a group
if (
await this.game_PersonRepository.findOne({
group: Not(null),
person: person,
})
) {
let check = await this.game_PersonRepository.findOne({
where: { person: person, game: gameId },
relations: ['group'],
});
if (check.group) {
throw new HttpException(
'You already belong to a group!',
HttpStatus.BAD_REQUEST,
......
......@@ -22,9 +22,4 @@ export class ReplayController {
async mockData(@Param('id') gameId) {
return this.replayservice.mockdata(gameId);
}
@Get('players/:id')
async getPlayers(@Param('id') gameId) {
return this.replayservice.getPlayers(gameId);
}
}
......@@ -3,7 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ReplayController } from './replay.controller';
import { ReplayService } from './replay.service';
import { GameEntity, Game_PersonEntity } from '../game/game.entity';
import {
GameEntity,
Game_PersonEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
} from '../game/game.entity';
import { FactionEntity, GameGroupEntity } from '../faction/faction.entity';
import { UserService } from '../user/user.service';
import { FactionService } from '../faction/faction.service';
......@@ -14,6 +19,9 @@ import {
MapDrawingEntity,
MapDrawingHistoryEntity,
} from '../draw/coordinate.entity';
import { ScoreService } from '../score/score.service';
import { ScoreEntity } from '../score/score.entity';
import { NotificationModule } from 'src/notifications/notifications.module';
@Module({
imports: [
......@@ -26,9 +34,19 @@ import {
Game_PersonEntity,
MapDrawingEntity,
MapDrawingHistoryEntity,
ScoreEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
]),
NotificationModule,
],
controllers: [ReplayController],
providers: [ReplayService, UserService, FactionService, TrackingService],
providers: [
ReplayService,
UserService,
FactionService,
TrackingService,
ScoreService,
],
})
export class ReplayModule {}
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, In } from 'typeorm';
import * as jwt from 'jsonwebtoken';
import { FactionEntity } from '../faction/faction.entity';
......@@ -13,6 +13,8 @@ import {
MapDrawingEntity,
MapDrawingHistoryEntity,
} from '../draw/coordinate.entity';
import { ScoreService } from 'src/score/score.service';
import { ScoreEntity } from 'src/score/score.entity';
@Injectable()
export class ReplayService {
......@@ -27,12 +29,15 @@ export class ReplayService {
private mapdrawingRepository: Repository<MapDrawingEntity>,
@InjectRepository(MapDrawingHistoryEntity)
private mapHistoryRepository: Repository<MapDrawingHistoryEntity>,
@InjectRepository(ScoreEntity)
private scoreRepository: Repository<ScoreEntity>,
private trackingService: TrackingService,
private userService: UserService,
private factionService: FactionService,
private scoreService: ScoreService,
) {}
async replayData(gameId) {
/* async replayData(gameId) {
let mapDrawingIds = await this.mapdrawingRepository.find({
where: { gameId: gameId },
select: ['mapDrawingId'],
......@@ -48,10 +53,12 @@ export class ReplayService {
}),
);
return drawings;
}
} */
// get replay data for players
async getPlayers(gameId) {
async replayData(gameId) {
//
// this block returns all player data from the game
//
let playerdata = await this.trackingRepository.find({
where: { game: gameId },
relations: ['faction', 'gamepersonId', 'gamepersonId.person'],
......@@ -70,68 +77,138 @@ export class ReplayService {
return player['data'];
}),
);
//
// this block returns all faction data from the game
//
let factions = await this.factionRepository.find({ game: gameId });
let currentFactions = factions.map(faction => {
return {
name: faction.factionName,
colour: faction.colour,
// all factions will be rendered on the map on default
active: true,
};
});
let factionIds = factions.map(faction => faction.factionId);
//
// this block returns all score data from the game
//
let currentScore = [];
await Promise.all(
factions.map(async faction => {
let scores = await this.scoreRepository.find({
where: { faction: faction },
relations: ['faction'],
});
currentScore.push(
scores.map(score => {
return {
score: score.score,
timestamp: score.scoreTimeStamp,
faction: score.faction['factionName'],
};
}),
);
}),
);
return currentdata;
return {
players: currentdata,
factions: currentFactions,
scores: currentScore,
};
}
// generate mockdata for a 3 day game
// create x amount of players
// assign them to two separate factions
// assign them to three separate groups
// insert x amount of locations for each players around some lat lng area
// insert x amount of score ticks for score mockdata
// use the game's initial geojson to draw game area
//
async mockdata(gameId) {
// initial settings for mockdata
// set the x amount of users to be created
const USER_AMOUNT = 100;
// set the x amount of locations to be created
const USER_LOCATIONS = 10;
// set the LAT and LNG for initial location
const LAT = 62.24147;
const LNG = 25.72088;
// set the score tick amount
// not used at the moment
const SCORE_TICKS = 10;
// setup the arrays for users, gamepersons and groups
const users = [];
const gamepersons = [];
const groups = [];
// characters for generating random usernames and the length of the username
const chars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const N = 15;
// used to equally divide the players to factions
let f = 0;
// get game info and factions associated with it
const game = await this.gameRepository.findOne({
where: { id: gameId },
relations: ['factions'],
});
const groups = await this.factionService.showGroups(
game.factions[1].factionId,
);
for (let i = 0; i < 100; i++) {
// get groups for all factions
await game.factions.forEach(async faction => {
groups.push(await this.factionService.showGroups(faction.factionId));
});
// create x amount of users for the mock game with random username
for (let i = 0; i < USER_AMOUNT; i++) {
let res = await this.userService.register({
name: 'asdfasa' + i,
name: Array(N)
.join()
.split(',')
.map(function() {
return chars.charAt(Math.floor(Math.random() * chars.length));
})
.join(''),
password: 'asd',
});
// get user information by verifying the token returned by register
let user = await jwt.verify(res.token, process.env.SECRET);
// push the created user to users array
users.push(user);
/* let gameperson = await this.factionService.joinFaction(user['id'], {
factionId:
i < 10 ? game.factions[0].factionId : game.factions[1].factionId,
factionPassword:
i < 10
? game.factions[0].factionPassword
: game.factions[1].factionPassword,
game: gameId,
}); */
// reset index if it's out of range
if (f === game.factions.length) f = 0;
// join faction with the created user
let gameperson = await this.factionService.joinFaction(user['id'], {
factionId: game.factions[1].factionId,
factionPassword: game.factions[1].factionPassword,
factionId: game.factions[f].factionId,
factionPassword: game.factions[f].factionPassword,
game: gameId,
});
// join a group randomly
await this.factionService.joinGroup(gameperson, {
groupId: groups[Math.floor(Math.random() * 3)],
groupId: groups[f][Math.floor(Math.random() * 3)],
});
// push the gameperson ref to gamepersons array
gamepersons.push(gameperson);
f++;
}
let date: number = Date.now();
let y1 = 25.7;
let x1 = 62.0;
let y2 = 25.75;
let x2 = 62.05;
for (let i = 1; i < 6; i++) {
let x = 0;
date += 10000;
// push x amount of user locations
for (let i = 1; i < USER_LOCATIONS + 1; i++) {
let date: number = Date.now();
let x = 1;
// score ticks with players to sync them
await this.scoreService.scoreTick(gameId);
// add location entry for each gameperson
await Promise.all(
gamepersons.map(async gameperson => {
x++;
// format player locations with x-modifier that same factions are closer to each other
if (x > game.factions.length) x = 1;
let dataObject = {
lat:
x < 50
? x1 + ((i + Math.random()) * 5) / 2000
: x2 - ((i + Math.random()) * 5) / 2000,
lng:
x < 50
? y1 + ((i + Math.random()) * 5) / 2000
: y2 - ((i + Math.random()) * 5) / 2000,
lat: LAT + ((i + Math.random()) * 5) / 2000,
lng: LNG + x / 100 + (Math.random() * 5) / 2000,
time: date,
};
x++;
// push the tracking data
await this.trackingService.trackLocation(
gameperson,
gameId,
......@@ -140,6 +217,7 @@ export class ReplayService {
}),
);
}
return {
message: 'all done',
};
......
......@@ -12,7 +12,7 @@ import { FactionEntity } from '../faction/faction.entity';
export class ScoreEntity {
@PrimaryGeneratedColumn('uuid') scoreId: string;
@Column({ type: 'float' }) score: number;
@CreateDateColumn({ type: 'timestamp' }) scoreTimeStamp: Timestamp;
@Column({ type: 'float' }) scoreTimeStamp: number;
@ManyToOne(type => FactionEntity, factionName => factionName.factionId, {
onDelete: 'CASCADE',
......
......@@ -44,6 +44,7 @@ export class ScoreService {
}
// add the score for Faction
const newScore = await this.scoreRepository.create(scoreData);
newScore.scoreTimeStamp = Date.now();
await this.scoreRepository.insert(newScore);
return {
message: 'Score updated!',
......
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