mudb
Version:
Real-time database for multiplayer games
71 lines • 2.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var state_1 = require("./state");
var MuRemoteServerState = (function () {
function MuRemoteServerState(schema, windowSize) {
this.tick = 0;
this.history = new state_1.MuStateSet(schema.identity);
this.state = this.history.states[0];
this.windowSize = windowSize;
}
return MuRemoteServerState;
}());
exports.MuRemoteServerState = MuRemoteServerState;
var MuClientState = (function () {
function MuClientState(spec) {
this.tick = 0;
this.windowSize = 0;
this._observedStates = [[0]];
this.schema = spec.schema;
this.client = spec.client;
this.sessionId = spec.client.sessionId;
if (typeof spec.windowSize === 'number') {
this.windowSize = spec.windowSize;
}
this._protocol = spec.client.protocol(state_1.MuDefaultStateSchema);
this.state = spec.schema.client.clone(spec.schema.client.identity);
this.history = new state_1.MuStateSet(spec.schema.client.identity);
this.server = new MuRemoteServerState(spec.schema.server, this.windowSize);
}
MuClientState.prototype.configure = function (spec) {
var _this = this;
this._protocol.configure({
message: {
ackState: function (tick) {
state_1.addObservation(_this._observedStates[0], tick);
},
forgetState: function (horizon) {
state_1.forgetObservation(_this._observedStates[0], horizon);
},
},
raw: function (data, unreliable) {
if (!(data instanceof Uint8Array)) {
return;
}
if (state_1.parseState(data, _this.schema.server, _this.server, _this._protocol.server.message.ackState, _this._protocol.server.message.forgetState)) {
if (spec && spec.state) {
spec.state(_this.server.state, _this.server.tick, !unreliable);
}
}
},
ready: function () {
if (spec && spec.ready) {
spec.ready();
}
},
close: function () {
state_1.garbageCollectStates(_this.schema.client, _this.history, Infinity);
state_1.garbageCollectStates(_this.schema.server, _this.server.history, Infinity);
if (spec && spec.close) {
spec.close();
}
},
});
};
MuClientState.prototype.commit = function (reliable) {
state_1.publishState(this.schema.client, this._observedStates, this, this._protocol.server.sendRaw, !!reliable);
};
return MuClientState;
}());
exports.MuClientState = MuClientState;
//# sourceMappingURL=client.js.map