This commit is contained in:
Kirill Ivlev 2024-11-12 21:40:40 +04:00
parent 9177d1fc16
commit 456cdcb4aa
8 changed files with 39 additions and 27 deletions

View file

@ -10,4 +10,5 @@ export class CommandsConsts {
static ApplyDebuff = 'ApplyDebuff'; static ApplyDebuff = 'ApplyDebuff';
static CompleteQueue = 'CompleteQueue'; static CompleteQueue = 'CompleteQueue';
static GetQuestion = 'GetQuestion'; static GetQuestion = 'GetQuestion';
static QuestionAnswer = "QuestionAnswer";
} }

View file

@ -197,7 +197,6 @@ export class BanPlayer extends GameCard {
eventBus.publish(new CardsSetChangedEvent(sourceUser.telegramId)); eventBus.publish(new CardsSetChangedEvent(sourceUser.telegramId));
}) })
eventBus.pipe(ofType(NextQuestionEvent)).subscribe(async (r)=> { eventBus.pipe(ofType(NextQuestionEvent)).subscribe(async (r)=> {
this.logger.verbose(`next event`);
const players = await queryBus.execute(new FilterGuestsWithPropertyQuery(DebuffsConsts.bannedFor, '$gt', 0)) const players = await queryBus.execute(new FilterGuestsWithPropertyQuery(DebuffsConsts.bannedFor, '$gt', 0))
this.logger.verbose(`enter: ban card handler, banned players count ${players.length}`); this.logger.verbose(`enter: ban card handler, banned players count ${players.length}`);
players.map(async (player) => { players.map(async (player) => {

View file

@ -28,13 +28,16 @@ export class TgPostCardsToUserCommandHandler implements ICommandHandler<PostCard
}, },
} }
if (command.cards.length === 0) { if (command.cards.length === 0) {
this.telegramService.emit({ cmd: CommandsConsts.SendMessage }, { chatId: command.chatId, message: "У вас нет карт которые можно сейчас использовать"});
return; return;
} }
command.cards.forEach((card) => { command.cards.forEach((card) => {
extra.reply_markup.keyboard.push([ extra.reply_markup.keyboard.push([
{text: Messages.EMOJI_CARD + ' ' + card}, {text: Messages.EMOJI_CARD + ' ' + card},
]); ]);
extra_Inline.reply_markup.inline_keyboard.push([{ text: Messages.EMOJI_CARD + ' ' + card}]) extra_Inline.reply_markup.inline_keyboard.push([{ text: Messages.EMOJI_CARD + ' ' + card, callback_data: `card/${card}`}])
}); });
await this.sharedService.setConfig(`buttons_${command.chatId}`, await this.sharedService.setConfig(`buttons_${command.chatId}`,
JSON.stringify(extra), JSON.stringify(extra),

View file

@ -96,12 +96,18 @@ export class GuestsService {
const extra = { const extra = {
reply_markup: { reply_markup: {
keyboard: [], keyboard: [],
inline_keyboard: [],
}, },
}; };
questionDto.answers.forEach((item, index) => { questionDto.answers.forEach((item, index) => {
extra.reply_markup.keyboard.push([ // extra.reply_markup.keyboard.push([
{ text: this.nums[index] + ' ' + item }, // { text: this.nums[index] + ' ' + item },
]); // ]);
if(item !== null){
extra.reply_markup.inline_keyboard.push(
[ { text: item, callback_data: `${item.substring(0,50)}` },
]);
}
}); });
if (!targetId) { if (!targetId) {
guests.forEach((guest) => { guests.forEach((guest) => {

View file

@ -64,4 +64,5 @@ export class GuestsMessageController {
async getQuestion(@Payload() data: { user: number, inline: false}) { async getQuestion(@Payload() data: { user: number, inline: false}) {
await this.quizService.displayQuestionForUser(data.user); await this.quizService.displayQuestionForUser(data.user);
} }
} }

View file

@ -2,4 +2,10 @@ export interface ValidateAnswerModel {
answer: string; answer: string;
user: number; user: number;
name: string; name: string;
}
export interface ValidateAnswerInline {
answer: string;
user: number;
name: string;
} }

View file

@ -1,7 +1,7 @@
import {Controller, Logger} from "@nestjs/common"; import {Controller, Logger} from "@nestjs/common";
import {MessagePattern, Payload} from "@nestjs/microservices"; import {MessagePattern, Payload} from "@nestjs/microservices";
import {CommandsConsts} from "../Consts/commands.consts"; import {CommandsConsts} from "../Consts/commands.consts";
import {ValidateAnswerModel} from "./models/validate-answer.model"; import {ValidateAnswerInline, ValidateAnswerModel} from "./models/validate-answer.model";
import {QuizAnsweredEvent} from "../game/events/quiz.answered"; import {QuizAnsweredEvent} from "../game/events/quiz.answered";
import {QuizService} from "../quiz/quiz.service"; import {QuizService} from "../quiz/quiz.service";
import {CommandBus, EventBus} from "@nestjs/cqrs"; import {CommandBus, EventBus} from "@nestjs/cqrs";
@ -15,11 +15,12 @@ export class QuizMessagingController {
constructor(private quizService: QuizService, private eventBus: EventBus, private cmdBus: CommandBus, private gameService: GameService) { constructor(private quizService: QuizService, private eventBus: EventBus, private cmdBus: CommandBus, private gameService: GameService) {
} }
@MessagePattern({ cmd: CommandsConsts.ValidateAnswer})
async validateAnswer(@Payload() data: ValidateAnswerModel) { @MessagePattern({ cmd: CommandsConsts.QuestionAnswer})
async getQuestionAnswer(@Payload() data: ValidateAnswerInline) {
this.logger.verbose(`Validate answer ${data}`); this.logger.verbose(`Validate answer ${data}`);
this.eventBus.publish(new QuizAnsweredEvent(data.name)); this.eventBus.publish(new QuizAnsweredEvent(data.name));
const result = await this.quizService.validateAnswer( const result = await this.quizService.validateAnswerInline(
data.answer, data.answer,
data.user, data.user,
); );
@ -36,7 +37,7 @@ export class QuizMessagingController {
@MessagePattern({ cmd: CommandsConsts.GetCards}) @MessagePattern({ cmd: CommandsConsts.GetCards})
async getCardsForUser(@Payload() data: { user: number, inline: boolean}) { async getCardsForUser(@Payload() data: { user: number, inline: boolean}) {
this.logger.verbose(`getCardsForUser ${data}`); this.logger.verbose(`getCardsForUser ${data}`);
await this.cmdBus.execute(new SendBetweenRoundsActionsCommand(data.user, data.inline)) await this.cmdBus.execute(new SendBetweenRoundsActionsCommand(data.user, true))
} }
@MessagePattern({ cmd: CommandsConsts.ApplyDebuff}) @MessagePattern({ cmd: CommandsConsts.ApplyDebuff})

View file

@ -55,25 +55,20 @@ export class QuizService {
return item.save(); return item.save();
} }
async validateAnswerInline(answer:string, id: number) {
async validateAnswer(answer: string, id: number) { this.logger.verbose(`[validateAnswer] enter ${answer} ${id}`);
this.logger.verbose(`enter validate answer ${answer} ${id}`);
const question = await this.get(); const question = await this.get();
await question.save();
const regexp = new RegExp(
Object.keys(this.answerNumbers)
.map((x) => {
x = this.answerNumbers[x].replace('.', '.').replace(' ', ' ');
return x;
})
.join('|'),
'gi',
);
this.logger.verbose( this.logger.verbose(
`Validating answer for question: ${JSON.stringify(question.text)}`, `Validating answer for question: ${JSON.stringify(question.text)}`,
); );
const filtered = answer.replace(regexp, '').trim(); // check that answer exist
const isAnswerValid = question.valid === filtered; const shortAnswers = question.answers.map((answer) => answer.substring(0,50));
const shortValidAnswer = question.valid.substring(0,50);
if(shortAnswers.indexOf(answer) === -1) {
this.logger.warn(`[validateAnswer] this question is not on game now`);
return;
}
const isAnswerValid = shortValidAnswer === answer;
if(question.userAnswers.find(answer => answer.user === id)) { if(question.userAnswers.find(answer => answer.user === id)) {
this.logger.verbose("question->user answer is already containing record"); this.logger.verbose("question->user answer is already containing record");
return; return;
@ -85,11 +80,11 @@ export class QuizService {
}) })
await question.save(); await question.save();
this.logger.verbose("question saved with user details") this.logger.verbose("question saved with user details")
if (question.valid === filtered) { if (shortValidAnswer=== answer) {
question.answeredBy = id; question.answeredBy = id;
this.logger.verbose(`extra ${question.note}`); this.logger.verbose(`extra ${question.note}`);
this.eventBus.publish( this.eventBus.publish(
new ValidAnswerReceivedEvent(id, filtered, question.note), new ValidAnswerReceivedEvent(id, answer, question.note),
); );
await question.save(); await question.save();
await this.markQuestionStorageAsAnsweredCorrectly(question.text); await this.markQuestionStorageAsAnsweredCorrectly(question.text);