diff --git a/src/app.module.ts b/src/app.module.ts index f2c7b7765304436488bbadb3fdfce4fbdc1fbd9a..53a56f466f39eb0e799f5c1c56f9906f9220a102 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -19,6 +19,7 @@ import { DrawModule } from './draw/draw.module'; import { FactionModule } from './faction/faction.module'; import { GameModule } from './game/game.module'; import { ScoreModule } from './score/score.module'; +import { ReplayModule } from './replay/replay.module'; @Module({ imports: [ @@ -31,6 +32,7 @@ import { ScoreModule } from './score/score.module'; FactionModule, TrackingModule, ScoreModule, + ReplayModule, ], controllers: [AppController], providers: [ diff --git a/src/replay/replay.controller.ts b/src/replay/replay.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..acc2ef50dafc1fb1c7f193c8da4834162b6ff4be --- /dev/null +++ b/src/replay/replay.controller.ts @@ -0,0 +1,23 @@ +import { Controller, Get, Param, Post } from '@nestjs/common'; +import { ReplayService } from './replay.service'; +import { Roles } from 'src/shared/guard.decorator'; + +@Controller('replay') +export class ReplayController { + constructor(private replayservice: ReplayService) {} + + @Get(':id') + async replayInfo(@Param('id') gameId) { + return this.replayservice.replayData(gameId); + } + + @Post('mockdata/:id') + 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 new file mode 100644 index 0000000000000000000000000000000000000000..c7accefa55f867a49c36e2dbc5cc3e75563b211c --- /dev/null +++ b/src/replay/replay.module.ts @@ -0,0 +1,28 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { ReplayController } from './replay.controller'; +import { ReplayService } from './replay.service'; +import { GameEntity, Game_PersonEntity } from '../game/game.entity'; +import { FactionEntity, GameGroupEntity } from '../faction/faction.entity'; +import { UserService } from '../user/user.service'; +import { FactionService } from '../faction/faction.service'; +import { TrackingService } from '../tracking/tracking.service'; +import { TrackingEntity } from 'src/tracking/tracking.entity'; +import { PersonEntity } from 'src/user/user.entity'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([ + PersonEntity, + GameEntity, + FactionEntity, + TrackingEntity, + GameGroupEntity, + Game_PersonEntity, + ]), + ], + controllers: [ReplayController], + providers: [ReplayService, UserService, FactionService, TrackingService], +}) +export class ReplayModule {} diff --git a/src/replay/replay.service.ts b/src/replay/replay.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5430860112e243305a4f0542036b8f9a0bb8a0a --- /dev/null +++ b/src/replay/replay.service.ts @@ -0,0 +1,117 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import * as jwt from 'jsonwebtoken'; + +import { FactionEntity } from '../faction/faction.entity'; +import { GameEntity } from 'src/game/game.entity'; +import { TrackingService } from 'src/tracking/tracking.service'; +import { UserService } from 'src/user/user.service'; +import { FactionService } from 'src/faction/faction.service'; +import { TrackingEntity } from 'src/tracking/tracking.entity'; + +@Injectable() +export class ReplayService { + constructor( + @InjectRepository(FactionEntity) + private factionRepository: Repository<FactionEntity>, + @InjectRepository(GameEntity) + private gameRepository: Repository<GameEntity>, + @InjectRepository(TrackingEntity) + private trackingRepository: Repository<TrackingEntity>, + private trackingService: TrackingService, + private userService: UserService, + private factionService: FactionService, + ) {} + + async replayData(gameId) { + const replay = await this.factionRepository.find({ + where: { game: gameId }, + relations: ['mapDrawings', 'scores', 'trackdata'], + }); + return replay; + } + + // get replay data for players + async getPlayers(gameId) { + let playerdata = await this.trackingRepository.find({ + where: { game: gameId }, + relations: ['faction', 'gamepersonId'], + }); + + // parse data + const currentdata = await Promise.all( + playerdata.map(async player => { + return player['data']; + /* return { + gamepersonId: player['gamepersonId']['gamepersonId'], + gamepersonRole: player['gamepersonId']['role'], + factionId: player['faction']['factionId'], + coordinates: player['data'], + }; */ + }), + ); + + return currentdata; + } + // generate mockdata for a 3 day game + async mockdata(gameId) { + const users = []; + const gamepersons = []; + const game = await this.gameRepository.findOne({ + where: { id: gameId }, + relations: ['factions'], + }); + for (let i = 0; i < 20; i++) { + let res = await this.userService.register({ + name: 'qqqqq' + i, + password: 'asd', + }); + let user = await jwt.verify(res.token, process.env.SECRET); + 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, + }); + gamepersons.push(gameperson); + } + let date: number = Date.now(); + let y1 = 25.7; + let x1 = 62.3; + let y2 = 25.75; + let x2 = 62.35; + for (let i = 1; i < 16 * 6 * 3; i++) { + let x = 0; + date += 10000; + await Promise.all( + gamepersons.map(async gameperson => { + x++; + let dataObject = { + lat: + x < 10 + ? x1 + ((i + Math.random()) * 5) / 2000 + : x2 - ((i + Math.random()) * 5) / 2000, + lng: + x < 10 + ? y1 + ((i + Math.random()) * 5) / 2000 + : y2 - ((i + Math.random()) * 5) / 2000, + time: date, + }; + await this.trackingService.trackLocation( + gameperson, + gameId, + dataObject, + ); + }), + ); + } + return { + message: 'all done', + }; + } +}