shoehive
Version:
WebSocket-based multiplayer game framework for real-time, event-driven gameplay
67 lines (66 loc) • 2.22 kB
TypeScript
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: () => void): void;
sendMessage(message: any): void;
setTable(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: string, value: any, 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: Record<string, any>): 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: string): any;
/**
* Get all attributes from the player.
* @returns An object containing all player attributes
*/
getAttributes(): Record<string, any>;
/**
* 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: string): boolean;
/**
* Disconnect the player from the server.
*/
disconnect(): void;
}