diff --git a/src/Consts/guest-property-names.consts.ts b/src/Consts/guest-property-names.consts.ts new file mode 100644 index 0000000..33d1b60 --- /dev/null +++ b/src/Consts/guest-property-names.consts.ts @@ -0,0 +1,4 @@ +export class GuestPropertyNamesConsts { + static VersusWonCount = 'versusWonCount'; + static VersusLoseCount = 'versusLoseCount'; +} \ No newline at end of file diff --git a/src/game/versus/versus.controller.ts b/src/game/versus/versus.controller.ts index 7d6cb7c..e5c3ada 100644 --- a/src/game/versus/versus.controller.ts +++ b/src/game/versus/versus.controller.ts @@ -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') diff --git a/src/game/versus/versus.service.spec.ts b/src/game/versus/versus.service.spec.ts index f5f06a8..b3725db 100644 --- a/src/game/versus/versus.service.spec.ts +++ b/src/game/versus/versus.service.spec.ts @@ -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(); diff --git a/src/game/versus/versus.service.ts b/src/game/versus/versus.service.ts index b554d5f..715e9ef 100644 --- a/src/game/versus/versus.service.ts +++ b/src/game/versus/versus.service.ts @@ -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, 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 } diff --git a/src/guests/command/get-guest-property.handler.ts b/src/guests/command/get-guest-property.handler.ts index 075d87f..19fdcac 100644 --- a/src/guests/command/get-guest-property.handler.ts +++ b/src/guests/command/get-guest-property.handler.ts @@ -15,9 +15,8 @@ export class GetGuestPropertyHandler implements ICommandHandler { 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}`); }