@colyseus/core
Version:
Multiplayer Framework for Node.js.
124 lines (123 loc) • 4.67 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 LobbyRoom_exports = {};
__export(LobbyRoom_exports, {
LobbyRoom: () => LobbyRoom
});
module.exports = __toCommonJS(LobbyRoom_exports);
var matchMaker = __toESM(require("../MatchMaker.js"));
var import_Lobby = require("../matchmaker/Lobby.js");
var import_Room = require("../Room.js");
class LobbyRoom extends import_Room.Room {
constructor() {
super(...arguments);
// tslint:disable-line
this.rooms = [];
this.clientOptions = {};
}
async onCreate(options) {
this.listing.unlisted = true;
this.unsubscribeLobby = await (0, import_Lobby.subscribeLobby)((roomId, data) => {
const roomIndex = this.rooms.findIndex((room) => room.roomId === roomId);
const clients = this.clients.filter((client) => this.clientOptions[client.sessionId]);
if (!data) {
if (roomIndex !== -1) {
const previousData = this.rooms[roomIndex];
this.rooms.splice(roomIndex, 1);
clients.forEach((client) => {
if (this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter)) {
client.send("-", roomId);
}
});
}
} else if (roomIndex === -1) {
this.rooms.push(data);
clients.forEach((client) => {
if (this.filterItemForClient(data, this.clientOptions[client.sessionId].filter)) {
client.send("+", [roomId, data]);
}
});
} else {
const previousData = this.rooms[roomIndex];
this.rooms[roomIndex] = data;
clients.forEach((client) => {
const hadData = this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter);
const hasData = this.filterItemForClient(data, this.clientOptions[client.sessionId].filter);
if (hadData && !hasData) {
client.send("-", roomId);
} else if (hasData) {
client.send("+", [roomId, data]);
}
});
}
});
this.rooms = await matchMaker.query({ private: false, unlisted: false });
this.onMessage("filter", (client, filter) => {
this.clientOptions[client.sessionId].filter = filter;
client.send("rooms", this.filterItemsForClient(this.clientOptions[client.sessionId]));
});
}
onJoin(client, options) {
this.clientOptions[client.sessionId] = options || {};
client.send("rooms", this.filterItemsForClient(this.clientOptions[client.sessionId]));
}
onLeave(client) {
delete this.clientOptions[client.sessionId];
}
onDispose() {
if (this.unsubscribeLobby) {
this.unsubscribeLobby();
}
}
filterItemsForClient(options) {
const filter = options.filter;
return filter ? this.rooms.filter((room) => this.filterItemForClient(room, filter)) : this.rooms;
}
filterItemForClient(room, filter) {
if (!filter) {
return true;
}
let isAllowed = true;
if (filter.name !== room.name) {
isAllowed = false;
}
if (filter.metadata) {
for (const field in filter.metadata) {
if (room.metadata[field] !== filter.metadata[field]) {
isAllowed = false;
break;
}
}
}
return isAllowed;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LobbyRoom
});