Fix : set current_turn to string
This commit is contained in:
@@ -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<string, any>, game: Game) {
|
||||
const user = session.user as ReadUserDto;
|
||||
@@ -44,6 +45,7 @@ function getPlayer(session: Record<string, any>, 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> & Game,
|
||||
@Session() session: Record<string, string>,
|
||||
) {
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -89,7 +89,7 @@ export class LobbyService {
|
||||
|
||||
async start(game: Document<Game> & 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,43 @@ export class GameService {
|
||||
private eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
async endTurn(game: Document<Game> & 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> & Game) {
|
||||
await this.drawCards(
|
||||
amount,
|
||||
|
||||
Reference in New Issue
Block a user