npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

poker-ts

v1.4.0

Published

Texas Hold 'Em Poker table model with convenience features for running real games.

Downloads

126

Readme

Poker (TypeScript)

Poker-ts is a poker game engine 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('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()` 🎉 🎉 🎉 

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.

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 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.)

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.

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 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.)

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.)

winners()

Poker.Table.prototype.winners(): [SeatIndex, { cards: Card[], ranking: HandRanking, strength: number }, Card[]][][]

Returns the winning hands for each pot after showdown. (Hand must not be in progress.)

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.)

License

MIT