refactor: informed logic elegance

This commit is contained in:
cha0s 2019-04-05 13:47:49 -04:00
parent cbccd7b1ae
commit 6b221837a7

View File

@ -70,26 +70,25 @@ export class Informed extends decorate(Trait) {
}
reduceStateDiff(diff) {
if (!diff || !diff.room || !diff.room.layers) {
return diff
}
const room = this.entity.room;
const camera = this.entity.camera;
for (const index in diff.room.layers) {
// Index entities.
const layer = room.layer(index);
const visibleEntities = layer.visibleEntities(
camera.rectangle
);
const indexedEntities = {};
for (const entity of visibleEntities) {
indexedEntities[entity.instanceUuid] = entity;
if (diff && diff.room && diff.room.layers) {
const room = this.entity.room;
const camera = this.entity.camera;
for (const index in diff.room.layers) {
// Index entities.
const layer = room.layer(index);
const visibleEntities = layer.visibleEntities(
camera.rectangle
);
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,
);
}
// Reduce entities.
diff.room.layers[index].entityList = this.entityReducer(
diff.room.layers[index].entityList || {},
indexedEntities,
);
}
return this.collapseStateDiff(diff);
}
@ -109,20 +108,17 @@ export class Informed extends decorate(Trait) {
inform: (stateSynchronizer) => {
// Take diff.
let diff = stateSynchronizer.diff(this._informState);
diff = this.reduceStateDiff(diff);
diff = diff ? diff : {};
this._informState = stateSynchronizer.state;
// Let the client know who they are.
if (!this._sentSelf) {
diff.selfEntity = this.entity.instanceUuid;
this._sentSelf = true;
}
// Remove dead updates.
if (0 === Object.keys(diff).length) {
return;
}
diff = this.reduceStateDiff(diff);
// Emit!
this._socket.send({type: 'state-update', payload: diff});
if (diff) {
this._socket.send({type: 'state-update', payload: diff});
}
},
};