fix voice & params

This commit is contained in:
Kirill Ivlev 2024-10-30 18:56:48 +04:00
parent 5f1b44d3d5
commit ed28fe6090
9 changed files with 45 additions and 28 deletions

View file

@ -10,6 +10,6 @@ export class QuizAnsweredEventHandler
} }
async handle(event: QuizAnsweredEvent) { async handle(event: QuizAnsweredEvent) {
await this.commandBus.execute(new HideKeyboardCommand(`На вопрос ответил: ${event.name}`)); // await this.commandBus.execute(new HideKeyboardCommand(`На вопрос ответил: ${event.name}`));
} }
} }

View file

@ -10,9 +10,6 @@ export class GameWrongAnswerReceivedEventHandler
} }
async handle(event: WrongAnswerReceivedEvent) { async handle(event: WrongAnswerReceivedEvent) {
await this.gameService.addTaskToGameQueue( //
event.tId,
GameQueueTypes.penalty,
);
} }
} }

View file

@ -317,7 +317,6 @@ export class GuestsService {
this.logger.error(`Can't find user ${tId} for incrementing invalid answers count`); this.logger.error(`Can't find user ${tId} for incrementing invalid answers count`);
return; return;
} }
guest.invalidAnswers = +guest.invalidAnswers + 1; guest.invalidAnswers = +guest.invalidAnswers + 1;
guest.invalidAnswersInRow = +guest.invalidAnswersInRow + 1; guest.invalidAnswersInRow = +guest.invalidAnswersInRow + 1;
this.logger.verbose(`Invalid answers: ${guest.invalidAnswers}, inRow: ${guest.invalidAnswersInRow}`); this.logger.verbose(`Invalid answers: ${guest.invalidAnswers}, inRow: ${guest.invalidAnswersInRow}`);
@ -328,7 +327,7 @@ export class GuestsService {
GenitiveCase: guest.get(StringHelper.getPropertyName(GuestPropertiesConsts.NameGenitiveCase)) GenitiveCase: guest.get(StringHelper.getPropertyName(GuestPropertiesConsts.NameGenitiveCase))
}, [...screpaDictManyInvalidAnswersDict]) }, [...screpaDictManyInvalidAnswersDict])
await this.commandBus.execute(new CreateNewQueueItemCommand(guest.telegramId, GameQueueTypes.screpaAnounce, text)); 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`); this.logger.verbose(`Reset invalidAnswerInRow, since user received penalty`);
guest.invalidAnswersInRow = 0; guest.invalidAnswersInRow = 0;
} }

View file

@ -1,8 +1,10 @@
export interface QuestionDto { export interface QuestionDto {
id: string;
text: string; text: string;
answers: string[]; answers: string[];
valid: string; valid: string;
note: string | null; note: string | null;
qId: string;
} }

View file

@ -11,6 +11,7 @@ import { GameNextQuestionCommandHandler } from './command-handlers/next-question
import { MarkQuestionsAsUnansweredCommandHandler } from './command-handlers/mark-questions-as-unanswred-command.handler'; import { MarkQuestionsAsUnansweredCommandHandler } from './command-handlers/mark-questions-as-unanswred-command.handler';
import { PenaltyModule } from '../penalty/penalty.module'; import { PenaltyModule } from '../penalty/penalty.module';
import {ConfigModule, ConfigService} from "@nestjs/config"; import {ConfigModule, ConfigService} from "@nestjs/config";
import {Config, ConfigSchema} from "../schemas/config.schema";
const cmdHandlers = [ const cmdHandlers = [
GameNextQuestionCommandHandler, GameNextQuestionCommandHandler,

View file

@ -15,11 +15,13 @@ import {Messages} from "../messaging/tg.text";
import {CreateNewQueueItemCommand} from "../game/commands/create-new-queue-item.command"; import {CreateNewQueueItemCommand} from "../game/commands/create-new-queue-item.command";
import {GameQueueTypes} from "../schemas/game-queue.schema"; import {GameQueueTypes} from "../schemas/game-queue.schema";
import {ConfigService} from "@nestjs/config"; import {ConfigService} from "@nestjs/config";
import {Config, ConfigDocument, ConfigSchema} from "../schemas/config.schema";
@Injectable({ scope: Scope.TRANSIENT }) @Injectable({ scope: Scope.TRANSIENT })
export class QuizService { export class QuizService {
private readonly answerNumbers = Messages.answerNumbers; private readonly answerNumbers = Messages.answerNumbers;
private readonly logger = new Logger(QuizService.name); private readonly logger = new Logger(QuizService.name);
private AcceptAnswersFromAllMembers: boolean = true; // TODO: move this to configurable state
constructor( constructor(
@InjectModel(Question.name) private questionModel: Model<QuestionDocument>, @InjectModel(Question.name) private questionModel: Model<QuestionDocument>,
@InjectModel(QuestionStorage.name) @InjectModel(QuestionStorage.name)
@ -27,15 +29,17 @@ export class QuizService {
private guestService: GuestsService, private guestService: GuestsService,
private sharedService: SharedService, private sharedService: SharedService,
private eventBus: EventBus, private eventBus: EventBus,
private configService: ConfigService,
private commandBus: CommandBus, private commandBus: CommandBus,
) {} ) {
}
async get(): Promise<QuestionDocument> { async get(): Promise<QuestionDocument> {
return this.questionModel.find().sort({ _id: -1 }).findOne(); return this.questionModel.find().sort({ _id: -1 }).findOne();
} }
async setQuestion(questionDto: QuestionDto, target: number = null) { async setQuestion(questionDto: QuestionDto, target: number = null) {
await this.sharedService.setConfig('currentQuestion', questionDto.id)
const item = new this.questionModel(questionDto); const item = new this.questionModel(questionDto);
await item.save(); await item.save();
this.logger.verbose(`Question updated`); this.logger.verbose(`Question updated`);
@ -50,10 +54,7 @@ export class QuizService {
async validateAnswer(answer: string, id: number) { async validateAnswer(answer: string, id: number) {
this.logger.verbose(`enter validate answer ${answer} ${id}`); this.logger.verbose(`enter validate answer ${answer} ${id}`);
const question = await this.get(); const question = await this.get();
if (question.answered) {
this.logger.verbose(`Question already answered`);
return false;
}
question.answered = true; question.answered = true;
await question.save(); await question.save();
const regexp = new RegExp( const regexp = new RegExp(
@ -69,6 +70,20 @@ export class QuizService {
`Validating answer for question: ${JSON.stringify(question.text)}`, `Validating answer for question: ${JSON.stringify(question.text)}`,
); );
const filtered = answer.replace(regexp, '').trim(); 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) { if (question.valid === filtered) {
question.answered = true; question.answered = true;
question.answeredBy = id; question.answeredBy = id;
@ -116,7 +131,7 @@ export class QuizService {
.findOne({ isAnswered: false }) .findOne({ isAnswered: false })
.exec(); .exec();
if (!question) { if (!question) {
const unanswered = await this.getRemainQuestionWithouValidAnswer(); const unanswered = await this.getRemainQuestionWithoutValidAnswer();
const skipRand = getRandomInt(0, unanswered); const skipRand = getRandomInt(0, unanswered);
question = await this.questionStorageModel question = await this.questionStorageModel
.findOne({ isAnsweredCorrectly: false }) .findOne({ isAnsweredCorrectly: false })
@ -131,6 +146,8 @@ export class QuizService {
const question = await this.getNextQuestion(); const question = await this.getNextQuestion();
question.isAnswered = true; question.isAnswered = true;
await this.setQuestion({ await this.setQuestion({
qId: question.id,
id: question.id,
text: question.text, text: question.text,
answers: question.answers, answers: question.answers,
valid: question.valid, valid: question.valid,
@ -143,7 +160,7 @@ export class QuizService {
const question = await this.getNextQuestion(); const question = await this.getNextQuestion();
this.logger.verbose(`playExtraQuestion: ${question.text}`); this.logger.verbose(`playExtraQuestion: ${question.text}`);
await this.setQuestion( 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, telegramId,
); );
question.isAnswered = true; question.isAnswered = true;
@ -166,7 +183,7 @@ export class QuizService {
return questions.length; return questions.length;
} }
async getRemainQuestionWithouValidAnswer(): Promise<number> { async getRemainQuestionWithoutValidAnswer(): Promise<number> {
const questions = await this.questionStorageModel.find({ const questions = await this.questionStorageModel.find({
isAnsweredCorrectly: false, isAnsweredCorrectly: false,
}); });

View file

@ -1,8 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose'; import { Document } from 'mongoose';
export type ConfigDocument = Config & Document;
@Schema() @Schema()
export class Config { export class Config {
@Prop() @Prop()
@ -12,3 +10,4 @@ export class Config {
} }
export const ConfigSchema = SchemaFactory.createForClass(Config); export const ConfigSchema = SchemaFactory.createForClass(Config);
export type ConfigDocument = Config & Document;

View file

@ -1,8 +1,16 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document } from 'mongoose'; import { Document } from 'mongoose';
export class QuestionAnswer {
user: number;
time: Date;
valid: boolean;
}
export type QuestionDocument = Question & Document; export type QuestionDocument = Question & Document;
@Schema() @Schema()
export class Question { export class Question {
@Prop() @Prop()
@ -15,8 +23,11 @@ export class Question {
answered: boolean; answered: boolean;
@Prop() @Prop()
answeredBy: number; answeredBy: number;
@Prop() @Prop()
note: string | null; note: string | null;
@Prop()
qId: string;
@Prop([ { user: { type: Number }, time: { type: Date }, valid: { type: Boolean}}])
userAnswers: QuestionAnswer[];
} }
export const QuestionSchema = SchemaFactory.createForClass(Question); export const QuestionSchema = SchemaFactory.createForClass(Question);

View file

@ -1,7 +1,6 @@
import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { WrongAnswerReceivedEvent } from '../../../game/events/wrong-answer-received.event'; import { WrongAnswerReceivedEvent } from '../../../game/events/wrong-answer-received.event';
import { SharedService } from '../../../shared/shared.service'; import { SharedService } from '../../../shared/shared.service';
import { SocketEvents } from '../../../shared/events.consts';
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
@EventsHandler(WrongAnswerReceivedEvent) @EventsHandler(WrongAnswerReceivedEvent)
@ -14,13 +13,5 @@ export class SocketWrongAnswerReceivedEventHandler
constructor(private sharedService: SharedService) {} constructor(private sharedService: SharedService) {}
handle(event: WrongAnswerReceivedEvent): any { handle(event: WrongAnswerReceivedEvent): any {
this.sharedService.sendSocketNotificationToAllClients(
SocketEvents.WRONG_ANSWER_RECEIVED,
{
telegramId: event.tId,
validAnswer: event.validAnswer,
},
);
} }
} }