From da7262fbebe2004d1fd75512df5c22ddca5c0291 Mon Sep 17 00:00:00 2001 From: Legonzaur Date: Tue, 11 Jun 2024 20:23:32 -0400 Subject: [PATCH] Fix : set current_turn to string --- src/games/controllers/games.controller.ts | 26 ++++++++++++++++ src/games/dto/read-games.dto.ts | 7 ++--- src/games/schemas/game.schema.ts | 6 ++-- src/games/services/games.lobby.service.ts | 10 +++--- src/games/services/games.service.ts | 37 +++++++++++++++++++++++ 5 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/games/controllers/games.controller.ts b/src/games/controllers/games.controller.ts index 63a2510..d61ccb2 100644 --- a/src/games/controllers/games.controller.ts +++ b/src/games/controllers/games.controller.ts @@ -20,6 +20,7 @@ import { GamePipe } from '../pipe/game.pipe'; import { ReadUserDto } from '../dto/read-games.dto'; import { Observable, fromEvent, map } from 'rxjs'; import { EventEmitter2 } from '@nestjs/event-emitter'; +import { GameService } from '../services/games.service'; function getPlayer(session: Record, game: Game) { const user = session.user as ReadUserDto; @@ -44,6 +45,7 @@ function getPlayer(session: Record, game: Game) { export class GamesController { constructor( private readonly lobbyService: LobbyService, + private readonly gameService: GameService, private eventEmitter: EventEmitter2, ) {} @@ -66,6 +68,30 @@ export class GamesController { await this.lobbyService.changeStartingDeck(startingDeckDto, player, game); } + @Post('endTurn') + @UseGuards(AuthGuard) + async endTurn( + @Param('id') id: string, + @Param('id', GamePipe) game: Document & Game, + @Session() session: Record, + ) { + const player = getPlayer(session, game); + const currentTurn = game.teams.find( + (t) => t._id.toHexString() == game.currentTurn, + ); + if ( + !currentTurn.players.some( + (p) => p._id.toHexString() == player._id.toHexString(), + ) + ) { + throw new HttpException( + 'It is not your turn to play', + HttpStatus.FORBIDDEN, + ); + } + await this.gameService.endTurn(game); + } + @Sse('/subscribe') subscribe( @Param('id') id: string, diff --git a/src/games/dto/read-games.dto.ts b/src/games/dto/read-games.dto.ts index 95dc009..7ae4879 100644 --- a/src/games/dto/read-games.dto.ts +++ b/src/games/dto/read-games.dto.ts @@ -90,10 +90,7 @@ export class ReadGameDto extends DatabaseObjectDto { this.ended = obj.ended; this.owner = new ReadUserDto(obj.owner); this.packs = obj.packs; - - if (obj.currentTurn) { - this.currentTurn = new ReadTeamDto(obj.currentTurn); - } + this.currentTurn = obj.currentTurn; } @ApiProperty() @@ -109,7 +106,7 @@ export class ReadGameDto extends DatabaseObjectDto { fireGems: ReadContainerDto; @ApiProperty() - currentTurn?: ReadTeamDto; + currentTurn?: string; @ApiProperty() started: boolean; diff --git a/src/games/schemas/game.schema.ts b/src/games/schemas/game.schema.ts index cef7737..b72421e 100644 --- a/src/games/schemas/game.schema.ts +++ b/src/games/schemas/game.schema.ts @@ -1,5 +1,5 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; -import mongoose, { HydratedDocument, Types } from 'mongoose'; +import mongoose, { HydratedDocument, Model, ObjectId, Types } from 'mongoose'; import { Card } from 'src/cards/schemas/cards.schema'; import { CardRole } from 'src/cards/schemas/cards.types'; import { User } from 'src/users/schemas/user.entity'; @@ -72,8 +72,8 @@ export class Game extends Types.ObjectId { @Prop({ type: ContainerSchema, default: {}, autopopulate: true }) fireGems: Container; - @Prop({ type: Team, autopopulate: true }) - currentTurn?: Team; + @Prop() + currentTurn?: string; @Prop({ default: false }) started: boolean; diff --git a/src/games/services/games.lobby.service.ts b/src/games/services/games.lobby.service.ts index 1edc197..0ac766f 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -89,7 +89,7 @@ export class LobbyService { async start(game: Document & Game) { const random = Math.floor(Math.random() * game.teams.length); - game.currentTurn = game.teams[random]; + game.currentTurn = game.teams[random]._id.toHexString(); await game.save(); game.started = true; this.eventEmitter.emit('sse.lobby', { @@ -116,7 +116,7 @@ export class LobbyService { type: 'populateContainer', container: new ReadContainerDto(player.stack, true), }); - this.gamesService.shuffleContainer(player.stack, game); + await this.gamesService.shuffleContainer(player.stack, game); playerIndex++; }), ), @@ -146,10 +146,10 @@ export class LobbyService { for (const team of game.teams) { for (const player of team.players) { - if (team._id.toHexString() == game.currentTurn._id.toHexString()) { - this.gamesService.drawToHand(3, player, game); + if (team._id.toHexString() == game.currentTurn) { + await this.gamesService.drawToHand(3, player, game); } else { - this.gamesService.drawToHand(5, player, game); + await this.gamesService.drawToHand(5, player, game); } } } diff --git a/src/games/services/games.service.ts b/src/games/services/games.service.ts index cfab83e..6dcbdaf 100644 --- a/src/games/services/games.service.ts +++ b/src/games/services/games.service.ts @@ -24,6 +24,43 @@ export class GameService { private eventEmitter: EventEmitter2, ) {} + async endTurn(game: Document & Game) { + let currentTurn = game.teams.find( + (t) => t._id.toHexString() == game.currentTurn, + ); + for (const player of currentTurn.players) { + await this.distributeCards( + player.board.cards.filter((c) => !c.defense), + player.board, + player.discard, + game, + ); + } + + const index = game.teams.indexOf(currentTurn); + game.currentTurn = + game.teams[(index + 1) % game.teams.length]._id.toHexString(); + await game.save(); + + this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { + type: 'endTurn', + currentTurn: game.currentTurn, + }); + + currentTurn = game.teams.find( + (t) => t._id.toHexString() == game.currentTurn, + ); + + for (const player of currentTurn.players) { + await this.distributeCards( + player.hand.cards, + player.hand, + player.board, + game, + ); + } + } + async drawToBoard(amount, player: Player, game: Document & Game) { await this.drawCards( amount,