From 6be0e3bca35b2ae0074f35bdc68fad7edaade109 Mon Sep 17 00:00:00 2001 From: Ismail Pelaseyed Date: Mon, 18 Nov 2024 10:00:07 +0100 Subject: [PATCH] feat: calculate net winnings --- package.json | 2 +- src/index.ts | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 49f7b69..1581334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "open-hand-tracker", - "version": "1.0.6", + "version": "1.0.7", "description": "A package for creating and managing poker hand histories", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 19c5fce..81ba57c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,24 +80,23 @@ export class OpenHandHistory { calculateWinningAmount(playerId: number, potAmount: number): number { let totalContribution = 0; - // Calculate total contribution + // Calculate the total contribution of the player for (const round of this.ohh.rounds) { for (const action of round.actions) { if ( action.player_id === playerId && - (action.action === "Bet" || - action.action === "Raise" || - action.action === "Call" || - action.action === "Post SB" || - action.action === "Post BB") + ["Bet", "Raise", "Call", "Post SB", "Post BB"].includes(action.action) ) { - totalContribution += action.amount || 0; + totalContribution += action.amount || 0; // Add the action amount if present } } } - // Return the difference between pot amount and contribution - return potAmount - totalContribution; + // Calculate and return the net winning amount + const netWinningAmount = potAmount - totalContribution; + + // Ensure the result is not negative (players can't win less than 0) + return Math.max(netWinningAmount, 0); } }