refactor: simplify informer, state per informer
This commit is contained in:
parent
5bab361d43
commit
ca1a89c994
|
@ -16,6 +16,8 @@ class InformedBase extends Trait {
|
|||
}
|
||||
|
||||
initialize() {
|
||||
this.entityReducer = this.createIndexedReducer();
|
||||
this._informState = I.Map();
|
||||
this._sentSelf = false;
|
||||
this._socket = undefined;
|
||||
this.lastNearby = I.Set();
|
||||
|
@ -58,6 +60,32 @@ class InformedBase extends Trait {
|
|||
};
|
||||
}
|
||||
|
||||
reduceStateDiff(diff) {
|
||||
if (!diff || !diff.room || !diff.room.layers) {
|
||||
return diff
|
||||
}
|
||||
const room = this.entity.room;
|
||||
for (const index in diff.room.layers) {
|
||||
// Index entities.
|
||||
const layer = room.layer(index);
|
||||
const visibleEntities = layer.visibleEntities(
|
||||
[0, 0, 640, 360]
|
||||
);
|
||||
const indexedEntities = {};
|
||||
for (const entity of visibleEntities) {
|
||||
indexedEntities[entity.instanceUuid] = entity;
|
||||
}
|
||||
// Reduce entities.
|
||||
diff.room.layers[index].entityList = this.entityReducer(
|
||||
diff.room.layers[index].entityList || {},
|
||||
indexedEntities,
|
||||
);
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
||||
|
||||
|
||||
get socket() {
|
||||
return this._socket;
|
||||
}
|
||||
|
@ -68,56 +96,24 @@ class InformedBase extends Trait {
|
|||
}
|
||||
|
||||
methods() {
|
||||
const entityReducer = this.createIndexedReducer();
|
||||
const layerReducer = this.createIndexedReducer();
|
||||
return {
|
||||
|
||||
inform: (diff) => {
|
||||
// Remove dead updates.
|
||||
if (0 === Object.keys(diff).length) {
|
||||
return;
|
||||
}
|
||||
inform: (stateSynchronizer) => {
|
||||
// Take diff.
|
||||
let diff = stateSynchronizer.diff(this._informState);
|
||||
diff = this.reduceStateDiff(diff);
|
||||
this._informState = stateSynchronizer.state;
|
||||
// Let the client know who they are.
|
||||
if (!this._sentSelf) {
|
||||
diff.selfEntity = this.entity.instanceUuid;
|
||||
this._sentSelf = true;
|
||||
}
|
||||
this._socket.send({
|
||||
type: 'state-update',
|
||||
payload: diff,
|
||||
});
|
||||
},
|
||||
|
||||
reduceStateDiff: (diff) => {
|
||||
diff.room = diff.room || {};
|
||||
diff.room.layers = diff.room.layers || {};
|
||||
// Index layers.
|
||||
const room = this.entity.room;
|
||||
const indexedLayers = {};
|
||||
for (const {index, layer} of room.layers) {
|
||||
indexedLayers[index] = layer;
|
||||
// Remove dead updates.
|
||||
if (0 === Object.keys(diff).length) {
|
||||
return;
|
||||
}
|
||||
// Reduce layers.
|
||||
diff.room.layers = layerReducer(
|
||||
diff.room.layers,
|
||||
indexedLayers,
|
||||
);
|
||||
for (const index in diff.room.layers) {
|
||||
// Index entities.
|
||||
const layer = room.layer(index);
|
||||
const visibleEntities = layer.visibleEntities(
|
||||
[0, 0, 640, 360]
|
||||
);
|
||||
const indexedEntities = {};
|
||||
for (const entity of visibleEntities) {
|
||||
indexedEntities[entity.instanceUuid] = entity;
|
||||
}
|
||||
// Reduce entities.
|
||||
diff.room.layers[index].entityList = entityReducer(
|
||||
diff.room.layers[index].entityList || {},
|
||||
indexedEntities,
|
||||
);
|
||||
}
|
||||
return diff;
|
||||
// Emit!
|
||||
this._socket.send({type: 'state-update', payload: diff});
|
||||
},
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user