fix voice & params
This commit is contained in:
parent
5f1b44d3d5
commit
ed28fe6090
9 changed files with 45 additions and 28 deletions
|
|
@ -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}`));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ export class GameWrongAnswerReceivedEventHandler
|
|||
}
|
||||
|
||||
async handle(event: WrongAnswerReceivedEvent) {
|
||||
await this.gameService.addTaskToGameQueue(
|
||||
event.tId,
|
||||
GameQueueTypes.penalty,
|
||||
);
|
||||
//
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
export interface QuestionDto {
|
||||
id: string;
|
||||
text: string;
|
||||
answers: string[];
|
||||
valid: string;
|
||||
note: string | null;
|
||||
qId: string;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<QuestionDocument>,
|
||||
@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<QuestionDocument> {
|
||||
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<number> {
|
||||
async getRemainQuestionWithoutValidAnswer(): Promise<number> {
|
||||
const questions = await this.questionStorageModel.find({
|
||||
isAnsweredCorrectly: false,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue