@colyseus/core
Version:
Multiplayer Framework for Node.js.
118 lines (117 loc) • 5.21 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var DevMode_exports = {};
__export(DevMode_exports, {
cacheRoomHistory: () => cacheRoomHistory,
debugDevMode: () => debugDevMode,
getPreviousProcessId: () => getPreviousProcessId,
getProcessRestoreKey: () => getProcessRestoreKey,
getRoomRestoreListKey: () => getRoomRestoreListKey,
isDevMode: () => isDevMode,
reloadFromCache: () => reloadFromCache,
setDevMode: () => setDevMode
});
module.exports = __toCommonJS(DevMode_exports);
var import_debug = __toESM(require("debug"));
var import_Logger = require("../Logger.js");
var import_Debug = require("../Debug.js");
var import_MatchMaker = require("../MatchMaker.js");
const debugDevMode = (0, import_debug.default)("colyseus:devmode");
let isDevMode = false;
function setDevMode(bool) {
isDevMode = bool;
}
async function reloadFromCache() {
const roomHistoryList = Object.entries(await import_MatchMaker.presence.hgetall(getRoomRestoreListKey()));
debugDevMode("rooms to restore: %i", roomHistoryList.length);
for (const [roomId, value] of roomHistoryList) {
const roomHistory = JSON.parse(value);
debugDevMode("restoring room %s (%s)", roomHistory.roomName, roomId);
const recreatedRoomListing = await (0, import_MatchMaker.handleCreateRoom)(roomHistory.roomName, roomHistory.clientOptions, roomId);
const recreatedRoom = (0, import_MatchMaker.getLocalRoomById)(recreatedRoomListing.roomId);
import_Logger.logger.debug(`\u{1F504} room '${roomId}' has been restored.`);
if (roomHistory.hasOwnProperty("state")) {
recreatedRoom.state.decode(roomHistory.state);
recreatedRoom.setState(recreatedRoom.state.clone());
import_Logger.logger.debug(`\u{1F4CB} room '${roomId}' state =>`, recreatedRoom.state.toJSON());
}
recreatedRoom.onRestoreRoom?.(roomHistory["cache"]);
if (roomHistory.clients) {
for (const previousSessionId of roomHistory.clients) {
await (0, import_MatchMaker.remoteRoomCall)(recreatedRoomListing.roomId, "_reserveSeat", [previousSessionId, {}, 20, false, true]);
}
}
}
if (roomHistoryList.length > 0) {
import_Logger.logger.debug("\u2705", roomHistoryList.length, "room(s) have been restored.");
}
}
async function cacheRoomHistory(rooms) {
for (const room of Object.values(rooms)) {
const roomHistoryResult = await import_MatchMaker.presence.hget(getRoomRestoreListKey(), room.roomId);
if (roomHistoryResult) {
try {
const roomHistory = JSON.parse(roomHistoryResult);
roomHistory["cache"] = room.onCacheRoom?.();
debugDevMode("caching room %s (%s)", room.roomName, room.roomId);
if (room.state) {
roomHistory["state"] = room.state.encodeAll();
}
roomHistory["clients"] = room.clients.map((client) => client.sessionId);
for (const sessionId in room["reservedSeats"]) {
roomHistory["clients"].push(sessionId);
}
await import_MatchMaker.presence.hset(getRoomRestoreListKey(), room.roomId, JSON.stringify(roomHistory));
import_Logger.logger.debug(`\u{1F4BE} caching room '${room.roomId}' (clients: ${room.clients.length}, state size: ${(roomHistory["state"] || []).length} bytes)`);
} catch (e) {
(0, import_Debug.debugAndPrintError)(`\u274C couldn't cache room '${room.roomId}', due to:
${e.stack}`);
}
}
}
}
async function getPreviousProcessId(hostname) {
return await import_MatchMaker.presence.hget(getProcessRestoreKey(), hostname);
}
function getRoomRestoreListKey() {
return "roomhistory";
}
function getProcessRestoreKey() {
return "processhistory";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
cacheRoomHistory,
debugDevMode,
getPreviousProcessId,
getProcessRestoreKey,
getRoomRestoreListKey,
isDevMode,
reloadFromCache,
setDevMode
});