Feat : initial market population

This commit is contained in:
2024-07-02 11:44:58 +02:00
parent d4840eab8b
commit ebbb07829f
5 changed files with 61 additions and 27 deletions
+19 -22
View File
@@ -101,28 +101,25 @@ export class LobbyService {
type: 'start',
game: new ReadGameDto(game),
});
let playerIndex = 0;
await Promise.all(
game.teams.map((team) =>
Promise.all(
team.players.map(async (player) => {
player.stack.cards = await this.cardModel
.find({
pack: { $in: game.packs },
role: { $in: [player.class, player.race] },
playerIndex,
})
.exec();
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
type: 'populateContainer',
container: new ReadContainerDto(player.stack, true),
});
await this.gamesService.shuffleContainer(player.stack, game);
playerIndex++;
}),
),
),
);
for (const team of game.teams) {
for (const player of team.players) {
player.stack.cards = await this.cardModel
.find({
pack: { $in: game.packs },
role: { $in: [player.class, player.race] },
playerIndex,
})
.exec();
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
type: 'populateContainer',
container: new ReadContainerDto(player.stack, true),
});
await this.gamesService.shuffleContainer(player.stack, game);
playerIndex++;
}
}
game.fireGems.cards = await this.cardModel.find({
pack: { $in: game.packs },
@@ -144,7 +141,7 @@ export class LobbyService {
});
await this.gamesService.shuffleContainer(game.marketStack, game);
await this.gamesService.fillMarket(game);
for (const team of game.teams) {
for (const player of team.players) {
if (team._id.toHexString() == game.currentTurn) {
+30 -3
View File
@@ -4,7 +4,6 @@ import { Container, Game, Player } from '../schemas/game.schema';
import mongoose, { Document, Model } from 'mongoose';
import { Card } from 'src/cards/schemas/cards.schema';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Console } from 'console';
function shuffle(a) {
let j, x, i;
@@ -33,8 +32,8 @@ function WithTransaction(
return originalFunc.apply(this, [...args, session]);
})
.catch((e) => {
if(e instanceof HttpException){
throw e
if (e instanceof HttpException) {
throw e;
}
throw new HttpException(
'Another request is processing',
@@ -182,6 +181,34 @@ export class GameService {
}
}
@WithTransaction
async fillMarket(
game: Document<Game> & Game,
session?: mongoose.mongo.ClientSession,
) {
if (game.market.cards.length < 5) {
const amount = Math.min(
game.marketStack.cards.length,
5 - game.market.cards.length,
);
await this.distributeCards(
game.marketStack.cards.slice(0, amount),
game.marketStack,
game.market,
game,
session,
);
}
const missingIndexes = game.market.cards.reduce(function (a, e, i) {
if (e === null) a.push(i);
return a;
}, []);
if (missingIndexes.length > 0) {
console.error('not implemented');
}
}
@WithTransaction
async shuffleContainer(
container: Container,