UNPKG

multyx-client

Version:

Framework designed to simplify the creation of multiplayer browser games by addressing the complexities of managing server-client communication, shared state, and input handling

102 lines (101 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = void 0; exports.UncompressUpdate = UncompressUpdate; exports.CompressUpdate = CompressUpdate; function UncompressUpdate(str) { const [target, ...escapedData] = str.split(/;,/); const instruction = target[0]; const specifier = target.slice(1).replace(/;_/g, ';'); const data = escapedData.map(d => d.replace(/;_/g, ';')).map(d => d == "undefined" ? undefined : JSON.parse(d)); if (instruction == '0') return { instruction: 'edit', team: false, path: specifier.split('.'), value: data[0] }; if (instruction == '1') return { instruction: 'edit', team: true, path: specifier.split('.'), value: data[0] }; if (instruction == '2') return { instruction: 'self', property: "controller", data: JSON.parse(specifier) }; if (instruction == '3') return { instruction: 'self', property: "uuid", data: JSON.parse(specifier) }; if (instruction == '4') return { instruction: 'self', property: "constraint", data: JSON.parse(specifier) }; if (instruction == '9') return { instruction: 'self', property: "space", data: JSON.parse(specifier) }; if (instruction == '5') return { instruction: 'resp', name: specifier, response: data[0] }; if (instruction == '6') return { instruction: 'conn', uuid: specifier, publicData: data[0] }; if (instruction == '7') return { instruction: 'dcon', clientUUID: specifier }; if (instruction == '8') return { instruction: 'init', client: JSON.parse(specifier), tps: data[0], constraintTable: data[1], clients: data[2], teams: data[3], space: data[4] }; } function CompressUpdate(update) { let code, pieces; if (update.instruction == 'edit') { code = 0; pieces = [ update.path.join('.'), JSON.stringify(update.value) ]; } else if (update.instruction == 'input') { code = 1; pieces = [update.input, JSON.stringify(update.data)]; } else if (update.instruction == 'resp') { code = 2; pieces = [update.name, JSON.stringify(update.response)]; } if (!pieces) return ''; let compressed = code; for (let i = 0; i < pieces.length; i++) { compressed += pieces[i].replace(/;/g, ';_'); if (i < pieces.length - 1) compressed += ';,'; } return JSON.stringify([compressed]); } class Message { constructor(name, data, native = false) { this.name = name; this.data = data; this.time = Date.now(); this.native = native; } static BundleOperations(deltaTime, operations) { if (!Array.isArray(operations)) operations = [operations]; return JSON.stringify(new Message('_', { operations, deltaTime })); } static Native(update) { return CompressUpdate(update); } static Parse(str) { var _a, _b; const parsed = JSON.parse(str); if (Array.isArray(parsed)) { return new Message('_', parsed, true); } return new Message((_a = parsed.name) !== null && _a !== void 0 ? _a : '', (_b = parsed.data) !== null && _b !== void 0 ? _b : '', false); } static Create(name, data) { if (name.length == 0) throw new Error('Multyx message cannot have empty name'); if (name[0] == '_') name = '_' + name; if (typeof data === 'function') { throw new Error('Multyx data must be JSON storable'); } return JSON.stringify(new Message(name, data)); } } exports.Message = Message;