@dill-pixel/plugin-colyseus
Version:
Colyseus
1,117 lines (1,116 loc) • 117 kB
JavaScript
import { Plugin as un, Logger as dn } from "dill-pixel";
function ln(v) {
if (v.__esModule) return v;
var E = v.default;
if (typeof E == "function") {
var s = function h() {
return this instanceof h ? Reflect.construct(E, arguments, this.constructor) : E.apply(this, arguments);
};
s.prototype = E.prototype;
} else s = {};
return Object.defineProperty(s, "__esModule", { value: !0 }), Object.keys(v).forEach(function(h) {
var y = Object.getOwnPropertyDescriptor(v, h);
Object.defineProperty(s, h, y.get ? y : {
enumerable: !0,
get: function() {
return v[h];
}
});
}), s;
}
var qe = {}, ft = {}, ht;
function pn() {
return ht || (ht = 1, ArrayBuffer.isView || (ArrayBuffer.isView = (v) => v !== null && typeof v == "object" && v.buffer instanceof ArrayBuffer), typeof globalThis > "u" && typeof window < "u" && (window.globalThis = window)), ft;
}
var oe = {}, We = {}, ut;
function Ve() {
return ut || (ut = 1, function(v) {
Object.defineProperty(v, "__esModule", { value: !0 }), v.ServerError = v.CloseCode = void 0, function(s) {
s[s.CONSENTED = 4e3] = "CONSENTED", s[s.DEVMODE_RESTART = 4010] = "DEVMODE_RESTART";
}(v.CloseCode || (v.CloseCode = {}));
class E extends Error {
constructor(h, y) {
super(y), this.name = "ServerError", this.code = h;
}
}
v.ServerError = E;
}(We)), We;
}
var ee = {}, pe = {}, dt;
function gn() {
if (dt) return pe;
dt = 1, Object.defineProperty(pe, "__esModule", { value: !0 }), pe.decode = pe.encode = void 0;
function v(f, u) {
if (this._offset = u, f instanceof ArrayBuffer)
this._buffer = f, this._view = new DataView(this._buffer);
else if (ArrayBuffer.isView(f))
this._buffer = f.buffer, this._view = new DataView(this._buffer, f.byteOffset, f.byteLength);
else
throw new Error("Invalid argument");
}
function E(f, u, i) {
for (var a = "", c = 0, d = u, p = u + i; d < p; d++) {
var l = f.getUint8(d);
if (!(l & 128)) {
a += String.fromCharCode(l);
continue;
}
if ((l & 224) === 192) {
a += String.fromCharCode((l & 31) << 6 | f.getUint8(++d) & 63);
continue;
}
if ((l & 240) === 224) {
a += String.fromCharCode((l & 15) << 12 | (f.getUint8(++d) & 63) << 6 | (f.getUint8(++d) & 63) << 0);
continue;
}
if ((l & 248) === 240) {
c = (l & 7) << 18 | (f.getUint8(++d) & 63) << 12 | (f.getUint8(++d) & 63) << 6 | (f.getUint8(++d) & 63) << 0, c >= 65536 ? (c -= 65536, a += String.fromCharCode((c >>> 10) + 55296, (c & 1023) + 56320)) : a += String.fromCharCode(c);
continue;
}
throw new Error("Invalid byte " + l.toString(16));
}
return a;
}
v.prototype._array = function(f) {
for (var u = new Array(f), i = 0; i < f; i++)
u[i] = this._parse();
return u;
}, v.prototype._map = function(f) {
for (var u = "", i = {}, a = 0; a < f; a++)
u = this._parse(), i[u] = this._parse();
return i;
}, v.prototype._str = function(f) {
var u = E(this._view, this._offset, f);
return this._offset += f, u;
}, v.prototype._bin = function(f) {
var u = this._buffer.slice(this._offset, this._offset + f);
return this._offset += f, u;
}, v.prototype._parse = function() {
var f = this._view.getUint8(this._offset++), u, i = 0, a = 0, c = 0, d = 0;
if (f < 192)
return f < 128 ? f : f < 144 ? this._map(f & 15) : f < 160 ? this._array(f & 15) : this._str(f & 31);
if (f > 223)
return (255 - f + 1) * -1;
switch (f) {
// nil
case 192:
return null;
// false
case 194:
return !1;
// true
case 195:
return !0;
// bin
case 196:
return i = this._view.getUint8(this._offset), this._offset += 1, this._bin(i);
case 197:
return i = this._view.getUint16(this._offset), this._offset += 2, this._bin(i);
case 198:
return i = this._view.getUint32(this._offset), this._offset += 4, this._bin(i);
// ext
case 199:
if (i = this._view.getUint8(this._offset), a = this._view.getInt8(this._offset + 1), this._offset += 2, a === -1) {
var p = this._view.getUint32(this._offset);
return c = this._view.getInt32(this._offset + 4), d = this._view.getUint32(this._offset + 8), this._offset += 12, new Date((c * 4294967296 + d) * 1e3 + p / 1e6);
}
return [a, this._bin(i)];
case 200:
return i = this._view.getUint16(this._offset), a = this._view.getInt8(this._offset + 2), this._offset += 3, [a, this._bin(i)];
case 201:
return i = this._view.getUint32(this._offset), a = this._view.getInt8(this._offset + 4), this._offset += 5, [a, this._bin(i)];
// float
case 202:
return u = this._view.getFloat32(this._offset), this._offset += 4, u;
case 203:
return u = this._view.getFloat64(this._offset), this._offset += 8, u;
// uint
case 204:
return u = this._view.getUint8(this._offset), this._offset += 1, u;
case 205:
return u = this._view.getUint16(this._offset), this._offset += 2, u;
case 206:
return u = this._view.getUint32(this._offset), this._offset += 4, u;
case 207:
return c = this._view.getUint32(this._offset) * Math.pow(2, 32), d = this._view.getUint32(this._offset + 4), this._offset += 8, c + d;
// int
case 208:
return u = this._view.getInt8(this._offset), this._offset += 1, u;
case 209:
return u = this._view.getInt16(this._offset), this._offset += 2, u;
case 210:
return u = this._view.getInt32(this._offset), this._offset += 4, u;
case 211:
return c = this._view.getInt32(this._offset) * Math.pow(2, 32), d = this._view.getUint32(this._offset + 4), this._offset += 8, c + d;
// fixext
case 212:
if (a = this._view.getInt8(this._offset), this._offset += 1, a === 0) {
this._offset += 1;
return;
}
return [a, this._bin(1)];
case 213:
return a = this._view.getInt8(this._offset), this._offset += 1, [a, this._bin(2)];
case 214:
return a = this._view.getInt8(this._offset), this._offset += 1, a === -1 ? (u = this._view.getUint32(this._offset), this._offset += 4, new Date(u * 1e3)) : [a, this._bin(4)];
case 215:
if (a = this._view.getInt8(this._offset), this._offset += 1, a === 0)
return c = this._view.getInt32(this._offset) * Math.pow(2, 32), d = this._view.getUint32(this._offset + 4), this._offset += 8, new Date(c + d);
if (a === -1) {
c = this._view.getUint32(this._offset), d = this._view.getUint32(this._offset + 4), this._offset += 8;
var l = (c & 3) * 4294967296 + d;
return new Date(l * 1e3 + (c >>> 2) / 1e6);
}
return [a, this._bin(8)];
case 216:
return a = this._view.getInt8(this._offset), this._offset += 1, [a, this._bin(16)];
// str
case 217:
return i = this._view.getUint8(this._offset), this._offset += 1, this._str(i);
case 218:
return i = this._view.getUint16(this._offset), this._offset += 2, this._str(i);
case 219:
return i = this._view.getUint32(this._offset), this._offset += 4, this._str(i);
// array
case 220:
return i = this._view.getUint16(this._offset), this._offset += 2, this._array(i);
case 221:
return i = this._view.getUint32(this._offset), this._offset += 4, this._array(i);
// map
case 222:
return i = this._view.getUint16(this._offset), this._offset += 2, this._map(i);
case 223:
return i = this._view.getUint32(this._offset), this._offset += 4, this._map(i);
}
throw new Error("Could not parse");
};
function s(f, u = 0) {
var i = new v(f, u), a = i._parse();
if (i._offset !== f.byteLength)
throw new Error(f.byteLength - i._offset + " trailing bytes");
return a;
}
pe.decode = s;
var h = 4294967296 - 1, y = 17179869184 - 1;
function m(f, u, i) {
for (var a = 0, c = 0, d = i.length; c < d; c++)
a = i.charCodeAt(c), a < 128 ? f.setUint8(u++, a) : a < 2048 ? (f.setUint8(u++, 192 | a >> 6), f.setUint8(u++, 128 | a & 63)) : a < 55296 || a >= 57344 ? (f.setUint8(u++, 224 | a >> 12), f.setUint8(u++, 128 | a >> 6 & 63), f.setUint8(u++, 128 | a & 63)) : (c++, a = 65536 + ((a & 1023) << 10 | i.charCodeAt(c) & 1023), f.setUint8(u++, 240 | a >> 18), f.setUint8(u++, 128 | a >> 12 & 63), f.setUint8(u++, 128 | a >> 6 & 63), f.setUint8(u++, 128 | a & 63));
}
function A(f) {
for (var u = 0, i = 0, a = 0, c = f.length; a < c; a++)
u = f.charCodeAt(a), u < 128 ? i += 1 : u < 2048 ? i += 2 : u < 55296 || u >= 57344 ? i += 3 : (a++, i += 4);
return i;
}
function w(f, u, i) {
var a = typeof i, c = 0, d = 0, p = 0, l = 0, _ = 0, x = 0;
if (a === "string") {
if (_ = A(i), _ < 32)
f.push(_ | 160), x = 1;
else if (_ < 256)
f.push(217, _), x = 2;
else if (_ < 65536)
f.push(218, _ >> 8, _), x = 3;
else if (_ < 4294967296)
f.push(219, _ >> 24, _ >> 16, _ >> 8, _), x = 5;
else
throw new Error("String too long");
return u.push({ _str: i, _length: _, _offset: f.length }), x + _;
}
if (a === "number")
return Math.floor(i) !== i || !isFinite(i) ? (f.push(203), u.push({ _float: i, _length: 8, _offset: f.length }), 9) : i >= 0 ? i < 128 ? (f.push(i), 1) : i < 256 ? (f.push(204, i), 2) : i < 65536 ? (f.push(205, i >> 8, i), 3) : i < 4294967296 ? (f.push(206, i >> 24, i >> 16, i >> 8, i), 5) : (p = i / Math.pow(2, 32) >> 0, l = i >>> 0, f.push(207, p >> 24, p >> 16, p >> 8, p, l >> 24, l >> 16, l >> 8, l), 9) : i >= -32 ? (f.push(i), 1) : i >= -128 ? (f.push(208, i), 2) : i >= -32768 ? (f.push(209, i >> 8, i), 3) : i >= -2147483648 ? (f.push(210, i >> 24, i >> 16, i >> 8, i), 5) : (p = Math.floor(i / Math.pow(2, 32)), l = i >>> 0, f.push(211, p >> 24, p >> 16, p >> 8, p, l >> 24, l >> 16, l >> 8, l), 9);
if (a === "object") {
if (i === null)
return f.push(192), 1;
if (Array.isArray(i)) {
if (_ = i.length, _ < 16)
f.push(_ | 144), x = 1;
else if (_ < 65536)
f.push(220, _ >> 8, _), x = 3;
else if (_ < 4294967296)
f.push(221, _ >> 24, _ >> 16, _ >> 8, _), x = 5;
else
throw new Error("Array too large");
for (c = 0; c < _; c++)
x += w(f, u, i[c]);
return x;
}
if (i instanceof Date) {
var M = i.getTime(), P = Math.floor(M / 1e3), B = (M - P * 1e3) * 1e6;
return P >= 0 && B >= 0 && P <= y ? B === 0 && P <= h ? (f.push(214, 255, P >> 24, P >> 16, P >> 8, P), 6) : (p = P / 4294967296, l = P & 4294967295, f.push(215, 255, B >> 22, B >> 14, B >> 6, p, l >> 24, l >> 16, l >> 8, l), 10) : (p = Math.floor(P / 4294967296), l = P >>> 0, f.push(199, 12, 255, B >> 24, B >> 16, B >> 8, B, p >> 24, p >> 16, p >> 8, p, l >> 24, l >> 16, l >> 8, l), 15);
}
if (i instanceof ArrayBuffer) {
if (_ = i.byteLength, _ < 256)
f.push(196, _), x = 2;
else if (_ < 65536)
f.push(197, _ >> 8, _), x = 3;
else if (_ < 4294967296)
f.push(198, _ >> 24, _ >> 16, _ >> 8, _), x = 5;
else
throw new Error("Buffer too large");
return u.push({ _bin: i, _length: _, _offset: f.length }), x + _;
}
if (typeof i.toJSON == "function")
return w(f, u, i.toJSON());
var Q = [], J = "", q = Object.keys(i);
for (c = 0, d = q.length; c < d; c++)
J = q[c], i[J] !== void 0 && typeof i[J] != "function" && Q.push(J);
if (_ = Q.length, _ < 16)
f.push(_ | 128), x = 1;
else if (_ < 65536)
f.push(222, _ >> 8, _), x = 3;
else if (_ < 4294967296)
f.push(223, _ >> 24, _ >> 16, _ >> 8, _), x = 5;
else
throw new Error("Object too large");
for (c = 0; c < _; c++)
J = Q[c], x += w(f, u, J), x += w(f, u, i[J]);
return x;
}
if (a === "boolean")
return f.push(i ? 195 : 194), 1;
if (a === "undefined")
return f.push(192), 1;
if (typeof i.toJSON == "function")
return w(f, u, i.toJSON());
throw new Error("Could not encode");
}
function I(f) {
var u = [], i = [], a = w(u, i, f), c = new ArrayBuffer(a), d = new DataView(c), p = 0, l = 0, _ = -1;
i.length > 0 && (_ = i[0]._offset);
for (var x, M = 0, P = 0, B = 0, Q = u.length; B < Q; B++)
if (d.setUint8(l + B, u[B]), B + 1 === _) {
if (x = i[p], M = x._length, P = l + _, x._bin)
for (var J = new Uint8Array(x._bin), q = 0; q < M; q++)
d.setUint8(P + q, J[q]);
else x._str ? m(d, P, x._str) : x._float !== void 0 && d.setFloat64(P, x._float);
p++, l += M, i[p] && (_ = i[p]._offset);
}
return c;
}
return pe.encode = I, pe;
}
var Ae = {}, ge = {}, Je, lt;
function vn() {
return lt || (lt = 1, Je = function() {
throw new Error(
"ws does not work in the browser. Browser clients must use the native WebSocket object"
);
}), Je;
}
var pt;
function mn() {
if (pt) return ge;
pt = 1;
var v = ge && ge.__importDefault || function(y) {
return y && y.__esModule ? y : { default: y };
};
Object.defineProperty(ge, "__esModule", { value: !0 }), ge.WebSocketTransport = void 0;
const E = v(vn()), s = globalThis.WebSocket || E.default;
let h = class {
constructor(m) {
this.events = m;
}
send(m) {
m instanceof ArrayBuffer ? this.ws.send(m) : Array.isArray(m) && this.ws.send(new Uint8Array(m).buffer);
}
/**
* @param url URL to connect to
* @param headers custom headers to send with the connection (only supported in Node.js. Web Browsers do not allow setting custom headers)
*/
connect(m, A) {
try {
this.ws = new s(m, { headers: A, protocols: this.protocols });
} catch {
this.ws = new s(m, this.protocols);
}
this.ws.binaryType = "arraybuffer", this.ws.onopen = this.events.onopen, this.ws.onmessage = this.events.onmessage, this.ws.onclose = this.events.onclose, this.ws.onerror = this.events.onerror;
}
close(m, A) {
this.ws.close(m, A);
}
get isOpen() {
return this.ws.readyState === s.OPEN;
}
};
return ge.WebSocketTransport = h, ge;
}
var gt;
function _n() {
if (gt) return Ae;
gt = 1, Object.defineProperty(Ae, "__esModule", { value: !0 }), Ae.Connection = void 0;
const v = mn();
let E = class {
constructor() {
this.events = {}, this.transport = new v.WebSocketTransport(this.events);
}
send(h) {
this.transport.send(h);
}
connect(h, y) {
this.transport.connect(h, y);
}
close(h, y) {
this.transport.close(h, y);
}
get isOpen() {
return this.transport.isOpen;
}
};
return Ae.Connection = E, Ae;
}
var Ke = {}, vt;
function Pt() {
return vt || (vt = 1, function(v) {
Object.defineProperty(v, "__esModule", { value: !0 }), v.utf8Length = v.utf8Read = v.ErrorCode = v.Protocol = void 0, function(h) {
h[h.HANDSHAKE = 9] = "HANDSHAKE", h[h.JOIN_ROOM = 10] = "JOIN_ROOM", h[h.ERROR = 11] = "ERROR", h[h.LEAVE_ROOM = 12] = "LEAVE_ROOM", h[h.ROOM_DATA = 13] = "ROOM_DATA", h[h.ROOM_STATE = 14] = "ROOM_STATE", h[h.ROOM_STATE_PATCH = 15] = "ROOM_STATE_PATCH", h[h.ROOM_DATA_SCHEMA = 16] = "ROOM_DATA_SCHEMA", h[h.ROOM_DATA_BYTES = 17] = "ROOM_DATA_BYTES";
}(v.Protocol || (v.Protocol = {})), function(h) {
h[h.MATCHMAKE_NO_HANDLER = 4210] = "MATCHMAKE_NO_HANDLER", h[h.MATCHMAKE_INVALID_CRITERIA = 4211] = "MATCHMAKE_INVALID_CRITERIA", h[h.MATCHMAKE_INVALID_ROOM_ID = 4212] = "MATCHMAKE_INVALID_ROOM_ID", h[h.MATCHMAKE_UNHANDLED = 4213] = "MATCHMAKE_UNHANDLED", h[h.MATCHMAKE_EXPIRED = 4214] = "MATCHMAKE_EXPIRED", h[h.AUTH_FAILED = 4215] = "AUTH_FAILED", h[h.APPLICATION_ERROR = 4216] = "APPLICATION_ERROR";
}(v.ErrorCode || (v.ErrorCode = {}));
function E(h, y) {
const m = h[y++];
for (var A = "", w = 0, I = y, f = y + m; I < f; I++) {
var u = h[I];
if (!(u & 128)) {
A += String.fromCharCode(u);
continue;
}
if ((u & 224) === 192) {
A += String.fromCharCode((u & 31) << 6 | h[++I] & 63);
continue;
}
if ((u & 240) === 224) {
A += String.fromCharCode((u & 15) << 12 | (h[++I] & 63) << 6 | (h[++I] & 63) << 0);
continue;
}
if ((u & 248) === 240) {
w = (u & 7) << 18 | (h[++I] & 63) << 12 | (h[++I] & 63) << 6 | (h[++I] & 63) << 0, w >= 65536 ? (w -= 65536, A += String.fromCharCode((w >>> 10) + 55296, (w & 1023) + 56320)) : A += String.fromCharCode(w);
continue;
}
throw new Error("Invalid byte " + u.toString(16));
}
return A;
}
v.utf8Read = E;
function s(h = "") {
let y = 0, m = 0;
for (let A = 0, w = h.length; A < w; A++)
y = h.charCodeAt(A), y < 128 ? m += 1 : y < 2048 ? m += 2 : y < 55296 || y >= 57344 ? m += 3 : (A++, m += 4);
return m + 1;
}
v.utf8Length = s;
}(Ke)), Ke;
}
var ve = {}, mt;
function bt() {
if (mt) return ve;
mt = 1, Object.defineProperty(ve, "__esModule", { value: !0 }), ve.getSerializer = ve.registerSerializer = void 0;
const v = {};
function E(h, y) {
v[h] = y;
}
ve.registerSerializer = E;
function s(h) {
const y = v[h];
if (!y)
throw new Error("missing serializer: " + h);
return y;
}
return ve.getSerializer = s, ve;
}
var xe = {}, _t;
function Mt() {
if (_t) return xe;
_t = 1, Object.defineProperty(xe, "__esModule", { value: !0 }), xe.createNanoEvents = void 0;
const v = () => ({
emit(E, ...s) {
let h = this.events[E] || [];
for (let y = 0, m = h.length; y < m; y++)
h[y](...s);
},
events: {},
on(E, s) {
var h;
return !((h = this.events[E]) === null || h === void 0) && h.push(s) || (this.events[E] = [s]), () => {
var y;
this.events[E] = (y = this.events[E]) === null || y === void 0 ? void 0 : y.filter((m) => s !== m);
};
}
});
return xe.createNanoEvents = v, xe;
}
var me = {}, yt;
function yn() {
if (yt) return me;
yt = 1, Object.defineProperty(me, "__esModule", { value: !0 }), me.createSignal = me.EventEmitter = void 0;
class v {
constructor() {
this.handlers = [];
}
register(h, y = !1) {
return this.handlers.push(h), this;
}
invoke(...h) {
this.handlers.forEach((y) => y.apply(this, h));
}
invokeAsync(...h) {
return Promise.all(this.handlers.map((y) => y.apply(this, h)));
}
remove(h) {
const y = this.handlers.indexOf(h);
this.handlers[y] = this.handlers[this.handlers.length - 1], this.handlers.pop();
}
clear() {
this.handlers = [];
}
}
me.EventEmitter = v;
function E() {
const s = new v();
function h(y) {
return s.register(y, this === null);
}
return h.once = (y) => {
const m = function(...A) {
y.apply(this, A), s.remove(m);
};
s.register(m);
}, h.remove = (y) => s.remove(y), h.invoke = (...y) => s.invoke(...y), h.invokeAsync = (...y) => s.invokeAsync(...y), h.clear = () => s.clear(), h;
}
return me.createSignal = E, me;
}
var Te = { exports: {} }, $n = Te.exports, $t;
function Dt() {
return $t || ($t = 1, function(v, E) {
(function(s, h) {
h(E);
})($n, function(s) {
var h = function(t, e) {
return h = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(n, r) {
n.__proto__ = r;
} || function(n, r) {
for (var o in r) Object.prototype.hasOwnProperty.call(r, o) && (n[o] = r[o]);
}, h(t, e);
};
function y(t, e) {
if (typeof e != "function" && e !== null)
throw new TypeError("Class extends value " + String(e) + " is not a constructor or null");
h(t, e);
function n() {
this.constructor = t;
}
t.prototype = e === null ? Object.create(e) : (n.prototype = e.prototype, new n());
}
function m(t, e, n, r) {
var o = arguments.length, g = o < 3 ? e : r, R;
if (typeof Reflect == "object" && typeof Reflect.decorate == "function") g = Reflect.decorate(t, e, n, r);
else for (var O = t.length - 1; O >= 0; O--) (R = t[O]) && (g = (o < 3 ? R(g) : o > 3 ? R(e, n, g) : R(e, n)) || g);
return o > 3 && g && Object.defineProperty(e, n, g), g;
}
function A(t, e, n) {
if (arguments.length === 2) for (var r = 0, o = e.length, g; r < o; r++)
(g || !(r in e)) && (g || (g = Array.prototype.slice.call(e, 0, r)), g[r] = e[r]);
return t.concat(g || Array.prototype.slice.call(e));
}
typeof SuppressedError == "function" && SuppressedError;
var w = 255, I = 213;
s.OPERATION = void 0, function(t) {
t[t.ADD = 128] = "ADD", t[t.REPLACE = 0] = "REPLACE", t[t.DELETE = 64] = "DELETE", t[t.DELETE_AND_ADD = 192] = "DELETE_AND_ADD", t[t.TOUCH = 1] = "TOUCH", t[t.CLEAR = 10] = "CLEAR";
}(s.OPERATION || (s.OPERATION = {}));
var f = (
/** @class */
function() {
function t(e, n, r) {
this.changed = !1, this.changes = /* @__PURE__ */ new Map(), this.allChanges = /* @__PURE__ */ new Set(), this.caches = {}, this.currentCustomOperation = 0, this.ref = e, this.setParent(n, r);
}
return t.prototype.setParent = function(e, n, r) {
var o = this;
if (this.indexes || (this.indexes = this.ref instanceof G ? this.ref._definition.indexes : {}), this.parent = e, this.parentIndex = r, !!n)
if (this.root = n, this.ref instanceof G) {
var g = this.ref._definition;
for (var R in g.schema) {
var O = this.ref[R];
if (O && O.$changes) {
var j = g.indexes[R];
O.$changes.setParent(this.ref, n, j);
}
}
} else typeof this.ref == "object" && this.ref.forEach(function(k, S) {
if (k instanceof G) {
var N = k.$changes, T = o.ref.$changes.indexes[S];
N.setParent(o.ref, o.root, T);
}
});
}, t.prototype.operation = function(e) {
this.changes.set(--this.currentCustomOperation, e);
}, t.prototype.change = function(e, n) {
n === void 0 && (n = s.OPERATION.ADD);
var r = typeof e == "number" ? e : this.indexes[e];
this.assertValidIndex(r, e);
var o = this.changes.get(r);
(!o || o.op === s.OPERATION.DELETE || o.op === s.OPERATION.TOUCH) && this.changes.set(r, {
op: o && o.op === s.OPERATION.DELETE ? s.OPERATION.DELETE_AND_ADD : n,
// : OPERATION.REPLACE,
index: r
}), this.allChanges.add(r), this.changed = !0, this.touchParents();
}, t.prototype.touch = function(e) {
var n = typeof e == "number" ? e : this.indexes[e];
this.assertValidIndex(n, e), this.changes.has(n) || this.changes.set(n, { op: s.OPERATION.TOUCH, index: n }), this.allChanges.add(n), this.touchParents();
}, t.prototype.touchParents = function() {
this.parent && this.parent.$changes.touch(this.parentIndex);
}, t.prototype.getType = function(e) {
if (this.ref._definition) {
var n = this.ref._definition;
return n.schema[n.fieldsByIndex[e]];
} else {
var n = this.parent._definition, r = n.schema[n.fieldsByIndex[this.parentIndex]];
return Object.values(r)[0];
}
}, t.prototype.getChildrenFilter = function() {
var e = this.parent._definition.childFilters;
return e && e[this.parentIndex];
}, t.prototype.getValue = function(e) {
return this.ref.getByIndex(e);
}, t.prototype.delete = function(e) {
var n = typeof e == "number" ? e : this.indexes[e];
if (n === void 0) {
console.warn("@colyseus/schema ".concat(this.ref.constructor.name, ": trying to delete non-existing index: ").concat(e, " (").concat(n, ")"));
return;
}
var r = this.getValue(n);
this.changes.set(n, { op: s.OPERATION.DELETE, index: n }), this.allChanges.delete(n), delete this.caches[n], r && r.$changes && (r.$changes.parent = void 0), this.changed = !0, this.touchParents();
}, t.prototype.discard = function(e, n) {
var r = this;
e === void 0 && (e = !1), n === void 0 && (n = !1), this.ref instanceof G || this.changes.forEach(function(o) {
if (o.op === s.OPERATION.DELETE) {
var g = r.ref.getIndex(o.index);
delete r.indexes[g];
}
}), this.changes.clear(), this.changed = e, n && this.allChanges.clear(), this.currentCustomOperation = 0;
}, t.prototype.discardAll = function() {
var e = this;
this.changes.forEach(function(n) {
var r = e.getValue(n.index);
r && r.$changes && r.$changes.discardAll();
}), this.discard();
}, t.prototype.cache = function(e, n) {
this.caches[e] = n;
}, t.prototype.clone = function() {
return new t(this.ref, this.parent, this.root);
}, t.prototype.ensureRefId = function() {
this.refId === void 0 && (this.refId = this.root.getNextUniqueId());
}, t.prototype.assertValidIndex = function(e, n) {
if (e === void 0)
throw new Error('ChangeTree: missing index for field "'.concat(n, '"'));
}, t;
}()
);
function u(t, e, n, r) {
return t[e] || (t[e] = []), t[e].push(n), r == null || r.forEach(function(o, g) {
return n(o, g);
}), function() {
return a(t[e], t[e].indexOf(n));
};
}
function i(t) {
var e = this, n = typeof this.$changes.getType() != "string";
this.$items.forEach(function(r, o) {
t.push({
refId: e.$changes.refId,
op: s.OPERATION.DELETE,
field: o,
value: void 0,
previousValue: r
}), n && e.$changes.root.removeRef(r.$changes.refId);
});
}
function a(t, e) {
if (e === -1 || e >= t.length)
return !1;
for (var n = t.length - 1, r = e; r < n; r++)
t[r] = t[r + 1];
return t.length = n, !0;
}
var c = function(t, e) {
var n = t.toString(), r = e.toString();
return n < r ? -1 : n > r ? 1 : 0;
};
function d(t) {
return t.$proxy = !0, t = new Proxy(t, {
get: function(e, n) {
return typeof n != "symbol" && !isNaN(n) ? e.at(n) : e[n];
},
set: function(e, n, r) {
if (typeof n != "symbol" && !isNaN(n)) {
var o = Array.from(e.$items.keys()), g = parseInt(o[n] || n);
r == null ? e.deleteAt(g) : e.setAt(g, r);
} else
e[n] = r;
return !0;
},
deleteProperty: function(e, n) {
return typeof n == "number" ? e.deleteAt(n) : delete e[n], !0;
},
has: function(e, n) {
return typeof n != "symbol" && !isNaN(Number(n)) ? e.$items.has(Number(n)) : Reflect.has(e, n);
}
}), t;
}
var p = (
/** @class */
function() {
function t() {
for (var e = [], n = 0; n < arguments.length; n++)
e[n] = arguments[n];
this.$changes = new f(this), this.$items = /* @__PURE__ */ new Map(), this.$indexes = /* @__PURE__ */ new Map(), this.$refId = 0, this.push.apply(this, e);
}
return t.prototype.onAdd = function(e, n) {
return n === void 0 && (n = !0), u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.ADD, e, n ? this.$items : void 0);
}, t.prototype.onRemove = function(e) {
return u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.DELETE, e);
}, t.prototype.onChange = function(e) {
return u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.REPLACE, e);
}, t.is = function(e) {
return (
// type format: ["string"]
Array.isArray(e) || // type format: { array: "string" }
e.array !== void 0
);
}, Object.defineProperty(t.prototype, "length", {
get: function() {
return this.$items.size;
},
set: function(e) {
e === 0 ? this.clear() : this.splice(e, this.length - e);
},
enumerable: !1,
configurable: !0
}), t.prototype.push = function() {
for (var e = this, n = [], r = 0; r < arguments.length; r++)
n[r] = arguments[r];
var o;
return n.forEach(function(g) {
o = e.$refId++, e.setAt(o, g);
}), o;
}, t.prototype.pop = function() {
var e = Array.from(this.$indexes.values()).pop();
if (e !== void 0) {
this.$changes.delete(e), this.$indexes.delete(e);
var n = this.$items.get(e);
return this.$items.delete(e), n;
}
}, t.prototype.at = function(e) {
if (e = Math.trunc(e) || 0, e < 0 && (e += this.length), !(e < 0 || e >= this.length)) {
var n = Array.from(this.$items.keys())[e];
return this.$items.get(n);
}
}, t.prototype.setAt = function(e, n) {
var r, o;
if (n == null) {
console.error("ArraySchema items cannot be null nor undefined; Use `deleteAt(index)` instead.");
return;
}
if (this.$items.get(e) !== n) {
n.$changes !== void 0 && n.$changes.setParent(this, this.$changes.root, e);
var g = (o = (r = this.$changes.indexes[e]) === null || r === void 0 ? void 0 : r.op) !== null && o !== void 0 ? o : s.OPERATION.ADD;
this.$changes.indexes[e] = e, this.$indexes.set(e, e), this.$items.set(e, n), this.$changes.change(e, g);
}
}, t.prototype.deleteAt = function(e) {
var n = Array.from(this.$items.keys())[e];
return n === void 0 ? !1 : this.$deleteAt(n);
}, t.prototype.$deleteAt = function(e) {
return this.$changes.delete(e), this.$indexes.delete(e), this.$items.delete(e);
}, t.prototype.clear = function(e) {
this.$changes.discard(!0, !0), this.$changes.indexes = {}, this.$indexes.clear(), e && i.call(this, e), this.$items.clear(), this.$changes.operation({ index: 0, op: s.OPERATION.CLEAR }), this.$changes.touchParents();
}, t.prototype.concat = function() {
for (var e, n = [], r = 0; r < arguments.length; r++)
n[r] = arguments[r];
return new (t.bind.apply(t, A([void 0], (e = Array.from(this.$items.values())).concat.apply(e, n), !1)))();
}, t.prototype.join = function(e) {
return Array.from(this.$items.values()).join(e);
}, t.prototype.reverse = function() {
var e = this, n = Array.from(this.$items.keys()), r = Array.from(this.$items.values()).reverse();
return r.forEach(function(o, g) {
e.setAt(n[g], o);
}), this;
}, t.prototype.shift = function() {
var e = Array.from(this.$items.keys()), n = e.shift();
if (n !== void 0) {
var r = this.$items.get(n);
return this.$deleteAt(n), r;
}
}, t.prototype.slice = function(e, n) {
var r = new t();
return r.push.apply(r, Array.from(this.$items.values()).slice(e, n)), r;
}, t.prototype.sort = function(e) {
var n = this;
e === void 0 && (e = c);
var r = Array.from(this.$items.keys()), o = Array.from(this.$items.values()).sort(e);
return o.forEach(function(g, R) {
n.setAt(r[R], g);
}), this;
}, t.prototype.splice = function(e, n) {
n === void 0 && (n = this.length - e);
for (var r = [], o = 2; o < arguments.length; o++)
r[o - 2] = arguments[o];
for (var g = Array.from(this.$items.keys()), R = [], O = e; O < e + n; O++)
R.push(this.$items.get(g[O])), this.$deleteAt(g[O]);
for (var O = 0; O < r.length; O++)
this.setAt(e + O, r[O]);
return R;
}, t.prototype.unshift = function() {
for (var e = this, n = [], r = 0; r < arguments.length; r++)
n[r] = arguments[r];
var o = this.length, g = n.length, R = Array.from(this.$items.values());
return n.forEach(function(O, j) {
e.setAt(j, O);
}), R.forEach(function(O, j) {
e.setAt(g + j, O);
}), o + g;
}, t.prototype.indexOf = function(e, n) {
return Array.from(this.$items.values()).indexOf(e, n);
}, t.prototype.lastIndexOf = function(e, n) {
return n === void 0 && (n = this.length - 1), Array.from(this.$items.values()).lastIndexOf(e, n);
}, t.prototype.every = function(e, n) {
return Array.from(this.$items.values()).every(e, n);
}, t.prototype.some = function(e, n) {
return Array.from(this.$items.values()).some(e, n);
}, t.prototype.forEach = function(e, n) {
Array.from(this.$items.values()).forEach(e, n);
}, t.prototype.map = function(e, n) {
return Array.from(this.$items.values()).map(e, n);
}, t.prototype.filter = function(e, n) {
return Array.from(this.$items.values()).filter(e, n);
}, t.prototype.reduce = function(e, n) {
return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
}, t.prototype.reduceRight = function(e, n) {
return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
}, t.prototype.find = function(e, n) {
return Array.from(this.$items.values()).find(e, n);
}, t.prototype.findIndex = function(e, n) {
return Array.from(this.$items.values()).findIndex(e, n);
}, t.prototype.fill = function(e, n, r) {
throw new Error("ArraySchema#fill() not implemented");
}, t.prototype.copyWithin = function(e, n, r) {
throw new Error("ArraySchema#copyWithin() not implemented");
}, t.prototype.toString = function() {
return this.$items.toString();
}, t.prototype.toLocaleString = function() {
return this.$items.toLocaleString();
}, t.prototype[Symbol.iterator] = function() {
return Array.from(this.$items.values())[Symbol.iterator]();
}, Object.defineProperty(t, Symbol.species, {
get: function() {
return t;
},
enumerable: !1,
configurable: !0
}), t.prototype.entries = function() {
return this.$items.entries();
}, t.prototype.keys = function() {
return this.$items.keys();
}, t.prototype.values = function() {
return this.$items.values();
}, t.prototype.includes = function(e, n) {
return Array.from(this.$items.values()).includes(e, n);
}, t.prototype.flatMap = function(e, n) {
throw new Error("ArraySchema#flatMap() is not supported.");
}, t.prototype.flat = function(e) {
throw new Error("ArraySchema#flat() is not supported.");
}, t.prototype.findLast = function() {
var e = Array.from(this.$items.values());
return e.findLast.apply(e, arguments);
}, t.prototype.findLastIndex = function() {
var e = Array.from(this.$items.values());
return e.findLastIndex.apply(e, arguments);
}, t.prototype.with = function(e, n) {
var r = Array.from(this.$items.values());
return r[e] = n, new (t.bind.apply(t, A([void 0], r, !1)))();
}, t.prototype.toReversed = function() {
return Array.from(this.$items.values()).reverse();
}, t.prototype.toSorted = function(e) {
return Array.from(this.$items.values()).sort(e);
}, t.prototype.toSpliced = function(e, n) {
var r = Array.from(this.$items.values());
return r.toSpliced.apply(r, arguments);
}, t.prototype.setIndex = function(e, n) {
this.$indexes.set(e, n);
}, t.prototype.getIndex = function(e) {
return this.$indexes.get(e);
}, t.prototype.getByIndex = function(e) {
return this.$items.get(this.$indexes.get(e));
}, t.prototype.deleteByIndex = function(e) {
var n = this.$indexes.get(e);
this.$items.delete(n), this.$indexes.delete(e);
}, t.prototype.toArray = function() {
return Array.from(this.$items.values());
}, t.prototype.toJSON = function() {
return this.toArray().map(function(e) {
return typeof e.toJSON == "function" ? e.toJSON() : e;
});
}, t.prototype.clone = function(e) {
var n;
return e ? n = new (t.bind.apply(t, A([void 0], Array.from(this.$items.values()), !1)))() : n = new (t.bind.apply(t, A([void 0], this.map(function(r) {
return r.$changes ? r.clone() : r;
}), !1)))(), n;
}, t;
}()
);
function l(t) {
return t.$proxy = !0, t = new Proxy(t, {
get: function(e, n) {
return typeof n != "symbol" && // accessing properties
typeof e[n] > "u" ? e.get(n) : e[n];
},
set: function(e, n, r) {
return typeof n != "symbol" && n.indexOf("$") === -1 && n !== "onAdd" && n !== "onRemove" && n !== "onChange" ? e.set(n, r) : e[n] = r, !0;
},
deleteProperty: function(e, n) {
return e.delete(n), !0;
}
}), t;
}
var _ = (
/** @class */
function() {
function t(e) {
var n = this;
if (this.$changes = new f(this), this.$items = /* @__PURE__ */ new Map(), this.$indexes = /* @__PURE__ */ new Map(), this.$refId = 0, e)
if (e instanceof Map || e instanceof t)
e.forEach(function(o, g) {
return n.set(g, o);
});
else
for (var r in e)
this.set(r, e[r]);
}
return t.prototype.onAdd = function(e, n) {
return n === void 0 && (n = !0), u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.ADD, e, n ? this.$items : void 0);
}, t.prototype.onRemove = function(e) {
return u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.DELETE, e);
}, t.prototype.onChange = function(e) {
return u(this.$callbacks || (this.$callbacks = {}), s.OPERATION.REPLACE, e);
}, t.is = function(e) {
return e.map !== void 0;
}, t.prototype[Symbol.iterator] = function() {
return this.$items[Symbol.iterator]();
}, Object.defineProperty(t.prototype, Symbol.toStringTag, {
get: function() {
return this.$items[Symbol.toStringTag];
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t, Symbol.species, {
get: function() {
return t;
},
enumerable: !1,
configurable: !0
}), t.prototype.set = function(e, n) {
if (n == null)
throw new Error("MapSchema#set('".concat(e, "', ").concat(n, "): trying to set ").concat(n, " value on '").concat(e, "'."));
e = e.toString();
var r = typeof this.$changes.indexes[e] < "u", o = r ? this.$changes.indexes[e] : this.$refId++, g = r ? s.OPERATION.REPLACE : s.OPERATION.ADD, R = n.$changes !== void 0;
if (R && n.$changes.setParent(this, this.$changes.root, o), !r)
this.$changes.indexes[e] = o, this.$indexes.set(o, e);
else {
if (!R && this.$items.get(e) === n)
return;
R && // if is schema, force ADD operation if value differ from previous one.
this.$items.get(e) !== n && (g = s.OPERATION.ADD);
}
return this.$items.set(e, n), this.$changes.change(e, g), this;
}, t.prototype.get = function(e) {
return this.$items.get(e);
}, t.prototype.delete = function(e) {
return this.$changes.delete(e.toString()), this.$items.delete(e);
}, t.prototype.clear = function(e) {
this.$changes.discard(!0, !0), this.$changes.indexes = {}, this.$indexes.clear(), e && i.call(this, e), this.$items.clear(), this.$changes.operation({ index: 0, op: s.OPERATION.CLEAR }), this.$changes.touchParents();
}, t.prototype.has = function(e) {
return this.$items.has(e);
}, t.prototype.forEach = function(e) {
this.$items.forEach(e);
}, t.prototype.entries = function() {
return this.$items.entries();
}, t.prototype.keys = function() {
return this.$items.keys();
}, t.prototype.values = function() {
return this.$items.values();
}, Object.defineProperty(t.prototype, "size", {
get: function() {
return this.$items.size;
},
enumerable: !1,
configurable: !0
}), t.prototype.setIndex = function(e, n) {
this.$indexes.set(e, n);
}, t.prototype.getIndex = function(e) {
return this.$indexes.get(e);
}, t.prototype.getByIndex = function(e) {
return this.$items.get(this.$indexes.get(e));
}, t.prototype.deleteByIndex = function(e) {
var n = this.$indexes.get(e);
this.$items.delete(n), this.$indexes.delete(e);
}, t.prototype.toJSON = function() {
var e = {};
return this.forEach(function(n, r) {
e[r] = typeof n.toJSON == "function" ? n.toJSON() : n;
}), e;
}, t.prototype.clone = function(e) {
var n;
return e ? n = Object.assign(new t(), this) : (n = new t(), this.forEach(function(r, o) {
r.$changes ? n.set(o, r.clone()) : n.set(o, r);
})), n;
}, t;
}()
), x = {};
function M(t, e) {
x[t] = e;
}
function P(t) {
return x[t];
}
var B = (
/** @class */
function() {
function t() {
this.indexes = {}, this.fieldsByIndex = {}, this.deprecated = {}, this.descriptors = {};
}
return t.create = function(e) {
var n = new t();
return n.schema = Object.assign({}, e && e.schema || {}), n.indexes = Object.assign({}, e && e.indexes || {}), n.fieldsByIndex = Object.assign({}, e && e.fieldsByIndex || {}), n.descriptors = Object.assign({}, e && e.descriptors || {}), n.deprecated = Object.assign({}, e && e.deprecated || {}), n;
}, t.prototype.addField = function(e, n) {
var r = this.getNextFieldIndex();
this.fieldsByIndex[r] = e, this.indexes[e] = r, this.schema[e] = Array.isArray(n) ? { array: n[0] } : n;
}, t.prototype.hasField = function(e) {
return this.indexes[e] !== void 0;
}, t.prototype.addFilter = function(e, n) {
return this.filters || (this.filters = {}, this.indexesWithFilters = []), this.filters[this.indexes[e]] = n, this.indexesWithFilters.push(this.indexes[e]), !0;
}, t.prototype.addChildrenFilter = function(e, n) {
var r = this.indexes[e], o = this.schema[e];
if (P(Object.keys(o)[0]))
return this.childFilters || (this.childFilters = {}), this.childFilters[r] = n, !0;
console.warn("@filterChildren: field '".concat(e, "' can't have children. Ignoring filter."));
}, t.prototype.getChildrenFilter = function(e) {
return this.childFilters && this.childFilters[this.indexes[e]];
}, t.prototype.getNextFieldIndex = function() {
return Object.keys(this.schema || {}).length;
}, t;
}()
);
function Q(t) {
return t._context && t._context.useFilters;
}
var J = (
/** @class */
function() {
function t() {
this.types = {}, this.schemas = /* @__PURE__ */ new Map(), this.useFilters = !1;
}
return t.prototype.has = function(e) {
return this.schemas.has(e);
}, t.prototype.get = function(e) {
return this.types[e];
}, t.prototype.add = function(e, n) {
n === void 0 && (n = this.schemas.size), e._definition = B.create(e._definition), e._typeid = n, this.types[n] = e, this.schemas.set(e, n);
}, t.create = function(e) {
return e === void 0 && (e = {}), function(n) {
return e.context || (e.context = new t()), X(n, e);
};
}, t;
}()
), q = new J();
function X(t, e) {
return e === void 0 && (e = {}), function(n, r) {
var o = e.context || q, g = n.constructor;
if (g._context = o, !t)
throw new Error("".concat(g.name, ': @type() reference provided for "').concat(r, `" is undefined. Make sure you don't have any circular dependencies.`));
o.has(g) || o.add(g);
var R = g._definition;
if (R.addField(r, t), R.descriptors[r]) {
if (R.deprecated[r])
return;
try {
throw new Error("@colyseus/schema: Duplicate '".concat(r, "' definition on '").concat(g.name, `'.
Check @type() annotation`));
} catch (T) {
var O = T.stack.split(`
`)[4].trim();
throw new Error("".concat(T.message, " ").concat(O));
}
}
var j = p.is(t), k = !j && _.is(t);
if (typeof t != "string" && !G.is(t)) {
var S = Object.values(t)[0];
typeof S != "string" && !o.has(S) && o.add(S);
}
if (e.manual) {
R.descriptors[r] = {
enumerable: !0,
configurable: !0,
writable: !0
};
return;
}
var N = "_".concat(r);
R.descriptors[N] = {
enumerable: !1,
configurable: !1,
writable: !0
}, R.descriptors[r] = {
get: function() {
return this[N];
},
set: function(T) {
T !== this[N] && (T != null ? (j && !(T instanceof p) && (T = new (p.bind.apply(p, A([void 0], T, !1)))()), k && !(T instanceof _) && (T = new _(T)), T.$proxy === void 0 && (k ? T = l(T) : j && (T = d(T))), this.$changes.change(r), T.$changes && T.$changes.setParent(this, this.$changes.root, this._definition.indexes[r])) : this[N] !== void 0 && this.$changes.delete(r), this[N] = T);
},
enumerable: !0,
configurable: !0
};
};
}
function jt(t) {
return function(e, n) {
var r = e.constructor, o = r._definition;
o.addFilter(n, t) && (r._context.useFilters = !0);
};
}
function Ut(t) {
return function(e, n) {
var r = e.constructor, o = r._definition;
o.addChildrenFilter(n, t) && (r._context.useFilters = !0);
};
}
function zt(t) {
return t === void 0 && (t = !0), function(e, n) {
var r = e.constructor, o = r._definition;
o.deprecated[n] = !0, t && (o.descriptors[n] = {
get: function() {
throw new Error("".concat(n, " is deprecated."));
},
set: function(g) {
},
enumerable: !1,
configurable: !0
});
};
}
function Bt(t, e, n) {
n === void 0 && (n = {}), n.context || (n.context = t._context || n.context || q);
for (var r in e)
X(e[r], n)(t.prototype, r);
return t;
}
function Ft(t) {
for (var e = 0, n = 0, r = 0, o = t.length; r < o; r++)
e = t.charCodeAt(r), e < 128 ? n += 1 : e < 2048 ? n += 2 : e < 55296 || e >= 57344 ? n += 3 : (r++, n += 4);
return n;
}
function Ye(t, e, n) {
for (var r = 0, o = 0, g = n.length; o < g; o++)
r = n.charCodeAt(o), r < 128 ? t[e++] = r : r < 2048 ? (t[e++] = 192 | r >> 6, t[e++] = 128 | r & 63) : r < 55296 || r >= 57344 ? (t[e++] = 224 | r >> 12, t[e++] = 128 | r >> 6 & 63, t[e++] = 128 | r & 63) : (o++, r = 65536 + ((r & 1023) << 10 | n.charCodeAt(o) & 1023), t[e++] = 240 | r >> 18, t[e++] = 128 | r >> 12 & 63, t[e++] = 128 | r >> 6 & 63, t[e++] = 128 | r & 63);
}
function Xe(t, e) {
t.push(e & 255);
}
function K(t, e) {
t.push(e & 255);