Skip to content

Commit

Permalink
Fix hand winner state not being emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
mhluska committed Dec 28, 2023
1 parent f20f46a commit 5d74f9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {};

for (const [key, value] of this.handWinner.entries()) {
if (typeof value !== 'undefined') {
handWinner[key] = handWinnerToString(value);
}
}
Expand All @@ -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,
};
}
Expand Down
15 changes: 14 additions & 1 deletion test/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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: [
{
Expand All @@ -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 () {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5d74f9e

Please sign in to comment.