Skip to content

Commit

Permalink
Merge pull request #8 from homanp/feat/manually-handle-calculations
Browse files Browse the repository at this point in the history
Feat/manually handle calculations
  • Loading branch information
homanp authored Nov 17, 2024
2 parents 170e625 + 41628dd commit de49076
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 89 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
Expand Down Expand Up @@ -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[];
}
Expand All @@ -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;
}
```

Expand All @@ -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`
Expand Down
9 changes: 6 additions & 3 deletions src/__tests__/OpenHandHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
});
Expand Down
89 changes: 13 additions & 76 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}

Expand Down

0 comments on commit de49076

Please sign in to comment.