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 { ReadUserDto } from '../dto/read-games.dto';
|
||||||
import { Observable, fromEvent, map } from 'rxjs';
|
import { Observable, fromEvent, map } from 'rxjs';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
|
import { GameService } from '../services/games.service';
|
||||||
|
|
||||||
function getPlayer(session: Record<string, any>, game: Game) {
|
function getPlayer(session: Record<string, any>, game: Game) {
|
||||||
const user = session.user as ReadUserDto;
|
const user = session.user as ReadUserDto;
|
||||||
@@ -44,6 +45,7 @@ function getPlayer(session: Record<string, any>, game: Game) {
|
|||||||
export class GamesController {
|
export class GamesController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly lobbyService: LobbyService,
|
private readonly lobbyService: LobbyService,
|
||||||
|
private readonly gameService: GameService,
|
||||||
private eventEmitter: EventEmitter2,
|
private eventEmitter: EventEmitter2,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -66,6 +68,30 @@ export class GamesController {
|
|||||||
await this.lobbyService.changeStartingDeck(startingDeckDto, player, game);
|
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')
|
@Sse('/subscribe')
|
||||||
subscribe(
|
subscribe(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
|
|||||||
@@ -90,10 +90,7 @@ export class ReadGameDto extends DatabaseObjectDto {
|
|||||||
this.ended = obj.ended;
|
this.ended = obj.ended;
|
||||||
this.owner = new ReadUserDto(obj.owner);
|
this.owner = new ReadUserDto(obj.owner);
|
||||||
this.packs = obj.packs;
|
this.packs = obj.packs;
|
||||||
|
this.currentTurn = obj.currentTurn;
|
||||||
if (obj.currentTurn) {
|
|
||||||
this.currentTurn = new ReadTeamDto(obj.currentTurn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@@ -109,7 +106,7 @@ export class ReadGameDto extends DatabaseObjectDto {
|
|||||||
fireGems: ReadContainerDto;
|
fireGems: ReadContainerDto;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
currentTurn?: ReadTeamDto;
|
currentTurn?: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
started: boolean;
|
started: boolean;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
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 { Card } from 'src/cards/schemas/cards.schema';
|
||||||
import { CardRole } from 'src/cards/schemas/cards.types';
|
import { CardRole } from 'src/cards/schemas/cards.types';
|
||||||
import { User } from 'src/users/schemas/user.entity';
|
import { User } from 'src/users/schemas/user.entity';
|
||||||
@@ -72,8 +72,8 @@ export class Game extends Types.ObjectId {
|
|||||||
@Prop({ type: ContainerSchema, default: {}, autopopulate: true })
|
@Prop({ type: ContainerSchema, default: {}, autopopulate: true })
|
||||||
fireGems: Container;
|
fireGems: Container;
|
||||||
|
|
||||||
@Prop({ type: Team, autopopulate: true })
|
@Prop()
|
||||||
currentTurn?: Team;
|
currentTurn?: string;
|
||||||
|
|
||||||
@Prop({ default: false })
|
@Prop({ default: false })
|
||||||
started: boolean;
|
started: boolean;
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ export class LobbyService {
|
|||||||
|
|
||||||
async start(game: Document<Game> & Game) {
|
async start(game: Document<Game> & Game) {
|
||||||
const random = Math.floor(Math.random() * game.teams.length);
|
const random = Math.floor(Math.random() * game.teams.length);
|
||||||
game.currentTurn = game.teams[random];
|
game.currentTurn = game.teams[random]._id.toHexString();
|
||||||
await game.save();
|
await game.save();
|
||||||
game.started = true;
|
game.started = true;
|
||||||
this.eventEmitter.emit('sse.lobby', {
|
this.eventEmitter.emit('sse.lobby', {
|
||||||
@@ -116,7 +116,7 @@ export class LobbyService {
|
|||||||
type: 'populateContainer',
|
type: 'populateContainer',
|
||||||
container: new ReadContainerDto(player.stack, true),
|
container: new ReadContainerDto(player.stack, true),
|
||||||
});
|
});
|
||||||
this.gamesService.shuffleContainer(player.stack, game);
|
await this.gamesService.shuffleContainer(player.stack, game);
|
||||||
playerIndex++;
|
playerIndex++;
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
@@ -146,10 +146,10 @@ export class LobbyService {
|
|||||||
|
|
||||||
for (const team of game.teams) {
|
for (const team of game.teams) {
|
||||||
for (const player of team.players) {
|
for (const player of team.players) {
|
||||||
if (team._id.toHexString() == game.currentTurn._id.toHexString()) {
|
if (team._id.toHexString() == game.currentTurn) {
|
||||||
this.gamesService.drawToHand(3, player, game);
|
await this.gamesService.drawToHand(3, player, game);
|
||||||
} else {
|
} else {
|
||||||
this.gamesService.drawToHand(5, player, game);
|
await this.gamesService.drawToHand(5, player, game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,43 @@ export class GameService {
|
|||||||
private eventEmitter: EventEmitter2,
|
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) {
|
async drawToBoard(amount, player: Player, game: Document<Game> & Game) {
|
||||||
await this.drawCards(
|
await this.drawCards(
|
||||||
amount,
|
amount,
|
||||||
|
|||||||
Reference in New Issue
Block a user