chip-poker-ts
v0.0.1
Published
A fork of poker-ts with modifications for the CHIP library.
Maintainers
Readme
𐂐 Fork Notice
This repository is a maintained fork of the original poker-ts. It extends the public API to support additional control and evaluation flows while remaining compatible with the original usage. Notable differences:
- Added:
initialHandPlayers() - Added:
isAtStartOfBettingRound() - Added:
isInMiddleOfBettingRound() - Added:
setCommunityCards(cards) - Added:
setPlayerHoleCards(seatIndex, cards) - Added:
manualShowdown(communityCards, playerHoleCards) - Modified:
winners()now includes a numeric payout as the fourth tuple element per winner
Extended API (Added/Modified)
Quick reference for users coming from the original project. See the API section below for full details.
initialHandPlayers()
Poker.Table.prototype.initialHandPlayers(): ({ totalChips: number, stack: number, betSize: number } | null)[]
Returns the state of the players that were in the hand at the time the hand started.
isAtStartOfBettingRound()
Poker.Table.prototype.isAtStartOfBettingRound(): boolean
Returns true if the current betting round has started and no actions have been taken yet.
isInMiddleOfBettingRound()
Poker.Table.prototype.isInMiddleOfBettingRound(): boolean
Returns true if at least one action has been taken and the betting round is still in progress.
manualShowdown(communityCards, playerHoleCards)
Poker.Table.prototype.manualShowdown(communityCards: Card[], playerHoleCards: { [seatIndex: number]: Card[] }): void
Performs a showdown using explicitly provided community cards and a mapping of players' hole cards. This bypasses dealing and evaluates winners directly.
winners() (modified)
Poker.Table.prototype.winners(): [SeatIndex, { cards: Card[], ranking: HandRanking, strength: number }, Card[], number][][]
Returns the winner(s) per pot after showdown, now including the numeric payout as the fourth element for each winner tuple.
chip-poker-ts (TypeScript)
chip-poker-ts is a poker game engine fork of poker-ts that can be used to serve Texas hold'em games for real players.
Acknowledgment
This library is a TypeScript port of the C++ Poker library written by Janko Dedic. Note that minor differences in the API might exist.
Example Usage
Poker-ts exports a Poker.Table class that represents a state machine and models a real-world poker table.
Short example below:
const Poker = require('chip-poker-ts');
table = new Poker.Table({ smallBlind: 50, bigBlind: 100 })
table.sitDown(0, 1000); // seat a player at seat 0 with 1000 chips buy-in
table.sitDown(2, 1500); // seat a player at seat 2 with 1500 chips buy-in
table.sitDown(5, 1700); // seat a player at seat 5 with 1700 chips buy-in
table.startHand();
while (table.isHandInProgress()) {
while (table.isBettingRoundInProgress()) {
const seatIndex = table.playerToAct();
// Get `action` and possibly `betSize` in some way
const [action, betSize] = getPlayerActionSomehow(seatIndex);
table.actionTaken(action, betSize);
}
table.endBettingRound()
if (table.areBettingRoundsCompleted()) {
table.showdown()
}
}
// 🎉 🎉 🎉 Congrats to the `table.winners()` 🎉 🎉 🎉 Become a Sponsor
Consider making a small donation via Github if you find my work with this library valuable.
API
Custom types and enums used in documentation below
type Card = {
rank: '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'T' | 'J' | 'Q' | 'K' | 'A'
suit: 'clubs' | 'diamonds' | 'hearts' | 'spades'
}
type AutomaticAction = 'fold' | 'check/fold' | 'check' | 'call' | 'call any' | 'all-in'
type Action = 'fold' | 'check' | 'call' | 'bet' | 'raise'
type ChipRange = {
min: number
max: number
}
type SeatIndex = number
enum HandRanking {
HIGH_CARD,
PAIR,
TWO_PAIR,
THREE_OF_A_KIND,
STRAIGHT,
FLUSH,
FULL_HOUSE,
FOUR_OF_A_KIND,
STRAIGHT_FLUSH,
ROYAL_FLUSH,
}Constructor
Poker.Table(forcedBets: { ante?: number, bigBlind: number, smallBlind: number }, numSeats?: number)
Creates an instance of the poker table object. The ante (a forced bet in which all players put an equal amount of money or chips into the pot before the deal begins) and specifying the dealer button seat are optional.
playerToAct()
Poker.Table.prototype.playerToAct(): number
Returns the seat index of the player to act. (Betting round must be in progress.)
button()
Poker.Table.prototype.button(): number
Returns the seat index of the dealer button. (Hand must be in progress.)
seats()
Poker.Table.prototype.seats(): ({ totalChips: number, stack: number, betSize: number } | null)[]
Returns the state of the players seated at the table.
handPlayers()
Poker.Table.prototype.handPlayers(): ({ totalChips: number, stack: number, betSize: number } | null)[]
Returns the state of the players currently in the hand. (Hand must be in progress.)
initialHandPlayers()
Poker.Table.prototype.initialHandPlayers(): ({ totalChips: number, stack: number, betSize: number } | null)[]
Returns the state of the players that were in the hand at the time the hand started.
numActivePlayers()
Poker.Table.prototype.numActivePlayers(): number
Returns the number of active players in the active hand. (Hand must be in progress.)
pots()
Poker.Table.prototype.pots(): { size: number, eligiblePlayers: number[] }[]
Returns the state of all pots in the active hand.
This endpoint can be used prior to calling showdown() to check if all but one last eligible player has folded. A single eligible player will win the pot, even though the winnders() endpoints returns an empty array under that circumstance.
forcedBets()
Poker.Table.prototype.forcedBets(): { ante: number, bigBlind: number, smallBlind: number }
Returns the current bet structure at the table.
setForcedBets(forcedBets)
Poker.Table.prototype.setForcedBets(forcedBets: { ante?: number, bigBlind: number, smallBlind: number }): void
Modifies the bet structure of the table. (Hand must not be in progress.)
numSeats()
Poker.Table.prototype.numSeats(): number
Returns the number of seats at the table.
startHand(seat?: number)
Poker.Table.prototype.startHand(seat?: number)): void
Start a new hand by collecting ante, placing blinds and dealing cards. (Hand must not be in progress and there must be at least two players seated at the table.)
Optionally set dealer button seat. If seat is invalid, button will be placed at the first hand player.
isHandInProgress()
Poker.Table.prototype.isHandInProgress(): boolean
Returns true if hand is in progress.
isBettingRoundInProgress()
Poker.Table.prototype.isBettingRoundInProgress(): boolean
Returns true if betting round is in progress. (Hand must be in progress.)
isAtStartOfBettingRound()
Poker.Table.prototype.isAtStartOfBettingRound(): boolean
Returns true if the current betting round has started and no actions have been taken yet.
isInMiddleOfBettingRound()
Poker.Table.prototype.isInMiddleOfBettingRound(): boolean
Returns true if at least one action has been taken and the betting round is still in progress.
areBettingRoundsCompleted()
Poker.Table.prototype.areBettingRoundsCompleted(): boolean
Returns true if all betting rounds are completed. (Hand must be in progress.)
roundOfBetting()
Poker.Table.prototype.roundOfBetting(): 'preflop' | 'flop' | 'turn' | 'river'
Returns the current round of betting. (Hand must be in progress)
communityCards()
Poker.Table.prototype.communityCards(): Card[]
Returns the community cards for the active hand. (Hand must be in progress.)
legalActions()
Poker.Table.prototype.legalActions(): { actions: Action[], chipRange?: ChipRange }
Returns legal actions for the player to act. (Betting round must be in progress.)
holeCards()
Poker.Table.prototype.holeCards(): (Card[] | null)[]
Returns the hole cards for the active hand. (Hand must be in progress or showdown must have ended.)
actionTaken(action, betSize)
Poker.Table.prototype.actionTaken(action: Action, betSize?: number)
Indicate that the player to act has taken an action. (Betting round must be in progress)
endBettingRound()
Poker.Table.prototype.endBettingRound(): void
End the current betting round which is no longer in progress. Collect the bets and form the pots. (Betting round must not be in progress and betting rounds must not be completed.)
showdown()
Poker.Table.prototype.showdown(): void
Perform a showdown. Evaluate the players' hands and pay the winners. (Betting round must not be in progress and betting rounds must be completed.)
manualShowdown(communityCards, playerHoleCards)
Poker.Table.prototype.manualShowdown(communityCards: Card[], playerHoleCards: { [seatIndex: number]: Card[] }): void
Performs a showdown using explicitly provided community cards and a mapping of players' hole cards. This bypasses dealing and evaluates winners directly. (Betting rounds do not need to be in progress.)
winners()
Poker.Table.prototype.winners(): [SeatIndex, { cards: Card[], ranking: HandRanking, strength: number }, Card[], number][][]
Returns the winner(s) for each pot after showdown. Each winner tuple contains the seat index, the winner's five-card hand (with ranking and strength), the two hole cards of the winner, and the numeric payout for that pot. (Hand must not be in progress.)
For each pot, an array entry will contain one or more winner tuples as described above.
Note that the result will be empty if there is one pot with one single eligible player left in the hand. Access the pots() before calling showdown() to explicitly get the winner if there is only one eligible player left.
automaticActions()
Poker.Table.prototype.automaticActions(): (AutomaticAction | null)[]
Returns the toggled automatic actions at the table. (Hand must be in progress)
canSetAutomaticActions(seatIndex)
Poker.Table.prototype.canSetAutomaticActions(seatIndex: number): boolean
Returns true if the player with a given seatIndex can set an automatic action. (Betting round must be in progress.)
legalAutomaticActions(seatIndex)
Poker.Table.prototype.legalAutomaticActions(seatIndex: number): AutomaticAction[]
Returns an array of legal automatic actions for a player with a given seatIndex. (Player must be allowed to set automatic actions.)
setAutomaticAction(seatIndex, action)
Poker.Table.prototype.setAutomaticAction(seatIndex: number, action: AutomaticAction | null): void
Set the automatic action for a given player. (Player must be allowed to set automatic actions and player must not be the player to act.)
sitDown(seatIndex, buyIn)
Poker.Table.prototype.sitDown(seatIndex: number, buyIn: number): void
Indicate that a player took a given seat with a given buy-in. (Given seat must not be occupied.)
standUp(seatIndex)
Poker.Table.prototype.standUp(seatIndex: number): void
Indicate that a player has left the table. (Given seat must be occupied.)
