UNPKG

shoehive

Version:

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

122 lines (121 loc) 4.45 kB
/// <reference types="node" /> import * as http from "http"; import { EventBus } from "../events/EventBus"; import { MessageRouter } from "../events/MessageRouter"; import { Player } from "./Player"; import { GameManager } from "./GameManager"; import { Lobby } from "./Lobby"; import { AuthModule } from "../transport/AuthModule"; import { TableFactory } from "./TableFactory"; export declare class WebSocketManager { private wss; private eventBus; private messageRouter; private gameManager; private lobby; private players; private authModule?; private disconnectionTimeouts; private reconnectionTimeoutMs; constructor(server: http.Server, eventBus: EventBus, messageRouter: MessageRouter, gameManager: GameManager, authModule?: AuthModule, reconnectionTimeoutMs?: number, lobby?: Lobby, tableFactory?: TableFactory); /** * Sets up the connection handler for the WebSocket server. * This handler authenticates the connection, creates a new player or reconnects an existing one, * and handles incoming messages. */ private setupConnectionHandler; /** * Sets up event listeners for the WebSocket manager. * This listens for lobby state updates and player joined events, * and sends the appropriate messages to all players. */ private setupEventListeners; /** * Sends the initial state to a player. * This includes player details and lobby state. * * @param player The player to send the initial state to. */ private sendInitialState; /** * Distribute player updates to relevant players. * This notifies the player about their own changes and also updates * any tables they're part of. * * @param player The player whose state changed * @param key The attribute that changed * @param value The new value * @param updateTableState Whether to update the table state */ distributePlayerUpdate(player: Player, key: string, value: any, updateTableState?: boolean): void; /** * Distribute multiple player updates to relevant players. * * @param player The player whose state changed * @param attributes The attributes that changed * @param updateTableState Whether to update the table state */ distributePlayerUpdates(player: Player, attributes: Record<string, any>, updateTableState?: boolean): void; /** * Creates a new player or reconnects an existing one. * * @param socket The WebSocket connection. * @param playerId The player ID. * @returns The player object. */ private createOrReconnectPlayer; /** * Setup disconnect handler for a player to manage reconnection timeout * * @param player The player to set up disconnect handler for */ private setupPlayerDisconnectHandler; /** * Permanently remove a player from the game server * * @param playerId The ID of the player to remove */ private removePlayerPermanently; /** * Gets a player by their ID. * * @param playerId The ID of the player to get. * @returns The player object or undefined if the player does not exist. */ getPlayer(playerId: string): Player | undefined; /** * Disconnects a player by their ID without waiting for timeout. * This bypasses the reconnection timeout and immediately removes the player. * * @param playerId The ID of the player to disconnect. */ disconnectPlayer(playerId: string): void; /** * Gets the current reconnection timeout in milliseconds */ getReconnectionTimeout(): number; /** * Sets the reconnection timeout in milliseconds * * @param timeoutMs The timeout in milliseconds (0 to disable reconnection) */ setReconnectionTimeout(timeoutMs: number): void; /** * Gets information about temporarily disconnected players. * This is useful for monitoring and debugging connection issues. * * @returns Array of objects containing information about disconnected players */ getDisconnectedPlayers(): Array<{ id: string; disconnectedAt: number; reconnectionAvailableUntil: number; timeLeftMs: number; }>; /** * Gets the current number of connected players. * * @returns The number of connected players */ getConnectedPlayerCount(): number; }