diff --git a/src/draw/draw.module.ts b/src/draw/draw.module.ts index a56cbbbb7fb49b476842177a4a0a9e7e0b592b3c..a310951f38e0f39c3ae3943133124cdea4b3b4c5 100644 --- a/src/draw/draw.module.ts +++ b/src/draw/draw.module.ts @@ -6,6 +6,7 @@ import { DrawService } from './draw.service'; import { MapDrawingEntity } from '../draw/coordinate.entity'; import { FactionEntity } from '../faction/faction.entity'; import { Game_PersonEntity } from '../game/game.entity'; +import { NotificationModule } from 'src/notifications/notifications.module'; /* Draw - contains everything to do with mapdrawing data. @@ -17,6 +18,7 @@ Draw FactionEntity, Game_PersonEntity, ]), + NotificationModule, ], controllers: [DrawController], providers: [DrawService], diff --git a/src/draw/draw.service.ts b/src/draw/draw.service.ts index d5ddf23e9574771bc769e2b6d58b59a4e95ad10d..d699af7a990a2178cda1617a6a2a06c3ba988f0e 100644 --- a/src/draw/draw.service.ts +++ b/src/draw/draw.service.ts @@ -4,17 +4,22 @@ import { Repository } from 'typeorm'; import { MapDrawingEntity } from '../draw/coordinate.entity'; import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; +import { NotificationGateway } from 'src/notifications/notifications.gateway'; @Injectable() export class DrawService { constructor( @InjectRepository(MapDrawingEntity) private mapDrawingRepository: Repository<MapDrawingEntity>, + private notificationGateway: NotificationGateway, ) {} async draw(gameId, data: MapDrawingDTO) { data['gameId'] = gameId; const drawing = await this.mapDrawingRepository.create(data); + this.notificationGateway.server.emit(gameId, { + type: 'drawing-update', + }); if (data.mapDrawingId == null || data.mapDrawingId == '') { // luo uuden instanssin. const mapDrawing = await this.mapDrawingRepository.insert(drawing); diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 8d7494ba4dfa4a77e22963600e45a7579fca5d5d..a0b85a24e645f99bff5d1b4175bc6e9b24d1353f 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -55,8 +55,13 @@ export class GameController { } @Get('listgames') - async listGames() { - return this.gameservice.listGames(); + async listGames(state) { + return this.gameservice.listGames(state); + } + + @Get('listgames/:state') + async listGamesState(@Param('state') state: string) { + return this.gameservice.listGames(state); } // ClassSerializerInterceptor removes excluded columns set in Entities diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index 266a8b1564e5b11fb42da755359c79d37f4b170b..abf1742d2af2bd9e10eed82b264217af11f96d8c 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -76,7 +76,7 @@ export class newGameDTO { export class GameStateDTO { @IsUUID('4') id: string; - @IsIn(['CREATED', 'STARTED', 'PAUSED', 'ENDED']) + @IsIn(['CREATED', 'STARTED', 'PAUSED', 'ENDED', 'ONGOING']) state: string; } diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 3f6506b649f7dbe09936e283b4658f9765be8ec4..391d812fb2cad207ded708cf5bf619e0065b5009 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -170,11 +170,31 @@ export class GameService { } // returns name and id of each game - async listGames() { - const games = await this.gameRepository.find(); - return games.map(game => { - return game.gameObject(); - }); + async listGames(state) { + if(state == null){ + const games = await this.gameRepository.find(); + return games.map(game => { + return game.gameObject(); + }); + } + else if(state == 'ONGOING'){ + const games = await this.gameRepository.find({ + where: [ + {state: 'CREATED'}, {state: 'STARTED'}, {state: 'PAUSED'}, + ] + }); + return games.map(game => { + return game.gameObject(); + }); + } + else{ + const games = await this.gameRepository.find({ + where: {state: state} + }); + return games.map(game => { + return game.gameObject(); + }); + } } // returns information about a game identified by id diff --git a/src/score/score.controller.ts b/src/score/score.controller.ts index 554e0617591574ce5c34b3dff8b5fdbaacab4ca4..73fda22e699f5702b1616360bd7e95f0ac8aeb10 100644 --- a/src/score/score.controller.ts +++ b/src/score/score.controller.ts @@ -26,4 +26,11 @@ export class ScoreController { async scoreTick(@Param('id') gameId: GameEntity) { return this.scoreService.scoreTick(gameId); } + + // shows scores, :id is gameId + @Get('get-score/:id') + @GameStates('STARTED') + async getScores(@Param('id') gameId: GameEntity) { + return this.scoreService.getScores(gameId); + } } diff --git a/src/score/score.service.ts b/src/score/score.service.ts index aed55353afcbbbba37be2f84c38239d8d4b4fc07..dba8e6e79206322a4dffae937b5af6b2bdd89041 100644 --- a/src/score/score.service.ts +++ b/src/score/score.service.ts @@ -85,6 +85,31 @@ export class ScoreService { message: 'Scores added', }; } + + async getScores(gameId: GameEntity) { + // find games factions + const factions = await this.factionRepository.find({ + where: {game: gameId,}, + relations: ['game'], + }); + let scores = []; + await Promise.all( + factions.map(async factionNow => { + let score = await this.scoreRepository.findOne({ + where: {faction: factionNow}, + relations: ['faction'], + 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, + ); + scores.push(score); + } + })) + return scores; + } } // // Hae kaikki Objective pointit diff --git a/src/tracking/geo.dto.ts b/src/tracking/geo.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..352faf704db72912a548734185d277253d67c789 --- /dev/null +++ b/src/tracking/geo.dto.ts @@ -0,0 +1,14 @@ +import { IsNumber, Min, Max, Allow } from 'class-validator'; + +export class GeoDTO { + @IsNumber() + @Min(-90) + @Max(90) + lat: number; + @IsNumber() + @Min(-180) + @Max(180) + lng: number; + @Allow() + time: number; +} diff --git a/src/tracking/tracking.controller.ts b/src/tracking/tracking.controller.ts index 0ccbb85315bcc9d36e08c3e6e5517fcf4b028595..f360ac6c6ea3459d8b344f32b88432f3260ce3dc 100644 --- a/src/tracking/tracking.controller.ts +++ b/src/tracking/tracking.controller.ts @@ -15,6 +15,7 @@ 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'; @Controller('tracking') export class TrackingController { @@ -29,7 +30,7 @@ export class TrackingController { async trackLocation( @User('id') userId, @Param('id') id, - @Body() trackdata: TrackingDTO, + @Body() trackdata: GeoDTO, ) { return this.trackingservice.trackLocation(userId, id, trackdata); } diff --git a/src/tracking/tracking.dto.ts b/src/tracking/tracking.dto.ts index 8dc949c954931fb730a455ba2096f89fe9bb5fec..81cc71597fa93910cb195b1fd4e30f38c62c1f05 100644 --- a/src/tracking/tracking.dto.ts +++ b/src/tracking/tracking.dto.ts @@ -1,8 +1,10 @@ import { Game_PersonEntity } from '../game/game.entity'; -import { Allow } from 'class-validator'; +import { Allow, ValidateNested } from 'class-validator'; +import { GeoDTO } from './geo.dto'; +import { Type } from 'class-transformer'; export class TrackingDTO { - @Allow() - data: JSON; - gamepersonId: Game_PersonEntity; + @ValidateNested() + @Type(() => GeoDTO) + data: GeoDTO; } diff --git a/src/tracking/tracking.entity.ts b/src/tracking/tracking.entity.ts index 7af1ca40806131b1a9517ae5489dc649184d56b4..c2699a09388ce8ad47c7119b46f4614fa52b34ec 100644 --- a/src/tracking/tracking.entity.ts +++ b/src/tracking/tracking.entity.ts @@ -1,11 +1,13 @@ import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; import { Game_PersonEntity, GameEntity } from '../game/game.entity'; import { FactionEntity } from 'src/faction/faction.entity'; +import { GeoDTO } from './geo.dto'; @Entity('Tracking') export class TrackingEntity { @PrimaryGeneratedColumn('uuid') id: string; - @Column({ type: 'json', nullable: true }) data: JSON; + @Column({ type: 'json', nullable: true }) data: GeoDTO[]; + @ManyToOne(type => Game_PersonEntity, person => person.gamepersonId, { onDelete: 'CASCADE', }) diff --git a/src/tracking/tracking.service.ts b/src/tracking/tracking.service.ts index 3c6453406bf96a2817dd5d8e2c382fb3f5741eb3..f180c7a464b290f0a21e621d28116038a6dc1a38 100644 --- a/src/tracking/tracking.service.ts +++ b/src/tracking/tracking.service.ts @@ -6,7 +6,8 @@ import { Game_PersonEntity } from '../game/game.entity'; import { TrackingEntity } from './tracking.entity'; import { TrackingDTO } from './tracking.dto'; import { FactionEntity } from '../faction/faction.entity'; -import { PersonEntity } from '../user/user.entity'; +import { NotificationGateway } from 'src/notifications/notifications.gateway'; +import { GeoDTO } from './geo.dto'; @Injectable() export class TrackingService { @@ -15,11 +16,9 @@ export class TrackingService { private trackingrepository: Repository<TrackingEntity>, @InjectRepository(Game_PersonEntity) private gamepersonrepository: Repository<Game_PersonEntity>, - @InjectRepository(PersonEntity) - private personrepository: Repository<PersonEntity>, ) {} - async trackLocation(personId, gameId, trackdata: TrackingDTO) { + async trackLocation(personId, gameId, trackdata: GeoDTO) { // find player let gameperson = await this.gamepersonrepository.findOne({ where: { game: gameId, person: personId }, @@ -37,27 +36,24 @@ export class TrackingService { // if player has pushed tracking data, update entry if (trackedperson) { + trackdata['time'] = Date.now(); //add coordinates - trackedperson.data['geometry']['coordinates'].push( - await this.mapFunction(trackdata.data['geometry']['coordinates']), - ); + trackedperson.data.push(trackdata); //add timestamp - trackedperson.data['geometry']['properties']['time'].push( - new Date(Date.now()), - ); - - return await this.trackingrepository.save(trackedperson); + await this.trackingrepository.save(trackedperson); + return { code: 201, message: 'Location updated!' }; } else { // first entry will be empty - // initialize coordinates - trackdata.data['geometry']['coordinates'] = []; - // initialize timestamp - trackdata.data['geometry']['properties']['time'] = []; - trackedperson = await this.trackingrepository.create(trackdata); + trackdata['time'] = Date.now(); + // initialize data + trackedperson = await this.trackingrepository.create(trackedperson); + trackedperson.data = [trackdata]; trackedperson.faction = gameperson.faction; trackedperson.game = gameId; trackedperson.gamepersonId = gameperson; - return await this.trackingrepository.save(trackedperson); + await this.trackingrepository.save(trackedperson); + + return { code: 201, message: 'Entry Created!' }; } } @@ -90,7 +86,7 @@ export class TrackingService { gamepersonId: player['gamepersonId']['gamepersonId'], gamepersonRole: player['gamepersonId']['role'], factionId: player['faction']['factionId'], - coordinates: player['data']['geometry']['coordinates'].pop(), + coordinates: player['data'].pop(), }; }), );