Feat : initial market population
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||
"[typescript]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
}
|
||||
}
|
||||
@@ -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'}]})"
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user