Feat : implement per for effects

This commit is contained in:
2024-07-07 18:26:47 +02:00
parent 8b5c38a28c
commit a3907716cd
6 changed files with 212 additions and 28 deletions
+53 -13
View File
@@ -12,7 +12,12 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { Effect } from 'src/cards/schemas/effect.schema';
import { CardEffects, EffectType } from 'src/cards/schemas/cards.types';
import { WithTransaction } from '../utils';
import { effectMappings, isAutoActivable } from './effect.functions';
import {
conditionsMappings,
effectMappings,
isAutoActivable,
perMapping,
} from './effect.functions';
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
import { GameService } from './games.service';
@@ -55,14 +60,37 @@ export class TurnService {
session?: mongoose.mongo.ClientSession,
) {
const effectRunner = effectMappings[effect.effect];
if (!effectMappings[effect.effect]) {
console.warn(`Effect ${effect.effect} not implemented`);
return;
if (!effectRunner) {
throw new HttpException(
`Effect ${effect.effect} not implemented`,
HttpStatus.NOT_IMPLEMENTED,
);
}
// Process effect conditions
if (effect.condition) {
const effectCondition = conditionsMappings[effect.condition.effectId];
if (!effectCondition) {
throw new HttpException(
`Condition ${effect.condition.effectId} not implemented`,
HttpStatus.NOT_IMPLEMENTED,
);
}
const conditionSuccess = await effectCondition(
game,
player,
card,
effect.condition,
this.gamesService,
session,
);
if (!conditionSuccess) {
throw new HttpException(
`Condition ${effect.condition.effectId} not met`,
HttpStatus.BAD_REQUEST,
);
}
}
const effectUUID = effect._id.toHexString();
if (game.currentTurn.lockedEffects.some((e) => e._id.equals(effectUUID))) {
console.error(`Effect ${effectUUID} already used this turn`);
@@ -75,6 +103,9 @@ export class TurnService {
type: 'lockEffect',
effect: effectUUID,
});
// Process effect mutexes
if (effect.mutex) {
for (const e of card.effects) {
if (e.mutex == effect.mutex) {
@@ -86,16 +117,25 @@ export class TurnService {
}
}
}
// Process effect Per
game.currentTurn.lockedEffects.push(effect);
await effectRunner(
game,
effect,
player,
card,
this.gamesService,
this.eventEmitter,
session,
);
let times = 1;
if (effect.per) {
times = await perMapping[effect.per](game, card);
}
for (let t = 0; t < times; t++) {
await effectRunner(
game,
effect,
player,
card,
this.gamesService,
this.eventEmitter,
session,
);
}
await game.save({ session });
}