add loser to stat versus

This commit is contained in:
Kirill Ivlev 2024-11-18 12:09:24 +04:00
parent 0b28aff726
commit 7030a19b03
5 changed files with 38 additions and 11 deletions

View file

@ -0,0 +1,4 @@
export class GuestPropertyNamesConsts {
static VersusWonCount = 'versusWonCount';
static VersusLoseCount = 'versusLoseCount';
}

View file

@ -26,8 +26,8 @@ export class VersusController {
}
@Post('complete')
async Completed(@Body() payload: { winner: number }) {
return await this.versusService.complete(payload.winner);
async Completed(@Body() payload: { winner: number, loser: number }) {
return await this.versusService.complete(payload.winner, payload.loser);
}
@Post('reset-all')

View file

@ -6,7 +6,8 @@ import {SharedService} from "../../shared/shared.service";
import {getModelToken} from "@nestjs/mongoose";
import {Versus} from "../../schemas/versus.schema";
import {Model} from "mongoose";
import {CommandBus} from "@nestjs/cqrs";
import {CommandBus, QueryBus} from "@nestjs/cqrs";
import {QueryBusMock} from "../../mocks/querybus.mock";
describe('VersusService', () => {
let service: VersusService;
@ -19,6 +20,7 @@ describe('VersusService', () => {
{ provide: SharedService, useValue: SharedService },
{ provide: getModelToken(Versus.name), useValue: Model },
{ provide: CommandBus, useValue: CommandBus },
{ provide: QueryBus, useValue: QueryBusMock },
],
}).compile();

View file

@ -4,11 +4,15 @@ import {GuestsService} from "../../guests/guests.service";
import {SharedService} from "../../shared/shared.service";
import {InjectModel} from "@nestjs/mongoose";
import {Versus, VersusDocument} from "../../schemas/versus.schema";
import {Model} from "mongoose";
import {Model, Query} from "mongoose";
import {VersusDto} from "./versus.types";
import {CommandBus} from "@nestjs/cqrs";
import {CommandBus, QueryBus} from "@nestjs/cqrs";
import {IncreasePlayerScoreCommand} from "../../guests/command/increase-player-score.command";
import {IncreasePlayerWinningRateCommand} from "../commands/increase-player-winning-rate.command";
import {GetGuestQuery} from "../../guests/queries/getguest.query";
import {GetGuestPropertyQuery} from "../../guests/command/get-guest-property.handler";
import {GuestPropertyNamesConsts} from "../../Consts/guest-property-names.consts";
import {SetGuestPropertyCommand} from "../../guests/command/set-guest-property.command";
@Injectable()
export class VersusService {
@ -18,6 +22,7 @@ export class VersusService {
constructor(
private guestService: GuestsService,
private sharedService: SharedService,
private queryBus: QueryBus,
@InjectModel(Versus.name) private versusModel: Model<VersusDocument>,
private cmdBus: CommandBus,
) {
@ -81,14 +86,31 @@ export class VersusService {
return { result: true };
}
async complete(winner: number) {
async complete(winner: number, loser: number) {
const activeVersus = await this.sharedService.getConfig(VersusService.configKeyActiveVersus);
const item = await this.versusModel.findOne({ _id: activeVersus.value }).exec();
item.completed = true;
await item.save();
await this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 2));
await this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 30));
await this.sharedService.setConfig(VersusService.configKeyCurrentAction, '');
const tasks = [];
tasks.push(this.cmdBus.execute(new IncreasePlayerScoreCommand(winner, 2)));
tasks.push(this.cmdBus.execute(new IncreasePlayerWinningRateCommand(winner, 30)));
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;
} else {
wonCount = +wonCount++;
}
if(!loseCount) {
loseCount = 0;
} 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)));
await Promise.all(tasks);
this.sharedService.sendSocketNotificationToAllClients(
SocketEvents.END_VERSUS,
{ winner: winner }

View file

@ -15,9 +15,8 @@ export class GetGuestPropertyHandler implements ICommandHandler<GetGuestProperty
async execute(command: GetGuestPropertyQuery): Promise<string> {
this.logger.verbose(`entering`);
const guest = await this.guestService.findById(command.user);
console.log(command);
this.logger.verbose(command);
if(!command.property.startsWith('properties.')) {
command.property = `properties.${command.property}`;
this.logger.warn(`update prop ${command.property}`);
}