Feat : Lock champions at start of game

This commit is contained in:
2024-07-05 16:49:01 +02:00
parent 9d0cdca6e9
commit aca90efc5d
3 changed files with 67 additions and 20 deletions
+27 -5
View File
@@ -1,9 +1,11 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Game, Player } from '../schemas/game.schema';
import { Document } from 'mongoose';
import mongoose, { Document } from 'mongoose';
import { Card } from 'src/cards/schemas/cards.schema';
import { CardEffects, EffectType } from 'src/cards/schemas/cards.types';
import { Effect } from 'src/cards/schemas/effect.schema';
import { TurnService } from './turn.service';
import { GameService } from './games.service';
export function isAutoActivable(effect: Effect) {
if (
@@ -17,12 +19,14 @@ export function isAutoActivable(effect: Effect) {
}
export const effectMappings = {
[CardEffects.GOLD]: (
[CardEffects.GOLD]: async (
game: Document<Game> & Game,
effect: Effect,
_player: Player,
_card: Card,
_gameService: GameService,
eventEmitter: EventEmitter2,
_session: mongoose.mongo.ClientSession,
) => {
game.currentTurn.gold += effect.amount;
eventEmitter.emit('sse.game.' + game._id.toHexString(), {
@@ -32,12 +36,14 @@ export const effectMappings = {
});
},
[CardEffects.DAMAGE]: (
[CardEffects.DAMAGE]: async (
game: Document<Game> & Game,
effect: Effect,
_player: Player,
_card: Card,
_gameService: GameService,
eventEmitter: EventEmitter2,
_session: mongoose.mongo.ClientSession,
) => {
game.currentTurn.damage += effect.amount;
eventEmitter.emit('sse.game.' + game._id.toHexString(), {
@@ -47,12 +53,14 @@ export const effectMappings = {
});
},
[CardEffects.HEAL]: (
[CardEffects.HEAL]: async (
game: Document<Game> & Game,
effect: Effect,
player: Player,
_card: Card,
_gameService: GameService,
eventEmitter: EventEmitter2,
_session: mongoose.mongo.ClientSession,
) => {
const team = game.teams.find((t) =>
t.players.some((p) => p._id.equals(player._id)),
@@ -65,6 +73,18 @@ export const effectMappings = {
operation: effect.amount,
});
},
[CardEffects.DRAW]: async (
game: Document<Game> & Game,
effect: Effect,
player: Player,
_card: Card,
gameService: GameService,
_eventEmitter: EventEmitter2,
session: mongoose.mongo.ClientSession,
) => {
await gameService.drawToBoard(effect.amount, player, game, session);
},
} as Record<
CardEffects,
(
@@ -72,6 +92,8 @@ export const effectMappings = {
effect: Effect,
player: Player,
card: Card,
gameService: GameService,
eventEmitter: EventEmitter2,
) => void
session: mongoose.mongo.ClientSession,
) => Promise<void>
>;
+21 -3
View File
@@ -1,18 +1,26 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
} from '@nestjs/common';
import { InjectModel, InjectConnection } from '@nestjs/mongoose';
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 { WithTransaction, getCurrentTeam, shuffle } from '../utils';
import { TurnService } from './turn.service';
@Injectable()
export class GameService {
constructor(
@Inject(forwardRef(() => TurnService))
private readonly turnService: TurnService,
@InjectModel(Game.name) private gameModel: Model<Game>,
@InjectModel(Card.name) private cardModel: Model<Card>,
@InjectConnection() private readonly connection: mongoose.Connection,
private eventEmitter: EventEmitter2,
) {}
@@ -35,6 +43,7 @@ export class GameService {
}
const index = game.teams.indexOf(currentTeam);
game.currentTurn.currentTeam =
game.teams[(index + 1) % game.teams.length]._id.toHexString();
@@ -43,6 +52,7 @@ export class GameService {
game.currentTurn.damage = 0;
game.currentTurn.gold = 0;
await game.save({ session });
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
type: 'endTurn',
currentTurn: game.currentTurn,
@@ -50,6 +60,14 @@ export class GameService {
currentTeam = getCurrentTeam(game);
for (const player of currentTeam.players) {
for (const card of player.board.cards) {
if (card.defense) {
await this.turnService.lockCard(game, player, card, session);
}
}
}
for (const player of currentTeam.players) {
await this.distributeCards(
player.hand.cards,
@@ -193,7 +211,7 @@ export class GameService {
}
const shallow = [...cards];
for (const c of shallow) {
const index = from.cards.indexOf(c);
const index = from.cards.findIndex((cc) => cc?._id.equals(c._id));
from.cards.splice(index, 1);
to.cards.push(c);
}
+19 -12
View File
@@ -1,4 +1,10 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
} from '@nestjs/common';
import { Game, Player } from '../schemas/game.schema';
import mongoose, { Document, Model } from 'mongoose';
import { Card } from 'src/cards/schemas/cards.schema';
@@ -6,7 +12,7 @@ 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 } from './effect.functions';
import { effectMappings, isAutoActivable } from './effect.functions';
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
import { GameService } from './games.service';
@@ -14,6 +20,7 @@ import { GameService } from './games.service';
export class TurnService {
constructor(
private eventEmitter: EventEmitter2,
@Inject(forwardRef(() => GameService))
private readonly gamesService: GameService,
@InjectModel(Game.name) private gameModel: Model<Game>,
@InjectConnection() private readonly connection: mongoose.Connection,
@@ -32,15 +39,7 @@ export class TurnService {
card: card._id.toHexString(),
});
for (const effect of card.effects) {
if (
[CardEffects.HEAL, CardEffects.GOLD, CardEffects.DAMAGE].includes(
effect.effect,
) &&
!effect.mutex &&
!effect.per &&
(!effect.condition ||
effect.condition.effectId == EffectType.CHAMPION_ACTION)
) {
if (isAutoActivable(effect)) {
await this.useEffect(game, player, effect, card, session);
}
}
@@ -77,7 +76,15 @@ export class TurnService {
effect: effectUUID,
});
game.currentTurn.effectsUsed.push(effectUUID);
await effectRunner(game, effect, player, card, this.eventEmitter);
await effectRunner(
game,
effect,
player,
card,
this.gamesService,
this.eventEmitter,
session,
);
await game.save({ session });
}