This commit is contained in:
Kirill Ivlev 2024-11-21 00:17:59 +04:00
parent d18d12a458
commit 97d6b13541
6 changed files with 38 additions and 12 deletions

View file

@ -94,24 +94,26 @@ export class VersusService {
item.completed = true; item.completed = true;
await item.save(); await item.save();
const tasks = []; const tasks = [];
tasks.push(this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 2))); tasks.push(this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 1)));
tasks.push(this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 30))); tasks.push(this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 20)));
tasks.push(this.sharedService.setConfig(VersusService.configKeyCurrentAction, '')); tasks.push(this.sharedService.setConfig(VersusService.configKeyCurrentAction, ''));
let wonCount = await this.queryBus.execute(new GetGuestPropertyQuery(winner, GuestPropertyNamesConsts.VersusWonCount)); let wonCount = await this.queryBus.execute(new GetGuestPropertyQuery(winner, GuestPropertyNamesConsts.VersusWonCount));
let loseCount = await this.queryBus.execute(new GetGuestPropertyQuery(loser, GuestPropertyNamesConsts.VersusLoseCount)); let loseCount = await this.queryBus.execute(new GetGuestPropertyQuery(loser, GuestPropertyNamesConsts.VersusLoseCount));
if(!wonCount) { if(!wonCount) {
wonCount = 0; wonCount = 1;
} else { } else {
wonCount = +wonCount++; wonCount = +wonCount++;
} }
if(!loseCount) { if(!loseCount) {
loseCount = 0; loseCount = 1;
} else { } else {
loseCount = +loseCount++; loseCount = +loseCount++;
} }
this.logger.verbose(`Set loseCount for ${loser} to ${loseCount}`); this.logger.verbose(`Set loseCount for ${loser} to ${loseCount}`);
this.logger.verbose(`Set win count for ${winner} to ${wonCount}`); 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(winner, GuestPropertyNamesConsts.VersusWonCount, wonCount.toString)));
tasks.push(await this.cmdBus.execute(new SetGuestPropertyCommand(loser, GuestPropertyNamesConsts.VersusWonCount, loseCount.toString)));
await Promise.all(tasks); await Promise.all(tasks);
this.sharedService.notifyAllClients<IVersusEndSocketEvent>(ClientNotificationType.EndVersus, { this.sharedService.notifyAllClients<IVersusEndSocketEvent>(ClientNotificationType.EndVersus, {
winner: winner winner: winner
@ -123,6 +125,9 @@ export class VersusService {
async checkIfAnotherVersusInProgress() { async checkIfAnotherVersusInProgress() {
this.logger.debug(`checkIfAnotherVersusInProgress enter`) this.logger.debug(`checkIfAnotherVersusInProgress enter`)
const currentAction = await this.sharedService.getConfig(VersusService.configKeyCurrentAction); const currentAction = await this.sharedService.getConfig(VersusService.configKeyCurrentAction);
if(!currentAction) {
return false;
}
return currentAction.value !== ''; return currentAction.value !== '';
} }

View file

@ -21,14 +21,14 @@ export class GuestValidAnswerReceivedEventHandler
async handle(event: ValidAnswerReceivedEvent) { async handle(event: ValidAnswerReceivedEvent) {
await this.guestService.notifyAboutValidAnswer(event.tId); await this.guestService.notifyAboutValidAnswer(event.tId);
await this.guestService.sendValidAnswerActions(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) { if (event.extraDetails) {
await this.commandBus.execute( await this.commandBus.execute(
new SendToastCommand(event.extraDetails, 4000), new SendToastCommand(event.extraDetails, 4000),
); );
} }
const coef = +(await this.guestService.getCoefficient()); 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); await this.guestService.resetInvalidAnswersInTheRow(event.tId);
} }
} }

View file

@ -75,7 +75,9 @@ export class GuestsService {
} }
async findById(id: number) { 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) { async hideKeyboard(text: string) {

View file

@ -56,4 +56,9 @@ export class QuizController {
{ {
return await this.quizService.calculateEndgamePoints(); return await this.quizService.calculateEndgamePoints();
} }
@Get('endgame-results')
async endgameResults() {
return await this.quizService.getEndgameResults();
}
} }

View file

@ -122,10 +122,13 @@ export class QuizService {
private async calculateScore() { private async calculateScore() {
const question = await this.get(); const question = await this.get();
if(question.scoreCalculated) {
return;
}
if(!await this.featureFlagService.getFeatureFlag(FeatureFlagsConsts.DontMarkQuestionsAsCompleted)) { if(!await this.featureFlagService.getFeatureFlag(FeatureFlagsConsts.DontMarkQuestionsAsCompleted)) {
this.logger.verbose(`[proceedWithGame]: DontMarkQuestionsAsCompleted disabled, marking as complete`); this.logger.verbose(`[proceedWithGame]: DontMarkQuestionsAsCompleted disabled, marking as complete`);
question.answered = true; question.answered = true;
await question.save();
} }
this.logger.verbose(`[calculateScore] enter `); this.logger.verbose(`[calculateScore] enter `);
const playerAnswers = question.userAnswers.map((answer) => { const playerAnswers = question.userAnswers.map((answer) => {
@ -141,11 +144,14 @@ export class QuizService {
if(winner) { if(winner) {
const totalWinningScore = 80; const totalWinningScore = 80;
sortedAnswers.filter(x => x.valid).forEach((answer) => { 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, this.commandBus.execute(new IncreasePlayerWinningRateCommand(answer.user,
totalWinningScore / sortedAnswers.filter((answer) => answer.valid).length)); totalWinningScore / sortedAnswers.filter((answer) => answer.valid).length));
this.commandBus.execute(new IncreasePlayerScoreCommand(answer.user,1)); this.commandBus.execute(new IncreasePlayerScoreCommand(answer.user,1));
}); });
await this.commandBus.execute(new IncreasePlayerWinningRateCommand(sortedAnswers[0].user, 15)); 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; 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(lastInvalidAnswer.user, GameQueueTypes.penalty));
} }
await this.commandBus.execute(new CreateNewQueueItemCommand(targetUser, GameQueueTypes.showresults)); await this.commandBus.execute(new CreateNewQueueItemCommand(targetUser, GameQueueTypes.showresults));
question.scoreCalculated = true;
await question.save();
} }
public async calculateEndgamePoints(): Promise<QuizEndGameResults> { public async calculateEndgamePoints(): Promise<QuizEndGameResults> {
@ -167,21 +175,20 @@ export class QuizService {
const maxRewardsPromise = this.guestService.getModel().find({}).sort({['rewardsReceived']: "desc"}).exec(); const maxRewardsPromise = this.guestService.getModel().find({}).sort({['rewardsReceived']: "desc"}).exec();
const maxPenaltiesPromise = this.guestService.getModel().find({}).sort({['penaltiesReceived']: '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 [maxRewards, maxInvalidAnswers, maxPenaltiesReceived] = await Promise.all([maxRewardsPromise, maxInvalidAnswersPromise, maxPenaltiesPromise]);
const result = { const result = {
maxInvalidAnswers: { maxInvalidAnswers: {
id: maxInvalidAnswers[0].id, id: maxInvalidAnswers[0].telegramId,
count: maxInvalidAnswers[0].invalidAnswers, count: maxInvalidAnswers[0].invalidAnswers,
name: maxInvalidAnswers[0].name, name: maxInvalidAnswers[0].name,
}, },
maxRewards: { maxRewards: {
id: maxRewards[0].id, id: maxRewards[0].telegramId,
count: maxRewards[0].rewardsReceived, count: maxRewards[0].rewardsReceived,
name: maxRewards[0].name, name: maxRewards[0].name,
}, },
maxPenalties: { maxPenalties: {
id: maxPenaltiesReceived[0].id, id: maxPenaltiesReceived[0].telegramId,
count: maxPenaltiesReceived[0].penaltiesReceived, count: maxPenaltiesReceived[0].penaltiesReceived,
name: maxPenaltiesReceived[0].name, name: maxPenaltiesReceived[0].name,
} }
@ -308,4 +315,9 @@ export class QuizService {
} }
await this.guestService.postQuestion(dto, telegramId); await this.guestService.postQuestion(dto, telegramId);
} }
async getEndgameResults() {
const res = await this.sharedService.getConfig('endgame-points');
return JSON.parse(res.value);
}
} }

View file

@ -29,5 +29,7 @@ export class Question {
qId: string; qId: string;
@Prop([ { user: { type: Number }, time: { type: Date }, valid: { type: Boolean}}]) @Prop([ { user: { type: Number }, time: { type: Date }, valid: { type: Boolean}}])
userAnswers: QuestionAnswer[]; userAnswers: QuestionAnswer[];
@Prop({ default: false })
scoreCalculated: boolean;
} }
export const QuestionSchema = SchemaFactory.createForClass(Question); export const QuestionSchema = SchemaFactory.createForClass(Question);