From e9d51ceb6e6f9324302e876ab7a26ffbe821b973 Mon Sep 17 00:00:00 2001 From: Ismail Pelaseyed Date: Sun, 17 Nov 2024 17:05:05 +0100 Subject: [PATCH 1/2] feat: update and --- README.md | 20 +++--- package.json | 2 +- src/__tests__/OpenHandHistory.test.ts | 9 ++- src/index.ts | 89 ++++----------------------- 4 files changed, 30 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index 9fcbc6b..8d7befb 100644 --- a/README.md +++ b/README.md @@ -51,16 +51,16 @@ ohh.addActionToRound(1, { amount: 100 }); +// Calcuatle winnings +win_amount = ohh.calculateWinningAmount(1, 50); // Pass the player ID + // Add pot ohh.addPot({ number: 1, amount: 50, - player_wins: [{ player_id: 1, win_amount: 50 }], + player_wins: [{ player_id: 1, win_amount: win_amount }], }); -// Calcuatle winnings -ohh.calculateWinningAmount(1); // Pass the player ID - // Save to file ohh.saveToFile('hand_history.json'); ``` @@ -100,7 +100,7 @@ Add a player to the hand history. interface Player { name: string; id: number; - startingStack: number; + starting_stack: number; seat: number; cards?: string[]; } @@ -121,11 +121,11 @@ interface Round { Add an action to a specific round. ```typescript interface Action { - actionNumber: number; - playerId: number; + action_number: number; + player_idd: number; action: string; amount?: number; - isAllIn?: boolean; + is_allin?: boolean; } ``` @@ -136,11 +136,11 @@ interface Pot { rake?: number; number: number; amount: number; - playerWins: { playerId: number; winAmount: number }[]; + player_wins: { player_id: number; win_amount: number }[]; } ``` -##### `calculateWinningAmount(player_id: string): number` +##### `calculateWinningAmount(player_id: number, totalPot: number): number` Calculates the winners winnings ##### `toJSON(): string` diff --git a/package.json b/package.json index f22d846..49f7b69 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "open-hand-tracker", - "version": "1.0.5", + "version": "1.0.6", "description": "A package for creating and managing poker hand histories", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/__tests__/OpenHandHistory.test.ts b/src/__tests__/OpenHandHistory.test.ts index 92fb6e6..a7675df 100644 --- a/src/__tests__/OpenHandHistory.test.ts +++ b/src/__tests__/OpenHandHistory.test.ts @@ -105,6 +105,7 @@ describe("OpenHandHistory", () => { ohh.addPot(pot); const json = ohh.toJSON(); expect(json.pots).toHaveLength(1); + expect(json.pots[0]).toEqual(pot); }); test("handles complex hand history correctly", () => { @@ -134,8 +135,8 @@ describe("OpenHandHistory", () => { // Add pot ohh.addPot({ number: 1, - amount: 200, - player_wins: [{ player_id: 1, win_amount: 200 }], + amount: 50, + player_wins: [{ player_id: 1, win_amount: 0 }], }); const json = ohh.toJSON(); @@ -189,11 +190,13 @@ describe("OpenHandHistory", () => { player_id: 3, action: "Fold" as const, }); + const win_amount = ohh.calculateWinningAmount(1, 50); ohh.addPot({ number: 1, amount: 50, - player_wins: [{ player_id: 1, win_amount: 50 }], + player_wins: [{ player_id: 1, win_amount }], }); + const json = ohh.toJSON(); expect(json.pots[0].player_wins[0].win_amount).toBe(30); // 50 (pot) - 20 (raise) }); diff --git a/src/index.ts b/src/index.ts index ea9828c..82626fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,15 +66,6 @@ export class OpenHandHistory { addPot(pot: Pot): void { this.ohh.pots.push(pot); - - const lastPotIndex = this.ohh.pots.length - 1; - this.ohh.pots[lastPotIndex] = { - ...this.ohh.pots[lastPotIndex], - player_wins: this.ohh.pots[lastPotIndex].player_wins.map((win) => ({ - ...win, - win_amount: this.calculateWinningAmount(win.player_id), - })), - }; } toJSON(): OHHData { @@ -86,81 +77,27 @@ export class OpenHandHistory { writeFileSync(filename, stringRepresentation); } - calculateWinningAmount(playerId: number): number { - let totalWinnings = 0; - let playerPosition = this.getPlayerPosition(playerId); + calculateWinningAmount(playerId: number, potAmount: number): number { + let totalContribution = 0; + // Calculate total contribution for (const round of this.ohh.rounds) { - let roundWinnings = 0; for (const action of round.actions) { - if (action.player_id === playerId) { - if ( - action.action === "Bet" || + if ( + action.player_id === playerId && + (action.action === "Bet" || action.action === "Raise" || - action.action === "Call" - ) { - roundWinnings -= action.amount || 0; - } - } else { - if (action.action === "Fold") { - roundWinnings += this.calculatePotContribution( - action.player_id, - round - ); - } + action.action === "Call" || + action.action === "Post SB" || + action.action === "Post BB") + ) { + totalContribution += action.amount || 0; } } - totalWinnings += roundWinnings; - } - - // Add winnings from pots - totalWinnings += this.ohh.pots.reduce((total, pot) => { - const playerWin = pot.player_wins.find( - (win) => win.player_id === playerId - ); - return total + (playerWin ? playerWin.win_amount : 0); - }, 0); - - // Adjust for blinds - if (playerPosition === "SB") { - totalWinnings -= this.ohh.small_blind_amount; - } else if (playerPosition === "BB") { - totalWinnings -= this.ohh.big_blind_amount; } - return totalWinnings; - } - - private getPlayerPosition( - playerId: number - ): "Button" | "SB" | "BB" | "Other" { - const playerIndex = this.ohh.players.findIndex( - (player) => player.id === playerId - ); - const dealerIndex = this.ohh.players.findIndex( - (player) => player.seat === this.ohh.dealer_seat - ); - - if (playerIndex === dealerIndex) return "Button"; - if (playerIndex === (dealerIndex + 1) % this.ohh.players.length) - return "SB"; - if (playerIndex === (dealerIndex + 2) % this.ohh.players.length) - return "BB"; - return "Other"; - } - - private calculatePotContribution(playerId: number, round: Round): number { - return round.actions.reduce((total, action) => { - if ( - action.player_id === playerId && - (action.action === "Bet" || - action.action === "Raise" || - action.action === "Call") - ) { - return total + (action.amount || 0); - } - return total; - }, 0); + // Return the difference between pot amount and contribution + return potAmount - totalContribution; } } From 41628ddf96d6dbdf4d8a3cc2ff987b6f45d792a6 Mon Sep 17 00:00:00 2001 From: Ismail Pelaseyed Date: Sun, 17 Nov 2024 17:06:11 +0100 Subject: [PATCH 2/2] minor tweaks --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 49f7b69..f22d846 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "open-hand-tracker", - "version": "1.0.6", + "version": "1.0.5", "description": "A package for creating and managing poker hand histories", "main": "dist/index.js", "types": "dist/index.d.ts",