From ed28fe6090a4117fe25cfadcc0a666652bddbce4 Mon Sep 17 00:00:00 2001 From: Kirill Ivlev Date: Wed, 30 Oct 2024 18:56:48 +0400 Subject: [PATCH] fix voice & params --- .../quiz-answered-event.handler.ts | 2 +- .../wrong-answer-received-event.handler.ts | 5 +-- src/guests/guests.service.ts | 3 +- src/quiz/dto/question.dto.ts | 2 ++ src/quiz/quiz.module.ts | 1 + src/quiz/quiz.service.ts | 35 ++++++++++++++----- src/schemas/config.schema.ts | 3 +- src/schemas/question.schema.ts | 13 ++++++- .../wrong-answer-received-event.handler.ts | 9 ----- 9 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/game/event-handlers/quiz-answered-event.handler.ts b/src/game/event-handlers/quiz-answered-event.handler.ts index b6337f8..3602780 100644 --- a/src/game/event-handlers/quiz-answered-event.handler.ts +++ b/src/game/event-handlers/quiz-answered-event.handler.ts @@ -10,6 +10,6 @@ export class QuizAnsweredEventHandler } async handle(event: QuizAnsweredEvent) { - await this.commandBus.execute(new HideKeyboardCommand(`На вопрос ответил: ${event.name}`)); + // await this.commandBus.execute(new HideKeyboardCommand(`На вопрос ответил: ${event.name}`)); } } diff --git a/src/game/event-handlers/wrong-answer-received-event.handler.ts b/src/game/event-handlers/wrong-answer-received-event.handler.ts index 1f2ae9c..c96d313 100644 --- a/src/game/event-handlers/wrong-answer-received-event.handler.ts +++ b/src/game/event-handlers/wrong-answer-received-event.handler.ts @@ -10,9 +10,6 @@ export class GameWrongAnswerReceivedEventHandler } async handle(event: WrongAnswerReceivedEvent) { - await this.gameService.addTaskToGameQueue( - event.tId, - GameQueueTypes.penalty, - ); + // } } diff --git a/src/guests/guests.service.ts b/src/guests/guests.service.ts index 4910caa..0e5f0cb 100644 --- a/src/guests/guests.service.ts +++ b/src/guests/guests.service.ts @@ -317,7 +317,6 @@ export class GuestsService { this.logger.error(`Can't find user ${tId} for incrementing invalid answers count`); return; } - guest.invalidAnswers = +guest.invalidAnswers + 1; guest.invalidAnswersInRow = +guest.invalidAnswersInRow + 1; this.logger.verbose(`Invalid answers: ${guest.invalidAnswers}, inRow: ${guest.invalidAnswersInRow}`); @@ -328,7 +327,7 @@ export class GuestsService { GenitiveCase: guest.get(StringHelper.getPropertyName(GuestPropertiesConsts.NameGenitiveCase)) }, [...screpaDictManyInvalidAnswersDict]) await this.commandBus.execute(new CreateNewQueueItemCommand(guest.telegramId, GameQueueTypes.screpaAnounce, text)); - await this.commandBus.execute(new CreateNewQueueItemCommand(guest.telegramId, GameQueueTypes.penalty)); + // await this.commandBus.execute(new CreateNewQueueItemCommand(guest.telegramId, GameQueueTypes.penalty)); this.logger.verbose(`Reset invalidAnswerInRow, since user received penalty`); guest.invalidAnswersInRow = 0; } diff --git a/src/quiz/dto/question.dto.ts b/src/quiz/dto/question.dto.ts index 2c1e413..017f163 100644 --- a/src/quiz/dto/question.dto.ts +++ b/src/quiz/dto/question.dto.ts @@ -1,8 +1,10 @@ export interface QuestionDto { + id: string; text: string; answers: string[]; valid: string; note: string | null; + qId: string; } diff --git a/src/quiz/quiz.module.ts b/src/quiz/quiz.module.ts index 7551cbd..3975216 100644 --- a/src/quiz/quiz.module.ts +++ b/src/quiz/quiz.module.ts @@ -11,6 +11,7 @@ import { GameNextQuestionCommandHandler } from './command-handlers/next-question import { MarkQuestionsAsUnansweredCommandHandler } from './command-handlers/mark-questions-as-unanswred-command.handler'; import { PenaltyModule } from '../penalty/penalty.module'; import {ConfigModule, ConfigService} from "@nestjs/config"; +import {Config, ConfigSchema} from "../schemas/config.schema"; const cmdHandlers = [ GameNextQuestionCommandHandler, diff --git a/src/quiz/quiz.service.ts b/src/quiz/quiz.service.ts index 76a89b2..da122a0 100644 --- a/src/quiz/quiz.service.ts +++ b/src/quiz/quiz.service.ts @@ -15,11 +15,13 @@ import {Messages} from "../messaging/tg.text"; import {CreateNewQueueItemCommand} from "../game/commands/create-new-queue-item.command"; import {GameQueueTypes} from "../schemas/game-queue.schema"; import {ConfigService} from "@nestjs/config"; +import {Config, ConfigDocument, ConfigSchema} from "../schemas/config.schema"; @Injectable({ scope: Scope.TRANSIENT }) export class QuizService { private readonly answerNumbers = Messages.answerNumbers; private readonly logger = new Logger(QuizService.name); + private AcceptAnswersFromAllMembers: boolean = true; // TODO: move this to configurable state constructor( @InjectModel(Question.name) private questionModel: Model, @InjectModel(QuestionStorage.name) @@ -27,15 +29,17 @@ export class QuizService { private guestService: GuestsService, private sharedService: SharedService, private eventBus: EventBus, - private configService: ConfigService, private commandBus: CommandBus, - ) {} + ) { + + } async get(): Promise { return this.questionModel.find().sort({ _id: -1 }).findOne(); } async setQuestion(questionDto: QuestionDto, target: number = null) { + await this.sharedService.setConfig('currentQuestion', questionDto.id) const item = new this.questionModel(questionDto); await item.save(); this.logger.verbose(`Question updated`); @@ -50,10 +54,7 @@ export class QuizService { async validateAnswer(answer: string, id: number) { this.logger.verbose(`enter validate answer ${answer} ${id}`); const question = await this.get(); - if (question.answered) { - this.logger.verbose(`Question already answered`); - return false; - } + question.answered = true; await question.save(); const regexp = new RegExp( @@ -69,6 +70,20 @@ export class QuizService { `Validating answer for question: ${JSON.stringify(question.text)}`, ); const filtered = answer.replace(regexp, '').trim(); + const isAnswerValid = question.valid === filtered; + if(question.userAnswers.find(answer => answer.user === id)) { + this.logger.verbose("question->useranswer is already containing record"); + return; + } + question.userAnswers.push({ + user: id, + valid: isAnswerValid, + time: new Date() + }) + console.log(question); + await question.save(); + console.log(question); + this.logger.verbose("question saved with user details") if (question.valid === filtered) { question.answered = true; question.answeredBy = id; @@ -116,7 +131,7 @@ export class QuizService { .findOne({ isAnswered: false }) .exec(); if (!question) { - const unanswered = await this.getRemainQuestionWithouValidAnswer(); + const unanswered = await this.getRemainQuestionWithoutValidAnswer(); const skipRand = getRandomInt(0, unanswered); question = await this.questionStorageModel .findOne({ isAnsweredCorrectly: false }) @@ -131,6 +146,8 @@ export class QuizService { const question = await this.getNextQuestion(); question.isAnswered = true; await this.setQuestion({ + qId: question.id, + id: question.id, text: question.text, answers: question.answers, valid: question.valid, @@ -143,7 +160,7 @@ export class QuizService { const question = await this.getNextQuestion(); this.logger.verbose(`playExtraQuestion: ${question.text}`); await this.setQuestion( - { text: question.text, answers: question.answers, valid: question.valid, note: question.note }, + { qId: question.id, id: question.id, text: question.text, answers: question.answers, valid: question.valid, note: question.note }, telegramId, ); question.isAnswered = true; @@ -166,7 +183,7 @@ export class QuizService { return questions.length; } - async getRemainQuestionWithouValidAnswer(): Promise { + async getRemainQuestionWithoutValidAnswer(): Promise { const questions = await this.questionStorageModel.find({ isAnsweredCorrectly: false, }); diff --git a/src/schemas/config.schema.ts b/src/schemas/config.schema.ts index 39a4049..a213192 100644 --- a/src/schemas/config.schema.ts +++ b/src/schemas/config.schema.ts @@ -1,8 +1,6 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; -export type ConfigDocument = Config & Document; - @Schema() export class Config { @Prop() @@ -12,3 +10,4 @@ export class Config { } export const ConfigSchema = SchemaFactory.createForClass(Config); +export type ConfigDocument = Config & Document; \ No newline at end of file diff --git a/src/schemas/question.schema.ts b/src/schemas/question.schema.ts index f3bc4b0..471956e 100644 --- a/src/schemas/question.schema.ts +++ b/src/schemas/question.schema.ts @@ -1,8 +1,16 @@ import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; import { Document } from 'mongoose'; + +export class QuestionAnswer { + user: number; + time: Date; + valid: boolean; +} + export type QuestionDocument = Question & Document; + @Schema() export class Question { @Prop() @@ -15,8 +23,11 @@ export class Question { answered: boolean; @Prop() answeredBy: number; - @Prop() note: string | null; + @Prop() + qId: string; + @Prop([ { user: { type: Number }, time: { type: Date }, valid: { type: Boolean}}]) + userAnswers: QuestionAnswer[]; } export const QuestionSchema = SchemaFactory.createForClass(Question); diff --git a/src/socket/socket-handlers/event-handlers/wrong-answer-received-event.handler.ts b/src/socket/socket-handlers/event-handlers/wrong-answer-received-event.handler.ts index f8dcc9b..034ce82 100644 --- a/src/socket/socket-handlers/event-handlers/wrong-answer-received-event.handler.ts +++ b/src/socket/socket-handlers/event-handlers/wrong-answer-received-event.handler.ts @@ -1,7 +1,6 @@ import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { WrongAnswerReceivedEvent } from '../../../game/events/wrong-answer-received.event'; import { SharedService } from '../../../shared/shared.service'; -import { SocketEvents } from '../../../shared/events.consts'; import { Logger } from '@nestjs/common'; @EventsHandler(WrongAnswerReceivedEvent) @@ -14,13 +13,5 @@ export class SocketWrongAnswerReceivedEventHandler constructor(private sharedService: SharedService) {} handle(event: WrongAnswerReceivedEvent): any { - - this.sharedService.sendSocketNotificationToAllClients( - SocketEvents.WRONG_ANSWER_RECEIVED, - { - telegramId: event.tId, - validAnswer: event.validAnswer, - }, - ); } }