Versus fixes

This commit is contained in:
Kirill Ivlev 2024-11-13 03:33:03 +04:00
parent ac9116e138
commit 666240dfb8
10 changed files with 97 additions and 8 deletions

View file

@ -0,0 +1,13 @@
import {CommandHandler, ICommandHandler} from "@nestjs/cqrs";
import {BeginVersusCommand} from "../commands/begin-versus.command";
import {VersusService} from "../versus/versus.service";
@CommandHandler(BeginVersusCommand)
export class BeginVersusCommandHandler implements ICommandHandler<BeginVersusCommand> {
constructor(private versusService:VersusService) {
}
execute(command: BeginVersusCommand): Promise<any> {
return this.versusService.beginVersus(command.sourceId,command.destinationId);
}
}

View file

@ -0,0 +1,6 @@
export class BeginVersusCommand {
constructor(public sourceId: number, public destinationId: number) {
}
}

View file

@ -19,8 +19,9 @@ import {SetGuestPropertyCommand} from "../../guests/command/set-guest-property.c
import {StringHelper} from "../../helpers/stringhelper";
import {GetGuestQuery} from "../../guests/queries/getguest.query";
import {CardsSetChangedEvent} from "../events/cards-events/cards-set-changed.event";
import {GetGuestPropertyQuery} from "../../guests/command/get-guest-property.handler";
import {GuestPropertiesConsts} from "../../schemas/properties.consts";
import {BeginVersusCommand} from "../commands/begin-versus.command";
import {CheckIfAnotherVersusInProgressQuery} from "../queries/check-if-another-versus-in-progress.query";
export interface IGameCard {
setupHandlers(eventBus: EventBus, commandBus: CommandBus, queryBus: QueryBus): void;
@ -74,7 +75,7 @@ export class DoubleTreasureCard extends GameCard {
await this.commandBus.execute(
new GiveOutAPrizeCommand(this.telegramId),
);
const userSrc = await this.queryBus.execute(new GetGuestQuery(this.telegramId));;
const userSrc = await this.queryBus.execute(new GetGuestQuery(this.telegramId));
const subjcaseFrom = userSrc.get(StringHelper.getPropertyName(GuestPropertiesConsts.NameSubjectiveCase));
const message = `${subjcaseFrom} решает удвоить приз!`;
await this.commandBus.execute(new SendToastCommand(message, 8000));
@ -174,6 +175,45 @@ export class AvoidPenaltyCard extends GameCard {
}
}
@Injectable()
export class VersusCard extends GameCard {
dealOnStart = true;
name = VersusCard.name;
chance = 10;
emoji = '🆚';
description = 'Поединок';
mightBePlayed = QuizAnswerStateEnum.betweenRounds;
async setupHandlers(eventBus: EventBus, commandBus: CommandBus, queryBus: QueryBus) {
super.setupHandlers(eventBus, commandBus, queryBus);
eventBus.pipe(
ofType(DebuffCardPlayedEvent),
filter(x => x.debufName == DebuffsConsts.versus))
.subscribe(async (r) =>{
const versusInProgress = await queryBus.execute(new CheckIfAnotherVersusInProgressQuery());
this.logger.verbose(`versusInProgress ${versusInProgress}`);
if(versusInProgress) {
this.logger.warn(`another versus in progress`);
return;
}
const destUser = await queryBus.execute(new GetGuestQuery(r.dest))
const sourceUser = await queryBus.execute(new GetGuestQuery(r.from));
await commandBus.execute(new BeginVersusCommand(sourceUser.telegramId, destUser.telegramId));
});
//eventBus.pipe(ofType(DebuffCardPlayedEvent)).subscribe(r => console.log(r));
}
async handle() {
await this.commandBus.execute(
new SelectTargetPlayerCommand(this.telegramId, DebuffsConsts.versus, 0)
)
await this.queryBus.execute(new FilterGuestsWithPropertyQuery(null,null,null));
this.eventBus.subscribe((data) =>{
this.logger.verbose(`Response from cmdBus: ${data}`);
});
}
}
@Injectable()
export class BanPlayer extends GameCard {
dealOnStart = true;
@ -227,5 +267,6 @@ export const gameCards: typeof GameCard[] = [
ShitCard,
LuckyCard,
AvoidPenaltyCard,
BanPlayer
BanPlayer,
VersusCard,
];

View file

@ -1,3 +1,4 @@
export class DebuffsConsts {
static bannedFor = 'bannedFor';
static versus = 'versus';
}

View file

@ -20,6 +20,8 @@ import {GuestsModule} from "../guests/guests.module";
import { VersusService } from './versus/versus.service';
import { VersusController } from './versus/versus.controller';
import {Versus, VersusSchema} from "../schemas/versus.schema";
import {BeginVersusCommandHandler} from "./comand-handlers/begin-versus-command.handler";
import {CheckIfAnotherVersusInProgressHandler} from "./queries/handlers/check-if-another-versus-in-progress.handler";
const eventHandlers = [
@ -37,8 +39,11 @@ const commandHandlers = [
GamePrizeChanceIncreasedEventHandler,
GameProceedGameQueueCommandHandler,
SelectTargetPlayerHandler,
SendBetweenRoundsActionsHandler
SendBetweenRoundsActionsHandler,
BeginVersusCommandHandler,
];
const queryHandlers = [CheckIfAnotherVersusInProgressHandler];
@Global()
@Module({
imports: [
@ -49,7 +54,7 @@ const commandHandlers = [
]),
forwardRef(() => GuestsModule)
],
providers: [GameService, ConfigService, ...eventHandlers, ...commandHandlers, VersusService],
providers: [GameService, ConfigService, ...eventHandlers, ...commandHandlers,...queryHandlers, VersusService],
exports: [GameService],
controllers: [GameController, VersusController],
})

View file

@ -0,0 +1,3 @@
export class CheckIfAnotherVersusInProgressQuery {
}

View file

@ -0,0 +1,13 @@
import {IQueryHandler, QueryHandler} from "@nestjs/cqrs";
import {CheckIfAnotherVersusInProgressQuery} from "../check-if-another-versus-in-progress.query";
import {VersusService} from "../../versus/versus.service";
@QueryHandler(CheckIfAnotherVersusInProgressQuery)
export class CheckIfAnotherVersusInProgressHandler implements IQueryHandler<CheckIfAnotherVersusInProgressHandler> {
constructor(private versusService: VersusService) {
}
async execute(query: CheckIfAnotherVersusInProgressHandler): Promise<any> {
return await this.versusService.checkIfAnotherVersusInProgress();
}
}

View file

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import {Injectable, Logger} from '@nestjs/common';
import {SocketEvents} from "../../shared/events.consts";
import {GuestsService} from "../../guests/guests.service";
import {SharedService} from "../../shared/shared.service";
@ -14,6 +14,7 @@ import {IncreasePlayerWinningRateCommand} from "../commands/increase-player-winn
export class VersusService {
static configKeyCurrentAction = 'current_action';
static configKeyActiveVersus = 'active_versus';
private logger = new Logger(VersusService.name);
constructor(
private guestService: GuestsService,
private sharedService: SharedService,
@ -95,4 +96,11 @@ export class VersusService {
return item;
}
async checkIfAnotherVersusInProgress() {
this.logger.debug(`checkIfAnotherVersusInProgress enter`)
const currentAction = await this.sharedService.getConfig(VersusService.configKeyCurrentAction);
return currentAction.value !== '';
}
}

View file

@ -3,7 +3,7 @@ import {Promise} from "mongoose";
import {GuestsService} from "../guests.service";
export class SendBetweenRoundsActionsCommand {
constructor(public user: number, public inline: boolean = false) {
constructor(public user: number, public inline: boolean = true) {
}
}

View file

@ -1,6 +1,5 @@
import {IQueryHandler, QueryHandler} from "@nestjs/cqrs";
import {GetGuestQuery} from "../getguest.query";
import {Promise} from "mongoose";
import {GuestsService} from "../../guests.service";
@QueryHandler(GetGuestQuery)