From 9761b4b37ff0c6ac116a5ce455377e1b8fc769ca Mon Sep 17 00:00:00 2001 From: Legonzaur Date: Tue, 11 Jun 2024 19:24:43 -0400 Subject: [PATCH] Feat : distribute cards at start of game --- src/games/dto/read-games.dto.ts | 13 ++++--- src/games/services/games.lobby.service.ts | 11 ++++++ src/games/services/games.service.ts | 46 ++++++++++++++++++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/games/dto/read-games.dto.ts b/src/games/dto/read-games.dto.ts index 55c3927..95dc009 100644 --- a/src/games/dto/read-games.dto.ts +++ b/src/games/dto/read-games.dto.ts @@ -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[]; } diff --git a/src/games/services/games.lobby.service.ts b/src/games/services/games.lobby.service.ts index 1c64737..1edc197 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -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(); } diff --git a/src/games/services/games.service.ts b/src/games/services/games.service.ts index 2de949d..cfab83e 100644 --- a/src/games/services/games.service.ts +++ b/src/games/services/games.service.ts @@ -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) { + await this.drawCards( + amount, + player.stack, + player.board, + player.discard, + game, + ); + } + + async drawToHand(amount, player: Player, game: Document & 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, + ) { + 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) { container.cards = shuffle(container.cards); this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {