From ebbb07829ff2a0598f247723e01057cda3c70071 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Tue, 2 Jul 2024 11:44:58 +0200 Subject: [PATCH] Feat : initial market population --- .env.dev | 3 +- .vscode/settings.json | 6 ++++ docker-compose.yml | 5 ++- src/games/services/games.lobby.service.ts | 41 +++++++++++------------ src/games/services/games.service.ts | 33 ++++++++++++++++-- 5 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.env.dev b/.env.dev index b0a8f7c..ca63f61 100644 --- a/.env.dev +++ b/.env.dev @@ -1,9 +1,10 @@ SESSION_SECRET=dev +API_ENDPOINT=http://localhost:8765 DISCORD_CLIENTID= DISCORD_CLIENTSECRET= -MONGO_ENDPOINT=127.0.0.1 +MONGO_ENDPOINT=mongodb://127.0.0.1 MONGO_PASSWORD=example MONGO_USER=root diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b55450f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "editor.defaultFormatter": "esbenp.prettier-vscode", + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 4e27d04..509e636 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,8 +10,11 @@ services: MONGO_INITDB_ROOT_PASSWORD: example ports: - 27017:27017 +# entrypoint: bash -c "chown 999:999 /opt/keyfile/mongodb-keyfile && chmod 400 /opt/keyfile/mongodb-keyfile && exec docker-entrypoint.sh $$@" command: mongod --replSet rs0 --keyFile /opt/keyfile/mongodb-keyfile volumes: - ./:/opt/keyfile/ + groups: + - 999 -# docker run --rm -it --network=host --name mongoContainer mongo:latest mongosh mongodb://127.0.0.1:27017 -u root -p example --eval "rs.initiate({'_id':'rs0', members: [{'_id':1, 'host':'127.0.0.1:27017'}]})" \ No newline at end of file +# docker run --rm -it --network=host --name mongoContainer mongo:latest mongosh mongodb://127.0.0.1:27017 -u root -p example --eval "rs.initiate({'_id':'rs0', members: [{'_id':1, 'host':'127.0.0.1:27017'}]})" diff --git a/src/games/services/games.lobby.service.ts b/src/games/services/games.lobby.service.ts index 3dff0e6..fe44fcd 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -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) { diff --git a/src/games/services/games.service.ts b/src/games/services/games.service.ts index 8235785..55770c6 100644 --- a/src/games/services/games.service.ts +++ b/src/games/services/games.service.ts @@ -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, + 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,