Feat : distribute cards at start of game

This commit is contained in:
2024-06-11 19:24:43 -04:00
parent 1d272b7761
commit 9761b4b37f
3 changed files with 63 additions and 7 deletions
+7 -6
View File
@@ -47,14 +47,15 @@ export class ReadContainerDto extends DatabaseObjectDto {
data: { cards: Card[] } & ConstructorParameters<
typeof DatabaseObjectDto
>[0],
hidden = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_hidden = false,
) {
super(data);
if (hidden) {
this.cards = data.cards.map(() => null);
} else {
this.cards = data.cards.map((e) => e);
}
// if (hidden) {
// this.cards = data.cards.map(() => null);
// } else {
this.cards = data.cards;
// }
}
cards: Card[];
}
+11
View File
@@ -143,6 +143,17 @@ export class LobbyService {
});
await this.gamesService.shuffleContainer(game.marketStack, game);
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);
} else {
this.gamesService.drawToHand(5, player, game);
}
}
}
await game.save();
}
+45 -1
View File
@@ -1,6 +1,6 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Container, Game } from '../schemas/game.schema';
import { Container, Game, Player } from '../schemas/game.schema';
import { Document, Model } from 'mongoose';
import { Card } from 'src/cards/schemas/cards.schema';
import { EventEmitter2 } from '@nestjs/event-emitter';
@@ -24,6 +24,50 @@ export class GameService {
private eventEmitter: EventEmitter2,
) {}
async drawToBoard(amount, player: Player, game: Document<Game> & Game) {
await this.drawCards(
amount,
player.stack,
player.board,
player.discard,
game,
);
}
async drawToHand(amount, player: Player, game: Document<Game> & Game) {
await this.drawCards(
amount,
player.stack,
player.hand,
player.discard,
game,
);
}
private async drawCards(
amount: number,
from: Container,
to: Container,
discard: Container,
game: Document<Game> & Game,
) {
if (amount >= from.cards.length) {
await this.distributeCards(from.cards.slice(0, amount), from, to, game);
await game.save();
return;
} else {
const toDraw = from.cards.length;
await this.distributeCards(from.cards.slice(0, toDraw), from, to, game);
if (discard.cards.length == 0) {
await game.save();
return;
}
// restack all discarded
await this.distributeCards(discard.cards, discard, from, game);
await this.shuffleContainer(from, game);
await this.drawCards(amount - toDraw, from, to, discard, game);
}
}
async shuffleContainer(container: Container, game: Document<Game> & Game) {
container.cards = shuffle(container.cards);
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {