/// /// import dgram from "dgram"; import vorpal from "vorpal"; import { BaseRootPacket, GameSettings, PacketDecoder } from "@skeldjs/protocol"; import { VersionInfo } from "@skeldjs/util"; import { EventEmitter, ExtractEventTypes } from "@skeldjs/events"; import { HindenburgConfig, ValidSearchTerm } from "../interfaces"; import { Connection } from "./Connection"; import { Room } from "./Room"; import { RoomEvents } from "./BaseRoom"; import { ClientBanEvent, ClientConnectEvent, ClientDisconnectEvent, RoomBeforeCreateEvent, WorkerBeforeJoinEvent, WorkerGetGameListEvent, WorkerLoadPluginEvent } from "../api"; import { LoadedPlugin, PluginLoader, WorkerPlugin } from "../handlers"; import { Logger } from "../logger"; import { Matchmaker } from "../matchmaker"; export declare type ReliableSerializable = BaseRootPacket & { nonce: number; }; /** * Basic information about a packet received from a client. */ export interface PacketContext { /** * The clent who sent the packet. */ sender?: Connection; /** * Whether or not the packet was sent reliably. */ reliable: boolean; } export declare type WorkerEvents = RoomEvents & ExtractEventTypes<[ ClientBanEvent, ClientConnectEvent, ClientDisconnectEvent, RoomBeforeCreateEvent, WorkerBeforeJoinEvent, WorkerGetGameListEvent, WorkerLoadPluginEvent ]>; export declare class Worker extends EventEmitter { /** * The name of the cluster that this node is apart of. */ readonly clusterName: string; /** * The ID of this node relative to the cluster. */ readonly nodeId: number; /** * The global configuration for Hindenburg. */ config: HindenburgConfig; static serverVersion: string; /** * Logger for this server. */ logger: Logger; /** * Vorpal instance responsible for handling interactive CLI. */ vorpal: vorpal; /** * The server's plugin loader. */ pluginLoader: PluginLoader; loadedPlugins: Map>; /** * A map of ports to UDP sockets that clients can connect to. */ listenSockets: Map; /** * The Http matchmaker for the server, if enabled, see {@link HindenburgConfig.matchmaker}. */ matchmaker?: Matchmaker; /** * All client connections connected to this server, mapped by their address:port, * see {@link Connection.address}. */ connections: Map; /** * All rooms created on this server, mapped by their game code as an integer. * * See {@link Worker.createRoom} */ rooms: Map; /** * The packet decoder used to decode incoming udp packets. */ decoder: PacketDecoder; /** * The last client ID that was used. * * Used for {@link Worker.getNextClientId} to get an incrementing client * ID. */ lastClientId: number; pingInterval: NodeJS.Timeout; protected acceptedVersions: number[]; constructor( /** * The name of the cluster that this node is apart of. */ clusterName: string, /** * The ID of this node relative to the cluster. */ nodeId: number, /** * The global configuration for Hindenburg. */ config: HindenburgConfig, pluginDirectories: string[]); protected _listenPort(port: number): dgram.Socket; protected _unlistenPort(port: number): dgram.Socket | undefined; /** * Bind the socket to the configured port. */ listen(): void; /** * Get the next available client ID. * @example * ```ts * console.log(worker.getNextClientId()); // => 1 * console.log(worker.getNextClientId()); // => 2 * console.log(worker.getNextClientId()); // => 3 * console.log(worker.getNextClientId()); // => 4 * console.log(worker.getNextClientId()); // => 5 * ``` */ getNextClientId(): number; /** * Retrieve or create a connection based on its remote information received * from a [socket `message` event](https://nodejs.org/api/dgram.html#dgram_event_message). */ getOrCreateConnection(listenSocket: dgram.Socket, rinfo: dgram.RemoteInfo): Connection; /** * Remove a connection from this server. * * Note that this does not notify the client of the connection that they have * been disconnected, see {@link Connection.disconnect}. * @param connection The connection to remove. */ removeConnection(connection: Connection): void; registerMessages(): void; isVersionAccepted(version: VersionInfo | number): boolean; registerPacketHandlers(): void; pollClientReliability(): Promise; getRoomRelevancy(room: Room, numImpostors: number, lang: number, mapId: number, quickChat: string, perfectMatches: boolean, ignoreSearchTerms: Set | false): number; updateConfig(newConfig: HindenburgConfig): void; sendRawPacket(listenSocket: dgram.Socket, remote: dgram.RemoteInfo, buffer: Buffer): Promise; /** * Serialize and reliable or unreliably send a packet to a client. * * For reliable packets, packets sent will be reliably recorded and marked * for re-sending if the client does not send an acknowledgement for the * packet. * @param connection The connection to send this packet to. * @param packet The root packet to send. * @example * ```ts * worker.sendPacket(connection, * new ReliablePacket( * connection.getNextNonce(), * [ * new HostGameMessage("ALBERT") * ] * ) * ); * ``` */ sendPacket(connection: Connection, packet: BaseRootPacket): Promise; /** * Handle a message being received via the udp socket. * @param buffer The raw data buffer that was received. * @param rinfo Information about the remote that sent this data. */ handleMessage(listenSocket: dgram.Socket, buffer: Buffer, rinfo: dgram.RemoteInfo): Promise; /** * Generate a 4 or 6 letter room code for a room. * @param len The length of the room code, 4 or 6. * @returns The generated room code as an integer. * @example * ```ts * // Generate a 4 letter code. * const roomCode = generateRoomCode(4); * * console.log(roomCode); // => 1246449490 * ``` * ```ts * // Generate a 6 letter code. * const roomCode = generateRoomCode(6); * * console.log(roomCode); // => -2007212745 * ``` */ generateRoomCode(len: 4 | 6): number; /** * Create a room on this server. * @param code The game code for the room, see {@link Worker.generateRoomCode} * to generate one. * @param options Game options for the room. * @returns The created room. */ createRoom(code: number, options: GameSettings): Promise; attemptJoin(connection: Connection, code: number): Promise; }