UNPKG

shoehive

Version:

WebSocket-based multiplayer game framework for real-time, event-driven gameplay

327 lines (326 loc) 12.1 kB
import { EventBus } from '../events/EventBus'; import { Player } from './Player'; import { Card, Deck, Hand } from './card/index'; import { Seat } from './Seat'; export declare enum TableState { WAITING = "waiting", ACTIVE = "active", ENDED = "ended" } export interface TableOptions { seatCount?: number; [key: string]: unknown; } /** * 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 */ export declare class Table { readonly id: string; private players; private seats; private eventBus; private state; private readonly totalSeats; private readonly maxSeatsPerPlayer; private attributes; private deck; private gameId; private options; constructor(eventBus: EventBus, totalSeats: number, maxSeatsPerPlayer: number, id?: string, gameId?: string, options?: TableOptions); private handleSitRequest; private handleStandRequest; /** * Set up event listeners for this table */ private setupEventListeners; /** * Destroys the table by safely unbinding all event listeners to prevent severe memory leaks. */ destroy(): void; /** * Creates a new deck for the table. Emits TABLE_EVENTS.DECK_CREATED when the deck is created. * @param numberOfDecks - The number of decks to create. */ createDeck({ numberOfDecks }?: { numberOfDecks?: number; }): void; /** * Gets the current deck. * @returns The deck object or null if no deck has been created. */ getDeck(): Deck | null; /** * Shuffles the current deck. Emits TABLE_EVENTS.DECK_SHUFFLED when the deck is shuffled. * @returns True if the deck was shuffled, false if no deck exists. */ shuffleDeck(): boolean; /** * Draws a card from the deck. Emits TABLE_EVENTS.DECK_CARD_DRAWN when a card is drawn. * @param isVisible - Whether the card should be visible to the player. * @returns The drawn card or null if no deck exists. */ drawCard({ isVisible }?: { isVisible?: boolean; }): Card | null; /** * Deals a card to a seat. Emits TABLE_EVENTS.CARD_DEALT when a card is dealt. * @param seatIndex - The index of the seat to deal the card to. * @param isVisible - Whether the card should be visible to the player. * @param handId - The ID of the hand to deal the card to. * @returns True if the card was dealt, false if no deck exists. */ dealCardToSeat({ seatIndex, isVisible, handId, }: { seatIndex: number; isVisible?: boolean; handId?: string; }): boolean; /** * Gets a hand at a specific seat. * @param seatIndex - The index of the seat to get the hand from. * @param handId - The ID of the hand to get. * @returns The hand object or null if no hand exists. */ getHandAtSeat({ seatIndex, handId, }: { seatIndex: number; handId?: string; }): Hand | null; /** * Deals a card to a hand. Emits TABLE_EVENTS.CARD_DEALT when a card is dealt. * @param hand <Hand> - The hand to deal the card to. * @returns True if the card was dealt, false if no seat or hand exists. */ dealCardToHand({ hand }: { hand: Hand; }): boolean; /** * Gets all hands at a specific seat. * @param seatIndex - The index of the seat to get the hands from. * @returns A map of hand IDs to hand objects or null if no seat exists. */ getAllHandsAtSeat({ seatIndex }: { seatIndex: number; }): Map<string, Hand> | null; /** * Clears a hand at a specific seat. Emits TABLE_EVENTS.SEAT_HAND_CLEARED when a hand is cleared. * @param seatIndex - The index of the seat to clear the hand from. * @param handId - The ID of the hand to clear. * @returns True if the hand was cleared, false if no seat exists. */ clearHandAtSeat({ seatIndex, handId, }: { seatIndex: number; handId?: string; }): boolean; /** * Clears all hands at the table. Emits TABLE_EVENTS.SEATS_HANDS_CLEARED when all hands are cleared. */ clearAllHands(): void; /** * Adds a hand to a seat. Emits TABLE_EVENTS.SEAT_HAND_ADDED when a hand is added to a seat. * @param seatIndex - The index of the seat to add the hand to. * @param handId - The ID of the hand to add. * @returns True if the hand was added, false if no seat exists. */ addHandToSeat({ seatIndex, handId }: { seatIndex: number; handId: string; }): boolean; /** * Removes a hand from a seat. Emits TABLE_EVENTS.SEAT_HAND_REMOVED when a hand is removed from a seat. * @param seatIndex - The index of the seat to remove the hand from. * @param handId - The ID of the hand to remove. * @returns True if the hand was removed, false if no seat exists. */ removeHandFromSeat({ seatIndex, handId }: { seatIndex: number; handId: string; }): boolean; /** * Adds a player to the table. Emits TABLE_EVENTS.PLAYER_JOINED when a player joins the table. * @param player - The player to add. * @returns True if the player was added, false if the player is already at a table. */ addPlayer({ player }: { player: Player; }): boolean; /** * Removes a player from the table. Emits TABLE_EVENTS.PLAYER_LEFT when a player leaves the table. * @param playerId - The ID of the player to remove. * @returns True if the player was removed, false if the player is not at the table. */ removePlayer({ playerId }: { playerId: string; }): boolean; /** * Sits a player at a specific seat. Emits TABLE_EVENTS.PLAYER_SAT when a player sits at a seat. * @param playerId - The ID of the player to sit. * @param seatIndex - The index of the seat to sit the player at. * @returns True if the player was seated, false if the seat is invalid or already taken. */ sitPlayerAtSeat({ playerId, seatIndex, }: { playerId: string; seatIndex: number; }): boolean; /** * Removes a player from a seat. Emits TABLE_EVENTS.PLAYER_STOOD when a player stands up from a seat. * @param seatIndex - The index of the seat to remove the player from. * @returns True if the player was removed from the seat, false if the seat is invalid or no player is seated. */ removePlayerFromSeat({ seatIndex }: { seatIndex: number; }): boolean; /** * Gets the number of seats a player is seated at. * @param playerId - The ID of the player to get the seat count for. * @returns The number of seats the player is seated at. */ getPlayerSeatCount({ playerId }: { playerId: string; }): number; /** * Gets the current state of the table. * @returns The current state of the table. */ getState(): TableState; /** * Sets the state of the table. Emits TABLE_EVENTS.STATE_UPDATED when the state is updated. * @param state - The new state of the table. */ setState({ state }: { state: TableState; }): void; /** * Gets the number of players at the table. * @returns The number of players at the table. */ getPlayerCount(): number; /** * Gets all players at the table. * @returns An array of all players at the table. */ getPlayers(): Player[]; /** * Gets a seat at a specific index. * @param seatIndex - The index of the seat to get. * @returns The seat object or null if the index is invalid. */ getSeat({ seatIndex }: { seatIndex: number; }): Seat | null; /** * Gets all seats at the table. * @returns An array of all seats at the table. */ getSeats(): Seat[]; /** * Gets the player at a specific seat. * @param seatIndex - The index of the seat to get the player from. * @returns The player object or null if the seat is invalid. */ getPlayerAtSeat({ seatIndex }: { seatIndex: number; }): Player | null; /** * Broadcasts a message to all players at the table. * @param message - The message to broadcast. */ broadcastMessage({ message }: { message: unknown; }): 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. */ broadcastTableState(): void; /** * Gets the complete table state including all attributes and game state. * Modifies output arrays to reveal hidden cards strictly to their seated owner. * * @param playerId An optional playerId. If matched against a seat owner, returns proprietary hidden cards. * @returns The localized complete table state. */ getTableState({ playerId }?: { playerId?: string; }): unknown; /** * Gets the table metadata for lobby display. * This includes only the essential information needed to display in the lobby. * * @returns The table metadata. */ getTableMetadata(): unknown; /** * Sets an attribute on the table. Emits TABLE_EVENTS.ATTRIBUTE_CHANGED when the attribute is updated. * @param key - The key of the attribute to set. * @param value - The value of the attribute to set. */ setAttribute({ key, value }: { key: string; value: unknown; }): void; /** * Gets an attribute from the table. * @param key - The key of the attribute to get. * @returns The value of the attribute or null if the attribute does not exist. */ getAttribute({ key }: { key: string; }): unknown; /** * Checks if the table has an attribute. * @param key - The key of the attribute to check. * @returns True if the attribute exists, false otherwise. */ hasAttribute({ key }: { key: string; }): boolean; /** * Gets all attributes from the table. * @returns An object containing all attributes. */ getAttributes(): Record<string, unknown>; /** * Sets multiple attributes on the table. Emits TABLE_EVENTS.ATTRIBUTES_CHANGED when any attributes are updated. * @param attributes - An object containing key-value pairs of attributes to set. * @param broadcast - Whether to broadcast the table state after updating the attributes. * @returns True if any attributes were changed, false otherwise. */ setAttributes({ attributes, broadcast, }: { attributes: Record<string, unknown>; broadcast?: boolean; }): boolean; /** * Updates the attributes of the table. Emits TABLE_EVENTS.ATTRIBUTES_CHANGED when any attributes are updated. * @param attributes - An object containing key-value pairs of attributes to set. * @returns True if any attributes were changed, false otherwise. */ updateAttributes({ attributes }: { attributes: Record<string, unknown>; }): boolean; /** * Removes an attribute from the table. Emits TABLE_EVENTS.ATTRIBUTE_CHANGED when the attribute is removed. * @param key - The key of the attribute to remove. */ removeAttribute({ key }: { key: string; }): void; /** * Makes a player stand up from all seats they occupy. * @param playerId - The ID of the player to stand up. * @returns True if the player was removed from at least one seat, false otherwise. */ standPlayerUp({ playerId }: { playerId: string; }): boolean; }