v0.8.2
Class: Table
Represents a game table with players, seats, and game state.
â Attribute Support
The Table class manages a group of players and seats for a specific game. It emits various events to notify other components about changes in the table state.
This class is responsible for:
- Managing player connections and disconnections
- Handling player messages and commands
- Distributing game events to all players
- Maintaining the game state and rules
Table of contents
Constructors
Properties
Methods
- addHandToSeat
- addPlayer
- broadcastMessage
- broadcastTableState
- clearAllHands
- clearHandAtSeat
- createDeck
- dealCardToHand
- dealCardToSeat
- drawCard
- getAllHandsAtSeat
- getAttribute
- getAttributes
- getDeck
- getHandAtSeat
- getPlayerAtSeat
- getPlayerCount
- getPlayerSeatCount
- getPlayers
- getSeat
- getSeats
- getState
- getTableMetadata
- getTableState
- hasAttribute
- removeAttribute
- removeHandFromSeat
- removePlayer
- removePlayerFromSeat
- setAttribute
- setAttributes
- setState
- shuffleDeck
- sitPlayerAtSeat
- updateAttributes
Constructors
constructor
⢠new Table(eventBus
, totalSeats
, maxSeatsPerPlayer
, id?
): Table
Parameters
Name | Type |
---|---|
eventBus | EventBus |
totalSeats | number |
maxSeatsPerPlayer | number |
id? | string |
Returns
Properties
id
⢠Readonly
id: string
Methods
addHandToSeat
⸠addHandToSeat(seatIndex
, handId
): boolean
Adds a hand to a seat. Emits TABLE_EVENTS.SEAT_HAND_ADDED when a hand is added to a seat.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to add the hand to. |
handId | string | The ID of the hand to add. |
Returns
boolean
True if the hand was added, false if no seat exists.
addPlayer
⸠addPlayer(player
): boolean
Adds a player to the table. Emits TABLE_EVENTS.PLAYER_JOINED when a player joins the table.
Parameters
Name | Type | Description |
---|---|---|
player | Player | The player to add. |
Returns
boolean
True if the player was added, false if the player is already at a table.
broadcastMessage
⸠broadcastMessage(message
): void
Broadcasts a message to all players at the table.
Parameters
Name | Type | Description |
---|---|---|
message | any | The message to broadcast. |
Returns
void
broadcastTableState
⸠broadcastTableState(): void
Broadcasts the current table state to all players at the table. This includes all game-specific state and is only meant for players at this table. Broadcasts a TABLE_EVENTS.STATE_UPDATED event that can be used by other components.
Returns
void
clearAllHands
⸠clearAllHands(): void
Clears all hands at the table. Emits TABLE_EVENTS.SEATS_HANDS_CLEARED when all hands are cleared.
Returns
void
clearHandAtSeat
⸠clearHandAtSeat(seatIndex
, handId?
): boolean
Clears a hand at a specific seat. Emits TABLE_EVENTS.SEAT_HAND_CLEARED when a hand is cleared.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
seatIndex | number | undefined | The index of the seat to clear the hand from. |
handId | string | "main" | The ID of the hand to clear. |
Returns
boolean
True if the hand was cleared, false if no seat exists.
createDeck
⸠createDeck(numberOfDecks?
): void
Creates a new deck for the table. Emits TABLE_EVENTS.DECK_CREATED when the deck is created.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
numberOfDecks | number | 1 | The number of decks to create. |
Returns
void
dealCardToHand
⸠dealCardToHand(hand
): boolean
Deals a card to a hand. Emits TABLE_EVENTS.CARD_DEALT when a card is dealt.
Parameters
Name | Type | Description |
---|---|---|
hand | Hand |
Returns
boolean
True if the card was dealt, false if no seat or hand exists.
dealCardToSeat
⸠dealCardToSeat(seatIndex
, isVisible?
, handId?
): boolean
Deals a card to a seat. Emits TABLE_EVENTS.CARD_DEALT when a card is dealt.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
seatIndex | number | undefined | The index of the seat to deal the card to. |
isVisible | boolean | true | Whether the card should be visible to the player. |
handId | string | "main" | The ID of the hand to deal the card to. |
Returns
boolean
True if the card was dealt, false if no deck exists.
drawCard
⸠drawCard(isVisible?
): null
| Card
Draws a card from the deck. Emits TABLE_EVENTS.DECK_CARD_DRAWN when a card is drawn.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
isVisible | boolean | true | Whether the card should be visible to the player. |
Returns
null
| Card
The drawn card or null if no deck exists.
getAllHandsAtSeat
⸠getAllHandsAtSeat(seatIndex
): null
| Map
<string
, Hand
>
Gets all hands at a specific seat.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to get the hands from. |
Returns
null
| Map
<string
, Hand
>
A map of hand IDs to hand objects or null if no seat exists.
getAttribute
⸠getAttribute(key
): any
Gets an attribute from the table.
Parameters
Name | Type | Description |
---|---|---|
key | string | The key of the attribute to get. |
Returns
any
The value of the attribute or null if the attribute does not exist.
getAttributes
⸠getAttributes(): Record
<string
, any
>
Gets all attributes from the table.
Returns
Record
<string
, any
>
An object containing all attributes.
getDeck
⸠getDeck(): null
| Deck
Gets the current deck.
Returns
null
| Deck
The deck object or null if no deck has been created.
getHandAtSeat
⸠getHandAtSeat(seatIndex
, handId?
): null
| Hand
Gets a hand at a specific seat.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
seatIndex | number | undefined | The index of the seat to get the hand from. |
handId | string | "main" | The ID of the hand to get. |
Returns
null
| Hand
The hand object or null if no hand exists.
getPlayerAtSeat
⸠getPlayerAtSeat(seatIndex
): null
| Player
Gets the player at a specific seat.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to get the player from. |
Returns
null
| Player
The player object or null if the seat is invalid.
getPlayerCount
⸠getPlayerCount(): number
Gets the number of players at the table.
Returns
number
The number of players at the table.
getPlayerSeatCount
⸠getPlayerSeatCount(playerId
): number
Gets the number of seats a player is seated at.
Parameters
Name | Type | Description |
---|---|---|
playerId | string | The ID of the player to get the seat count for. |
Returns
number
The number of seats the player is seated at.
getPlayers
⸠getPlayers(): Player
[]
Gets all players at the table.
Returns
Player
[]
An array of all players at the table.
getSeat
⸠getSeat(seatIndex
): null
| Seat
Gets a seat at a specific index.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to get. |
Returns
null
| Seat
The seat object or null if the index is invalid.
getSeats
⸠getSeats(): Seat
[]
Gets all seats at the table.
Returns
Seat
[]
An array of all seats at the table.
getState
⸠getState(): TableState
Gets the current state of the table.
Returns
The current state of the table.
getTableMetadata
⸠getTableMetadata(): any
Gets the table metadata for lobby display. This includes only the essential information needed to display in the lobby.
Returns
any
The table metadata.
getTableState
⸠getTableState(): any
Gets the complete table state including all attributes and game state. This is used for players who are at the table and need full information. Emits a TABLE_EVENTS.STATE_UPDATED event that can be used by other components.
Returns
any
The complete table state.
hasAttribute
⸠hasAttribute(key
): boolean
Checks if the table has an attribute.
Parameters
Name | Type | Description |
---|---|---|
key | string | The key of the attribute to check. |
Returns
boolean
True if the attribute exists, false otherwise.
removeAttribute
⸠removeAttribute(key
): void
Removes an attribute from the table. Emits TABLE_EVENTS.ATTRIBUTE_CHANGED when the attribute is removed.
Parameters
Name | Type | Description |
---|---|---|
key | string | The key of the attribute to remove. |
Returns
void
removeHandFromSeat
⸠removeHandFromSeat(seatIndex
, handId
): boolean
Removes a hand from a seat. Emits TABLE_EVENTS.SEAT_HAND_REMOVED when a hand is removed from a seat.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to remove the hand from. |
handId | string | The ID of the hand to remove. |
Returns
boolean
True if the hand was removed, false if no seat exists.
removePlayer
⸠removePlayer(playerId
): boolean
Removes a player from the table. Emits TABLE_EVENTS.PLAYER_LEFT when a player leaves the table.
Parameters
Name | Type | Description |
---|---|---|
playerId | string | The ID of the player to remove. |
Returns
boolean
True if the player was removed, false if the player is not at the table.
removePlayerFromSeat
⸠removePlayerFromSeat(seatIndex
): boolean
Removes a player from a seat. Emits TABLE_EVENTS.PLAYER_STOOD when a player stands up from a seat.
Parameters
Name | Type | Description |
---|---|---|
seatIndex | number | The index of the seat to remove the player from. |
Returns
boolean
True if the player was removed from the seat, false if the seat is invalid or no player is seated.
setAttribute
⸠setAttribute(key
, value
): void
Sets an attribute on the table. Emits TABLE_EVENTS.ATTRIBUTE_CHANGED when the attribute is updated.
Parameters
Name | Type | Description |
---|---|---|
key | string | The key of the attribute to set. |
value | any | The value of the attribute to set. |
Returns
void
setAttributes
⸠setAttributes(attributes
, broadcast?
): boolean
Sets multiple attributes on the table. Emits TABLE_EVENTS.ATTRIBUTES_CHANGED when any attributes are updated.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
attributes | Record <string , any > | undefined | An object containing key-value pairs of attributes to set. |
broadcast | boolean | false | Whether to broadcast the table state after updating the attributes. |
Returns
boolean
True if any attributes were changed, false otherwise.
setState
⸠setState(state
): void
Sets the state of the table. Emits TABLE_EVENTS.STATE_UPDATED when the state is updated.
Parameters
Name | Type | Description |
---|---|---|
state | TableState | The new state of the table. |
Returns
void
shuffleDeck
⸠shuffleDeck(): boolean
Shuffles the current deck. Emits TABLE_EVENTS.DECK_SHUFFLED when the deck is shuffled.
Returns
boolean
True if the deck was shuffled, false if no deck exists.
sitPlayerAtSeat
⸠sitPlayerAtSeat(playerId
, seatIndex
): boolean
Sits a player at a specific seat. Emits TABLE_EVENTS.PLAYER_SAT when a player sits at a seat.
Parameters
Name | Type | Description |
---|---|---|
playerId | string | The ID of the player to sit. |
seatIndex | number | The index of the seat to sit the player at. |
Returns
boolean
True if the player was seated, false if the seat is invalid or already taken.
updateAttributes
⸠updateAttributes(attributes
): boolean
Updates the attributes of the table. Emits TABLE_EVENTS.ATTRIBUTES_CHANGED when any attributes are updated.
Parameters
Name | Type | Description |
---|---|---|
attributes | Record <string , any > | An object containing key-value pairs of attributes to set. |
Returns
boolean
True if any attributes were changed, false otherwise.