From ade9ca1dbc8b0529f88d96e055f62876410c4162 Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Thu, 18 Jul 2019 14:02:41 +0300 Subject: [PATCH] replay returns players, factions, scores --- src/replay/replay.service.ts | 151 +++++++++++++++++++++++++---------- 1 file changed, 111 insertions(+), 40 deletions(-) diff --git a/src/replay/replay.service.ts b/src/replay/replay.service.ts index 76803d5..43fc0ab 100644 --- a/src/replay/replay.service.ts +++ b/src/replay/replay.service.ts @@ -1,6 +1,6 @@ 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 returs all player data from the game + // let playerdata = await this.trackingRepository.find({ where: { game: gameId }, relations: ['faction', 'gamepersonId', 'gamepersonId.person'], @@ -70,68 +77,127 @@ export class ReplayService { return player['data']; }), ); + // + // this block return 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 scores = await this.scoreRepository.find({ + where: { faction: In(factionIds) }, + relations: ['faction'], + }); + let currentScore = 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 + 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; 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 +206,11 @@ export class ReplayService { }), ); } + // generate x-amount of score tick + for (let i = 0; i < SCORE_TICKS; i++) { + await this.scoreService.scoreTick(gameId); + } + return { message: 'all done', }; -- GitLab