From 456cdcb4aaad2ba7d8dc553b20b6f634f3504c55 Mon Sep 17 00:00:00 2001 From: Kirill Ivlev Date: Tue, 12 Nov 2024 21:40:40 +0400 Subject: [PATCH] TGD-35 --- src/Consts/commands.consts.ts | 1 + src/game/entities/cards.entities.ts | 1 - .../guests.post-cards-to-user-command.ts | 5 +++- src/guests/guests.service.ts | 12 ++++++-- src/messaging/guests.controller.ts | 1 + src/messaging/models/validate-answer.model.ts | 6 ++++ src/messaging/quiz-messaging.controller.ts | 11 +++---- src/quiz/quiz.service.ts | 29 ++++++++----------- 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Consts/commands.consts.ts b/src/Consts/commands.consts.ts index fc65d8e..4486485 100644 --- a/src/Consts/commands.consts.ts +++ b/src/Consts/commands.consts.ts @@ -10,4 +10,5 @@ export class CommandsConsts { static ApplyDebuff = 'ApplyDebuff'; static CompleteQueue = 'CompleteQueue'; static GetQuestion = 'GetQuestion'; + static QuestionAnswer = "QuestionAnswer"; } diff --git a/src/game/entities/cards.entities.ts b/src/game/entities/cards.entities.ts index 6361606..a6751d2 100644 --- a/src/game/entities/cards.entities.ts +++ b/src/game/entities/cards.entities.ts @@ -197,7 +197,6 @@ export class BanPlayer extends GameCard { eventBus.publish(new CardsSetChangedEvent(sourceUser.telegramId)); }) eventBus.pipe(ofType(NextQuestionEvent)).subscribe(async (r)=> { - this.logger.verbose(`next event`); const players = await queryBus.execute(new FilterGuestsWithPropertyQuery(DebuffsConsts.bannedFor, '$gt', 0)) this.logger.verbose(`enter: ban card handler, banned players count ${players.length}`); players.map(async (player) => { diff --git a/src/guests/command/guests.post-cards-to-user-command.ts b/src/guests/command/guests.post-cards-to-user-command.ts index ac0ac03..3eafee4 100644 --- a/src/guests/command/guests.post-cards-to-user-command.ts +++ b/src/guests/command/guests.post-cards-to-user-command.ts @@ -28,13 +28,16 @@ export class TgPostCardsToUserCommandHandler implements ICommandHandler { extra.reply_markup.keyboard.push([ {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}`, JSON.stringify(extra), diff --git a/src/guests/guests.service.ts b/src/guests/guests.service.ts index cf3078e..ed5f18b 100644 --- a/src/guests/guests.service.ts +++ b/src/guests/guests.service.ts @@ -96,12 +96,18 @@ export class GuestsService { const extra = { reply_markup: { keyboard: [], + inline_keyboard: [], }, }; questionDto.answers.forEach((item, index) => { - extra.reply_markup.keyboard.push([ - { text: this.nums[index] + ' ' + item }, - ]); + // extra.reply_markup.keyboard.push([ + // { text: this.nums[index] + ' ' + item }, + // ]); + if(item !== null){ + extra.reply_markup.inline_keyboard.push( + [ { text: item, callback_data: `${item.substring(0,50)}` }, + ]); + } }); if (!targetId) { guests.forEach((guest) => { diff --git a/src/messaging/guests.controller.ts b/src/messaging/guests.controller.ts index 049160a..38cb32f 100644 --- a/src/messaging/guests.controller.ts +++ b/src/messaging/guests.controller.ts @@ -64,4 +64,5 @@ export class GuestsMessageController { async getQuestion(@Payload() data: { user: number, inline: false}) { await this.quizService.displayQuestionForUser(data.user); } + } \ No newline at end of file diff --git a/src/messaging/models/validate-answer.model.ts b/src/messaging/models/validate-answer.model.ts index e3c9249..229dc5c 100644 --- a/src/messaging/models/validate-answer.model.ts +++ b/src/messaging/models/validate-answer.model.ts @@ -2,4 +2,10 @@ export interface ValidateAnswerModel { answer: string; user: number; name: string; +} + +export interface ValidateAnswerInline { + answer: string; + user: number; + name: string; } \ No newline at end of file diff --git a/src/messaging/quiz-messaging.controller.ts b/src/messaging/quiz-messaging.controller.ts index 964adc2..5417a4f 100644 --- a/src/messaging/quiz-messaging.controller.ts +++ b/src/messaging/quiz-messaging.controller.ts @@ -1,7 +1,7 @@ import {Controller, Logger} from "@nestjs/common"; import {MessagePattern, Payload} from "@nestjs/microservices"; 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 {QuizService} from "../quiz/quiz.service"; 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) { } - @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.eventBus.publish(new QuizAnsweredEvent(data.name)); - const result = await this.quizService.validateAnswer( + const result = await this.quizService.validateAnswerInline( data.answer, data.user, ); @@ -36,7 +37,7 @@ export class QuizMessagingController { @MessagePattern({ cmd: CommandsConsts.GetCards}) async getCardsForUser(@Payload() data: { user: number, inline: boolean}) { 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}) diff --git a/src/quiz/quiz.service.ts b/src/quiz/quiz.service.ts index 269cc4b..2c29058 100644 --- a/src/quiz/quiz.service.ts +++ b/src/quiz/quiz.service.ts @@ -55,25 +55,20 @@ export class QuizService { return item.save(); } - - async validateAnswer(answer: string, id: number) { - this.logger.verbose(`enter validate answer ${answer} ${id}`); + async validateAnswerInline(answer:string, id: number) { + this.logger.verbose(`[validateAnswer] enter ${answer} ${id}`); 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( `Validating answer for question: ${JSON.stringify(question.text)}`, ); - const filtered = answer.replace(regexp, '').trim(); - const isAnswerValid = question.valid === filtered; + // check that answer exist + 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)) { this.logger.verbose("question->user answer is already containing record"); return; @@ -85,11 +80,11 @@ export class QuizService { }) await question.save(); this.logger.verbose("question saved with user details") - if (question.valid === filtered) { + if (shortValidAnswer=== answer) { question.answeredBy = id; this.logger.verbose(`extra ${question.note}`); this.eventBus.publish( - new ValidAnswerReceivedEvent(id, filtered, question.note), + new ValidAnswerReceivedEvent(id, answer, question.note), ); await question.save(); await this.markQuestionStorageAsAnsweredCorrectly(question.text);