From 97d6b13541c94392bdc13a60e0ae412093039f8b Mon Sep 17 00:00:00 2001 From: Kirill Ivlev Date: Thu, 21 Nov 2024 00:17:59 +0400 Subject: [PATCH] refact --- src/game/versus/versus.service.ts | 13 +++++++---- ...est-valid-answer-received-event.handler.ts | 4 ++-- src/guests/guests.service.ts | 4 +++- src/quiz/quiz.controller.ts | 5 +++++ src/quiz/quiz.service.ts | 22 ++++++++++++++----- src/schemas/question.schema.ts | 2 ++ 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/game/versus/versus.service.ts b/src/game/versus/versus.service.ts index cf8ede2..b4abfaf 100644 --- a/src/game/versus/versus.service.ts +++ b/src/game/versus/versus.service.ts @@ -94,24 +94,26 @@ export class VersusService { item.completed = true; await item.save(); const tasks = []; - tasks.push(this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 2))); - tasks.push(this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 30))); + tasks.push(this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 1))); + tasks.push(this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 20))); tasks.push(this.sharedService.setConfig(VersusService.configKeyCurrentAction, '')); let wonCount = await this.queryBus.execute(new GetGuestPropertyQuery(winner, GuestPropertyNamesConsts.VersusWonCount)); let loseCount = await this.queryBus.execute(new GetGuestPropertyQuery(loser, GuestPropertyNamesConsts.VersusLoseCount)); if(!wonCount) { - wonCount = 0; + wonCount = 1; } else { wonCount = +wonCount++; } if(!loseCount) { - loseCount = 0; + loseCount = 1; } else { loseCount = +loseCount++; } this.logger.verbose(`Set loseCount for ${loser} to ${loseCount}`); this.logger.verbose(`Set win count for ${winner} to ${wonCount}`); tasks.push(await this.cmdBus.execute(new SetGuestPropertyCommand(winner, GuestPropertyNamesConsts.VersusWonCount, wonCount.toString))); + tasks.push(await this.cmdBus.execute(new SetGuestPropertyCommand(loser, GuestPropertyNamesConsts.VersusWonCount, loseCount.toString))); + await Promise.all(tasks); this.sharedService.notifyAllClients(ClientNotificationType.EndVersus, { winner: winner @@ -123,6 +125,9 @@ export class VersusService { async checkIfAnotherVersusInProgress() { this.logger.debug(`checkIfAnotherVersusInProgress enter`) const currentAction = await this.sharedService.getConfig(VersusService.configKeyCurrentAction); + if(!currentAction) { + return false; + } return currentAction.value !== ''; } diff --git a/src/guests/event-handlers/guest-valid-answer-received-event.handler.ts b/src/guests/event-handlers/guest-valid-answer-received-event.handler.ts index 802ce1e..0c864fa 100644 --- a/src/guests/event-handlers/guest-valid-answer-received-event.handler.ts +++ b/src/guests/event-handlers/guest-valid-answer-received-event.handler.ts @@ -21,14 +21,14 @@ export class GuestValidAnswerReceivedEventHandler async handle(event: ValidAnswerReceivedEvent) { await this.guestService.notifyAboutValidAnswer(event.tId); await this.guestService.sendValidAnswerActions(event.tId); - await this.guestService.updatePlayerScore(event.tId, 1); + // await this.guestService.updatePlayerScore(event.tId, 1); if (event.extraDetails) { await this.commandBus.execute( new SendToastCommand(event.extraDetails, 4000), ); } const coef = +(await this.guestService.getCoefficient()); - await this.guestService.changeWinningChance(event.tId, 29 * coef); + // await this.guestService.changeWinningChance(event.tId, 29 * coef); await this.guestService.resetInvalidAnswersInTheRow(event.tId); } } diff --git a/src/guests/guests.service.ts b/src/guests/guests.service.ts index ed5f18b..97fa427 100644 --- a/src/guests/guests.service.ts +++ b/src/guests/guests.service.ts @@ -75,7 +75,9 @@ export class GuestsService { } async findById(id: number) { - return this.guestModel.findOne({ telegramId: id }).exec(); + const result = await this.guestModel.findOne({ telegramId: id }).exec(); + delete result.photo; + return result; } async hideKeyboard(text: string) { diff --git a/src/quiz/quiz.controller.ts b/src/quiz/quiz.controller.ts index c5b1d53..10818be 100644 --- a/src/quiz/quiz.controller.ts +++ b/src/quiz/quiz.controller.ts @@ -56,4 +56,9 @@ export class QuizController { { return await this.quizService.calculateEndgamePoints(); } + + @Get('endgame-results') + async endgameResults() { + return await this.quizService.getEndgameResults(); + } } diff --git a/src/quiz/quiz.service.ts b/src/quiz/quiz.service.ts index f9e6a8d..b744bba 100644 --- a/src/quiz/quiz.service.ts +++ b/src/quiz/quiz.service.ts @@ -122,10 +122,13 @@ export class QuizService { private async calculateScore() { const question = await this.get(); + if(question.scoreCalculated) { + return; + } if(!await this.featureFlagService.getFeatureFlag(FeatureFlagsConsts.DontMarkQuestionsAsCompleted)) { this.logger.verbose(`[proceedWithGame]: DontMarkQuestionsAsCompleted disabled, marking as complete`); question.answered = true; - await question.save(); + } this.logger.verbose(`[calculateScore] enter `); const playerAnswers = question.userAnswers.map((answer) => { @@ -141,11 +144,14 @@ export class QuizService { if(winner) { const totalWinningScore = 80; sortedAnswers.filter(x => x.valid).forEach((answer) => { + this.logger.debug(`Giving 1 point to all who answered right`); this.commandBus.execute(new IncreasePlayerWinningRateCommand(answer.user, totalWinningScore / sortedAnswers.filter((answer) => answer.valid).length)); this.commandBus.execute(new IncreasePlayerScoreCommand(answer.user,1)); }); await this.commandBus.execute(new IncreasePlayerWinningRateCommand(sortedAnswers[0].user, 15)); + this.logger.debug(`Giving 1 point to first`); + await this.commandBus.execute(new IncreasePlayerScoreCommand(winner.user,1)); targetUser = winner.user; } @@ -160,6 +166,8 @@ export class QuizService { await this.commandBus.execute(new CreateNewQueueItemCommand(lastInvalidAnswer.user, GameQueueTypes.penalty)); } await this.commandBus.execute(new CreateNewQueueItemCommand(targetUser, GameQueueTypes.showresults)); + question.scoreCalculated = true; + await question.save(); } public async calculateEndgamePoints(): Promise { @@ -167,21 +175,20 @@ export class QuizService { const maxRewardsPromise = this.guestService.getModel().find({}).sort({['rewardsReceived']: "desc"}).exec(); const maxPenaltiesPromise = this.guestService.getModel().find({}).sort({['penaltiesReceived']: 'desc'}).exec(); - //const { maxRewards, maxInvalidAnswers } = Promise.all([maxRewardsPromise, maxInvalidAnswersPromise]); const [maxRewards, maxInvalidAnswers, maxPenaltiesReceived] = await Promise.all([maxRewardsPromise, maxInvalidAnswersPromise, maxPenaltiesPromise]); const result = { maxInvalidAnswers: { - id: maxInvalidAnswers[0].id, + id: maxInvalidAnswers[0].telegramId, count: maxInvalidAnswers[0].invalidAnswers, name: maxInvalidAnswers[0].name, }, maxRewards: { - id: maxRewards[0].id, + id: maxRewards[0].telegramId, count: maxRewards[0].rewardsReceived, name: maxRewards[0].name, }, maxPenalties: { - id: maxPenaltiesReceived[0].id, + id: maxPenaltiesReceived[0].telegramId, count: maxPenaltiesReceived[0].penaltiesReceived, name: maxPenaltiesReceived[0].name, } @@ -308,4 +315,9 @@ export class QuizService { } await this.guestService.postQuestion(dto, telegramId); } + + async getEndgameResults() { + const res = await this.sharedService.getConfig('endgame-points'); + return JSON.parse(res.value); + } } diff --git a/src/schemas/question.schema.ts b/src/schemas/question.schema.ts index 471956e..2f23742 100644 --- a/src/schemas/question.schema.ts +++ b/src/schemas/question.schema.ts @@ -29,5 +29,7 @@ export class Question { qId: string; @Prop([ { user: { type: Number }, time: { type: Date }, valid: { type: Boolean}}]) userAnswers: QuestionAnswer[]; + @Prop({ default: false }) + scoreCalculated: boolean; } export const QuestionSchema = SchemaFactory.createForClass(Question);