UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

90 lines (89 loc) 3.13 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var RegisteredHandler_exports = {}; __export(RegisteredHandler_exports, { INVALID_OPTION_KEYS: () => INVALID_OPTION_KEYS, RegisteredHandler: () => RegisteredHandler }); module.exports = __toCommonJS(RegisteredHandler_exports); var import_events = require("events"); var import_Logger = require("../Logger.js"); var import_Lobby = require("./Lobby.js"); const INVALID_OPTION_KEYS = [ "clients", "locked", "private", // 'maxClients', - maxClients can be useful as filter options "metadata", "name", "processId", "roomId" ]; class RegisteredHandler extends import_events.EventEmitter { constructor(name, klass, options) { super(); this.name = name; this.klass = klass; this.options = options; this.filterOptions = []; if (typeof klass !== "function") { import_Logger.logger.debug("You are likely not importing your room class correctly."); throw new Error(`class is expected but ${typeof klass} was provided.`); } } enableRealtimeListing() { this.on("create", (room) => (0, import_Lobby.updateLobby)(room)); this.on("lock", (room) => (0, import_Lobby.updateLobby)(room)); this.on("unlock", (room) => (0, import_Lobby.updateLobby)(room)); this.on("join", (room) => (0, import_Lobby.updateLobby)(room)); this.on("leave", (room, _, willDispose) => { if (!willDispose) { (0, import_Lobby.updateLobby)(room); } }); this.on("dispose", (room) => (0, import_Lobby.updateLobby)(room, false)); this.on("visibility-change", (room, isVisible) => (0, import_Lobby.updateLobby)(room, isVisible)); return this; } filterBy(options) { this.filterOptions = options; return this; } sortBy(options) { this.sortOptions = options; return this; } getFilterOptions(options) { return this.filterOptions.reduce((prev, curr, i, arr) => { const field = arr[i]; if (options.hasOwnProperty(field)) { if (INVALID_OPTION_KEYS.indexOf(field) !== -1) { import_Logger.logger.warn(`option "${field}" has internal usage and is going to be ignored.`); } else { prev[field] = options[field]; } } return prev; }, {}); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { INVALID_OPTION_KEYS, RegisteredHandler });