diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts index da7e5950626bbcd5385d80755d208e59be589b48..2940b036852718e3af74947e120a513e138f74c9 100644 --- a/src/faction/faction.controller.ts +++ b/src/faction/faction.controller.ts @@ -22,6 +22,7 @@ import { } from './faction.dto'; import { FactionService } from './faction.service'; import { Roles, GameStates } from '../shared/guard.decorator'; +import { GamePerson } from 'src/game/gameperson.decorator'; @Controller('faction') export class FactionController { @@ -53,11 +54,11 @@ export class FactionController { @Roles('soldier') @GameStates('CREATED') async joinGroup( - @User('id') person, - @Param('id') id, + @GamePerson() gameperson, + @Param('id') gameId, @Body() data: JoinGameGroupDTO, ) { - return this.factionservice.joinGroup(person, id, data); + return this.factionservice.joinGroup(gameperson, data); } @UseInterceptors(ClassSerializerInterceptor) diff --git a/src/faction/faction.service.ts b/src/faction/faction.service.ts index 67edef1db5d071e1936aba03ded5c5051c1ae713..dc4e1490f76a1c386ec7d08a16b64f4d2337d5dd 100644 --- a/src/faction/faction.service.ts +++ b/src/faction/faction.service.ts @@ -110,13 +110,9 @@ export class FactionService { }); } - async joinGroup(person, gameId, data: JoinGameGroupDTO) { - const gamePerson = await this.game_PersonRepository.findOne({ - person: person, - game: gameId, - }); - gamePerson.group = data.groupId; - await this.game_PersonRepository.save(gamePerson); + async joinGroup(gameperson, data: JoinGameGroupDTO) { + gameperson.group = data.groupId; + await this.game_PersonRepository.save(gameperson); return { message: 'Joined group', }; diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 391d812fb2cad207ded708cf5bf619e0065b5009..84a8a97a30b53b094912d153ec9736c3fed1e59b 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -171,25 +171,25 @@ export class GameService { // returns name and id of each game async listGames(state) { - if(state == null){ + if (state == null) { const games = await this.gameRepository.find(); return games.map(game => { return game.gameObject(); }); - } - else if(state == 'ONGOING'){ + } else if (state == 'ONGOING') { const games = await this.gameRepository.find({ where: [ - {state: 'CREATED'}, {state: 'STARTED'}, {state: 'PAUSED'}, - ] + { state: 'CREATED' }, + { state: 'STARTED' }, + { state: 'PAUSED' }, + ], }); return games.map(game => { return game.gameObject(); }); - } - else{ + } else { const games = await this.gameRepository.find({ - where: {state: state} + where: { state: state }, }); return games.map(game => { return game.gameObject(); diff --git a/src/score/score.controller.ts b/src/score/score.controller.ts index 73fda22e699f5702b1616360bd7e95f0ac8aeb10..2fdebd9cbc1103961fcf99dad448aa5c838f144d 100644 --- a/src/score/score.controller.ts +++ b/src/score/score.controller.ts @@ -27,7 +27,7 @@ export class ScoreController { return this.scoreService.scoreTick(gameId); } - // shows scores, :id is gameId + // shows scores, :id is gameId @Get('get-score/:id') @GameStates('STARTED') async getScores(@Param('id') gameId: GameEntity) { diff --git a/src/score/score.service.ts b/src/score/score.service.ts index dba8e6e79206322a4dffae937b5af6b2bdd89041..d22e8dd24b8bf219a9f28557169c18e0d9e79247 100644 --- a/src/score/score.service.ts +++ b/src/score/score.service.ts @@ -89,26 +89,25 @@ export class ScoreService { async getScores(gameId: GameEntity) { // find games factions const factions = await this.factionRepository.find({ - where: {game: gameId,}, + where: { game: gameId }, relations: ['game'], }); let scores = []; await Promise.all( factions.map(async factionNow => { let score = await this.scoreRepository.findOne({ - where: {faction: factionNow}, + where: { faction: factionNow }, relations: ['faction'], - order: {scoreTimeStamp: 'DESC'}, + order: { scoreTimeStamp: 'DESC' }, }); //if score was found, put info to scores array if (score.faction) { - let index = await scores.findIndex( - i => i.faction === score.faction, - ); + let index = await scores.findIndex(i => i.faction === score.faction); scores.push(score); } - })) - return scores; + }), + ); + return scores; } } // diff --git a/src/shared/roles.guard.ts b/src/shared/roles.guard.ts index 73693f9e1551da1b1b34056a665eb59c1211b3ef..ae5c05fcfdb1d1a812d079de7e43cdb3c237b59a 100644 --- a/src/shared/roles.guard.ts +++ b/src/shared/roles.guard.ts @@ -34,9 +34,12 @@ export class RolesGuard implements CanActivate { const gameId = request.params.id; request.user = await this.getUserObject(request.headers.authorization); const role = await this.game_PersonRepository.findOne({ - person: request.user['id'], - game: gameId, + where: { person: request.user['id'], game: gameId }, + relations: ['faction'], }); + // add gameperson role to the request + // @GamePerson decorator can access it and pass it to service + request.gameperson = role; // check that the role matches the criteria and that token is valid for this game return role && roles.includes(role['role']); } diff --git a/src/task/task.controller.ts b/src/task/task.controller.ts index 8283ba5bd2c4d7da4eb668ac6268d5165930e3f4..14bbb215de47785a8bf9c284c1b0ef969b1ae84f 100644 --- a/src/task/task.controller.ts +++ b/src/task/task.controller.ts @@ -12,7 +12,7 @@ import { TaskService } from './task.service'; import { CreateTaskDTO, EditTaskDTO, DeleteTaskDTO } from './task.dto'; import { Roles } from '../shared/guard.decorator'; import { ValidationPipe } from '../shared/validation.pipe'; -import { User } from '../user/user.decorator'; +import { GamePerson } from 'src/game/gameperson.decorator'; @Controller('task') export class TaskController { @@ -49,7 +49,7 @@ export class TaskController { // :id is the id of the game @Get('get-tasks/:id') @Roles('soldier', 'factionleader', 'admin') - async fetchTasks(@User('id') person, @Param('id') id: string) { - return this.taskService.fetchTasks(person, id); + async fetchTasks(@GamePerson() gameperson, @Param('id') id: string) { + return this.taskService.fetchTasks(gameperson, id); } } diff --git a/src/task/task.module.ts b/src/task/task.module.ts index 66d5ac5fce93d8447317343283a84b5002755ab6..2391f284e62af1eaccb4df9a4cf051735a903775 100644 --- a/src/task/task.module.ts +++ b/src/task/task.module.ts @@ -5,12 +5,11 @@ import { TaskService } from './task.service'; import { TaskController } from './task.controller'; import { TaskEntity } from './task.entity'; import { FactionEntity } from '../faction/faction.entity'; -import { Game_PersonEntity } from '../game/game.entity'; import { NotificationModule } from '../notifications/notifications.module'; @Module({ imports: [ - TypeOrmModule.forFeature([TaskEntity, FactionEntity, Game_PersonEntity]), + TypeOrmModule.forFeature([TaskEntity, FactionEntity]), NotificationModule, ], controllers: [TaskController], diff --git a/src/task/task.service.ts b/src/task/task.service.ts index d79338e0d21e9ce741c67d4bf94b89ba64e4c9c9..043f65a130fd37f632b38464112ed353dc4a4233 100644 --- a/src/task/task.service.ts +++ b/src/task/task.service.ts @@ -15,8 +15,6 @@ export class TaskService { private taskRepository: Repository<TaskEntity>, @InjectRepository(FactionEntity) private factionRepository: Repository<FactionEntity>, - @InjectRepository(Game_PersonEntity) - private gamePersonRepository: Repository<Game_PersonEntity>, private notificationGateway: NotificationGateway, ) {} @@ -78,15 +76,8 @@ export class TaskService { throw new HttpException('Task not found', HttpStatus.BAD_REQUEST); } - async fetchTasks(user, taskGame) { - const gamePerson = await this.gamePersonRepository.findOne({ - where: { - person: user, - game: taskGame, - }, - relations: ['faction'], - }); - if (gamePerson.role == 'admin') { + async fetchTasks(gameperson, taskGame) { + if (gameperson.role == 'admin') { return await this.taskRepository.find({ where: { taskGame: taskGame }, relations: ['faction', 'taskWinner'], @@ -97,7 +88,7 @@ export class TaskService { where: [ { taskGame: taskGame, - faction: gamePerson.faction.factionId, + faction: gameperson.faction.factionId, }, { taskGame: taskGame, diff --git a/src/tracking/tracking.controller.ts b/src/tracking/tracking.controller.ts index ca0e79ee5195503f61d827dbc170474e66fd1bad..dbab7955f4cf8a90718b7ea0e5138173cbbe7e31 100644 --- a/src/tracking/tracking.controller.ts +++ b/src/tracking/tracking.controller.ts @@ -1,19 +1,11 @@ -import { - Controller, - Post, - Param, - UseGuards, - UsePipes, - Body, - Get, -} from '@nestjs/common'; +import { Controller, Post, Param, UsePipes, Body, Get } from '@nestjs/common'; import { TrackingService } from './tracking.service'; -import { TrackingDTO } from './tracking.dto'; import { User } from '../user/user.decorator'; import { Roles, GameStates } from '../shared/guard.decorator'; import { ValidationPipe } from '../shared/validation.pipe'; import { GeoDTO } from './geo.dto'; +import { GamePerson } from 'src/game/gameperson.decorator'; @Controller('tracking') export class TrackingController { @@ -26,17 +18,17 @@ export class TrackingController { @GameStates('STARTED') @UsePipes(new ValidationPipe()) async trackLocation( - @User('id') userId, + @GamePerson() gameperson, @Param('id') id, @Body() trackdata: GeoDTO, ) { - return this.trackingservice.trackLocation(userId, id, trackdata); + return this.trackingservice.trackLocation(gameperson, id, trackdata); } @Get('players/:id') @Roles('admin', 'factionleader') @GameStates('STARTED', 'PAUSED') - async getPlayerLocations(@User('id') userId, @Param('id') gameId) { - return this.trackingservice.getPlayers(userId, gameId); + async getPlayerLocations(@GamePerson() gameperson, @Param('id') gameId) { + return this.trackingservice.getPlayers(gameperson, gameId); } } diff --git a/src/tracking/tracking.module.ts b/src/tracking/tracking.module.ts index 45b167fc526ab41addbb87575518c9acae884482..4f6c2244473da2d9a80f63f5e1411fe6ae160d50 100644 --- a/src/tracking/tracking.module.ts +++ b/src/tracking/tracking.module.ts @@ -4,10 +4,9 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { TrackingController } from './tracking.controller'; import { TrackingService } from './tracking.service'; import { TrackingEntity } from './tracking.entity'; -import { Game_PersonEntity } from '../game/game.entity'; @Module({ - imports: [TypeOrmModule.forFeature([TrackingEntity, Game_PersonEntity])], + imports: [TypeOrmModule.forFeature([TrackingEntity])], controllers: [TrackingController], providers: [TrackingService], }) diff --git a/src/tracking/tracking.service.ts b/src/tracking/tracking.service.ts index 9941256dde52240d7c90b45d61b7b32ef4e1c242..446d0167c48f7951320c49f5976ad495e37f8fec 100644 --- a/src/tracking/tracking.service.ts +++ b/src/tracking/tracking.service.ts @@ -1,12 +1,9 @@ -import { Injectable, HttpStatus, HttpException } from '@nestjs/common'; +import { Injectable } 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 { TrackingDTO } from './tracking.dto'; -import { FactionEntity } from '../faction/faction.entity'; -import { NotificationGateway } from 'src/notifications/notifications.gateway'; import { GeoDTO } from './geo.dto'; @Injectable() @@ -14,26 +11,17 @@ export class TrackingService { constructor( @InjectRepository(TrackingEntity) private trackingrepository: Repository<TrackingEntity>, - @InjectRepository(Game_PersonEntity) - private gamepersonrepository: Repository<Game_PersonEntity>, ) {} - async trackLocation(personId, gameId, trackdata: GeoDTO) { - // find player - let gameperson = await this.gamepersonrepository.findOne({ - where: { game: gameId, person: personId }, - relations: ['faction'], - }); - if (!gameperson) { - throw new HttpException( - 'You have not joined this game', - HttpStatus.BAD_REQUEST, - ); - } + async trackLocation( + gameperson: Game_PersonEntity, + gameId, + trackdata: GeoDTO, + ) { + // find ref to gameperson's tracking data let trackedperson = await this.trackingrepository.findOne({ gamepersonId: gameperson, }); - // if player has pushed tracking data, update entry if (trackedperson) { trackdata['time'] = Date.now(); @@ -41,7 +29,7 @@ export class TrackingService { trackedperson.data.push(trackdata); //add timestamp await this.trackingrepository.save(trackedperson); - return { code: 201, message: 'Location updated!' }; + return { message: 'Location updated!' }; } else { // first entry will be empty trackdata['time'] = Date.now(); @@ -53,18 +41,12 @@ export class TrackingService { trackedperson.gamepersonId = gameperson; await this.trackingrepository.save(trackedperson); - return { code: 201, message: 'Entry Created!' }; + return { message: 'Entry Created!' }; } } // get player data while game is running - async getPlayers(userId, gameId) { - // get gameperson - const gameperson = await this.gamepersonrepository.findOne({ - where: { person: userId, game: gameId }, - relations: ['faction'], - }); - + async getPlayers(gameperson, gameId) { let playerdata; // get playerdata if (gameperson.faction) { @@ -93,10 +75,4 @@ export class TrackingService { return currentdata; } - - private async mapFunction(data): Promise<Number> { - return await data.map(type => { - return type; - }); - } }