UNPKG

shoehive

Version:

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

91 lines (90 loc) 2.74 kB
import { EventBus } from '../events/EventBus'; import { Table } from './Table'; import * as WebSocket from 'ws'; /** * Represents a connected client in the game. * * ✅ Attribute Support * * The Player class handles communication with the client and keeps track * of the player's current table and custom attributes. * */ export declare class Player { readonly id: string; private socket; private table; private eventBus; private attributes; private disconnectCallbacks; constructor(socket: WebSocket.WebSocket, eventBus: EventBus, id?: string); private setupSocketListeners; /** * Register a callback to be called when the player disconnects * @param callback The function to call when the player disconnects */ onDisconnect({ callback }: { callback: () => void; }): void; sendMessage({ message }: { message: unknown; }): void; /** * Updates the socket connection for this player. * Useful for handling reconnections without losing player state. * @param socket The new WebSocket connection */ setSocket({ socket }: { socket: WebSocket.WebSocket; }): void; setTable({ table }: { table: Table | null; }): void; getTable(): Table | null; /** * Set a single attribute on the player and emit an event for the change. * * @param key The attribute name * @param value The attribute value * @param notify Whether to emit an event (defaults to true) */ setAttribute({ key, value, notify, }: { key: string; value: unknown; notify?: boolean; }): void; /** * Set multiple attributes at once and emit a single event. * This is more efficient than calling setAttribute multiple times. * * @param attributes Object containing attribute key-value pairs */ setAttributes({ attributes }: { attributes: Record<string, unknown>; }): void; /** * Get a single attribute from the player. * @param key - The key of the attribute to get * @returns The value of the attribute, or undefined if it doesn't exist */ getAttribute({ key }: { key: string; }): unknown; /** * Get all attributes from the player. * @returns An object containing all player attributes */ getAttributes(): Record<string, unknown>; /** * Check if the player 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; /** * Disconnect the player from the server. */ disconnect(): void; }