From 5d74f9ed24845f8c22eae42e4b34f054899f7316 Mon Sep 17 00:00:00 2001 From: Maros Hluska Date: Thu, 28 Dec 2023 04:02:24 -0800 Subject: [PATCH] Fix hand winner state not being emitted --- src/player.ts | 10 ++++------ test/src/game.ts | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/player.ts b/src/player.ts index 796a4cb..e0ef268 100644 --- a/src/player.ts +++ b/src/player.ts @@ -156,11 +156,10 @@ export default class Player extends GameObject { } attributes(): PlayerAttributes { - // TODO: Get `Object.fromEntries` working when running `npm run test`. - const handWinner: { [key: string]: string } = {}; - for (const key of this.handWinner.keys()) { - const value = this.handWinner.get(key); - if (value) { + const handWinner: Record = {}; + + for (const [key, value] of this.handWinner.entries()) { + if (typeof value !== 'undefined') { handWinner[key] = handWinnerToString(value); } } @@ -169,7 +168,6 @@ export default class Player extends GameObject { id: this.id, balance: this.balance, hands: this.hands.map((hand) => hand.attributes()), - // handWinner: Object.fromEntries(this.handWinner), handWinner, }; } diff --git a/test/src/game.ts b/test/src/game.ts index 8214502..63f2390 100644 --- a/test/src/game.ts +++ b/test/src/game.ts @@ -165,6 +165,7 @@ describe('Game', function () { context('when the player bets and wins', function () { let playerBalanceBefore: number; + let emittedHandWinner = false; const game = setupGame({ // Force a winning hand for the player (blackjack with A-J). @@ -175,6 +176,15 @@ describe('Game', function () { before(function () { playerBalanceBefore = game.player.balance; + game.on(Event.Change, (name, value) => { + if ( + name === 'player' && + value.handWinner[value.hands[0].id] === 'player' + ) { + emittedHandWinner = true; + } + }); + runGame(game, { input: [ { @@ -189,6 +199,10 @@ describe('Game', function () { playerBalanceBefore + game.betAmount * (3 / 2) ); }); + + it('should emit the correct handWinner state', () => { + expect(emittedHandWinner).to.be.true; + }); }); context('when autoDeclineInsurance is enabled', function () { @@ -426,7 +440,6 @@ describe('Game', function () { }); game.on(Event.Change, (name, value) => { - console.log('EMIT', name, value); if (name === 'dealer' && value.hands[0].blackjack) { emittedBlackjack = true; }