From 8c09e1c0a9865866e837b179c242191de8133722 Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Wed, 10 Jul 2019 09:54:21 +0300 Subject: [PATCH] updated services with gameperson decorator --- src/faction/faction.controller.ts | 7 +++-- src/faction/faction.service.ts | 10 ++----- src/game/game.service.ts | 16 +++++------ src/score/score.controller.ts | 2 +- src/score/score.service.ts | 15 +++++----- src/shared/roles.guard.ts | 7 +++-- src/task/task.controller.ts | 6 ++-- src/task/task.module.ts | 3 +- src/task/task.service.ts | 15 ++-------- src/tracking/tracking.controller.ts | 20 ++++--------- src/tracking/tracking.module.ts | 3 +- src/tracking/tracking.service.ts | 44 +++++++---------------------- 12 files changed, 52 insertions(+), 96 deletions(-) diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts index da7e595..2940b03 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 67edef1..dc4e149 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 391d812..84a8a97 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 73fda22..2fdebd9 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 dba8e6e..d22e8dd 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 73693f9..ae5c05f 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 8283ba5..14bbb21 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 66d5ac5..2391f28 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 d79338e..043f65a 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 ca0e79e..dbab795 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 45b167f..4f6c224 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 9941256..446d016 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; - }); - } } -- GitLab