diff --git a/src/faction/faction.service.ts b/src/faction/faction.service.ts index bcaf4af49583471f614cad9287d4130c5a67aadf..4a574baa552ac205b509a50edc75c50072663410 100644 --- a/src/faction/faction.service.ts +++ b/src/faction/faction.service.ts @@ -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, diff --git a/src/replay/replay.controller.ts b/src/replay/replay.controller.ts index ed23960549f32e8b76e21b72dfb4693a7f389224..e91bbc09653b35ad45b7581b1529806e2a9c3e0a 100644 --- a/src/replay/replay.controller.ts +++ b/src/replay/replay.controller.ts @@ -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); - } } diff --git a/src/replay/replay.module.ts b/src/replay/replay.module.ts index b5dc7c888da8806ecae546a7dd5aecfcc7f10dae..00638def67675c7fd46775c4fa40c4c1ab2b250b 100644 --- a/src/replay/replay.module.ts +++ b/src/replay/replay.module.ts @@ -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 {} diff --git a/src/replay/replay.service.ts b/src/replay/replay.service.ts index 76803d53d827479c5256fb1a74bf6410ca55ab4f..04ace741bd2ed4dc1ee96a6a2359d745a219896c 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 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', }; diff --git a/src/score/score.entity.ts b/src/score/score.entity.ts index 6ca9ab77b968060a6dc88c860425119914cec030..1047f90dc7029c1011368ab54107a875a224b6d2 100644 --- a/src/score/score.entity.ts +++ b/src/score/score.entity.ts @@ -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', diff --git a/src/score/score.service.ts b/src/score/score.service.ts index 90716c4b2e9f5d72f21971edb574972a2ccfda6d..a0ff983f527dba7bb6d51160743be20dc37b5e0b 100644 --- a/src/score/score.service.ts +++ b/src/score/score.service.ts @@ -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!',