UNPKG

shoehive

Version:

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

244 lines (243 loc) 10.3 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" } /** * 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; constructor(eventBus: EventBus, totalSeats: number, maxSeatsPerPlayer: number, id?: string); /** * 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?: 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?: 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: 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: 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): 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: 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: 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: 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: 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): 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: 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: 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: 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: 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: 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: 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: number): Player | null; /** * Broadcasts a message to all players at the table. * @param message - The message to broadcast. */ broadcastMessage(message: any): 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. * 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 The complete table state. */ getTableState(): any; /** * Gets the table metadata for lobby display. * This includes only the essential information needed to display in the lobby. * * @returns The table metadata. */ getTableMetadata(): any; /** * 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: string, value: any): 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: string): any; /** * 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: string): boolean; /** * Gets all attributes from the table. * @returns An object containing all attributes. */ getAttributes(): Record<string, any>; /** * 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: Record<string, any>, 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: Record<string, any>): 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: string): void; }