@colyseus/core
Version:
Multiplayer Framework for Node.js.
114 lines (112 loc) • 3.53 kB
JavaScript
"use strict";
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);
// packages/core/src/matchmaker/LocalDriver/Query.ts
var Query_exports = {};
__export(Query_exports, {
Query: () => Query
});
module.exports = __toCommonJS(Query_exports);
var Query = class {
constructor(rooms, conditions) {
this.$rooms = rooms.slice(0);
this.conditions = conditions;
}
sort(options) {
this.sortOptions = options;
return this;
}
applySort(rooms) {
if (!this.sortOptions) {
return rooms;
}
return rooms.sort((room1, room2) => {
for (const field in this.sortOptions) {
const direction = this.sortOptions[field];
if (room1.hasOwnProperty(field)) {
if (direction === 1 || direction === "asc" || direction === "ascending") {
if (room1[field] > room2[field]) {
return 1;
}
if (room1[field] < room2[field]) {
return -1;
}
} else {
if (room1[field] > room2[field]) {
return -1;
}
if (room1[field] < room2[field]) {
return 1;
}
}
} else if (room1.metadata?.hasOwnProperty(field)) {
if (direction === 1 || direction === "asc" || direction === "ascending") {
if (room1.metadata[field] > room2.metadata[field]) {
return 1;
}
if (room1.metadata[field] < room2.metadata[field]) {
return -1;
}
} else {
if (room1.metadata[field] > room2.metadata[field]) {
return -1;
}
if (room1.metadata[field] < room2.metadata[field]) {
return 1;
}
}
}
}
return 0;
});
}
applyFilter(rooms, conditions) {
return rooms.filter(((room) => {
for (const field in conditions) {
if (conditions.hasOwnProperty(field)) {
if (room.hasOwnProperty(field)) {
if (room[field] !== conditions[field]) {
return false;
}
} else if (room.metadata?.hasOwnProperty(field)) {
if (room.metadata[field] !== conditions[field]) {
return false;
}
} else {
return false;
}
}
}
return true;
}));
}
filter(conditions) {
const filtered = this.applyFilter(this.$rooms, conditions);
return this.applySort(filtered);
}
then(resolve, reject) {
const filtered = this.applyFilter(this.$rooms, this.conditions);
const sorted = this.applySort(filtered);
const result = sorted[0];
return resolve(result);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Query
});