refactor: packet restructuring

This commit is contained in:
cha0s 2019-04-15 10:03:14 -05:00
parent 506bfec19b
commit 4e0f21d8ff
6 changed files with 16 additions and 75 deletions

View File

@ -5,18 +5,20 @@ import ReactDOM from 'react-dom';
// 2nd party.
import {create as createClient} from '@avocado/client/socket';
import {EntityList} from '@avocado/entity';
import {ActionRegistry} from '@avocado/input';
import {ActionRegistry, InputPacket} from '@avocado/input';
import {Stage} from '@avocado/graphics';
import {Vector} from '@avocado/math';
import {SocketIoParser} from '@avocado/packet';
import {Synchronizer, Unpacker} from '@avocado/state';
import {
StateKeysPacket,
StatePacket,
Synchronizer,
Unpacker,
} from '@avocado/state';
import {Room, RoomView} from '@avocado/topdown';
import {World} from '@avocado/physics/matter/world';
import {clearAnimation, setAnimation, Ticker} from '@avocado/timing';
// 1st party.
import {InputPacket} from '../common/packet/input.packet';
import {KeysPacket} from '../common/packet/keys.packet';
import {StatePacket} from '../common/packet/state.packet';
import {augmentParserWithThroughput} from '../common/parser-throughput';
import {WorldTime} from '../common/world-time';
import {App} from './app';
@ -224,7 +226,7 @@ const predictionHandle = setInterval(() => {
// State updates.
const unpacker = new Unpacker();
function onPacket(packet) {
if (packet instanceof KeysPacket) {
if (packet instanceof StateKeysPacket) {
unpacker.registerKeys(packet.data);
}
if (packet instanceof StatePacket) {

View File

@ -1,36 +0,0 @@
import * as I from 'immutable';
import {Packet} from '@avocado/packet';
export class InputPacket extends Packet {
static get schema() {
return {
...super.schema,
data: [
['string'],
['varint'],
],
};
}
static fromState(state) {
const data = [[], []];
const [actions, values] = data;
for (const [action, value] of state.entries()) {
actions.push(action);
values.push(parseInt(value));
}
return new InputPacket(data);
}
toState() {
const [actions, values] = this.data;
return I.Map().withMutations((state) => {
for (let i = 0; i < actions.length; ++i) {
state.set(actions[i], values[i]);
}
});
}
}

View File

@ -1,15 +0,0 @@
import {Packet} from '@avocado/packet';
export class KeysPacket extends Packet {
static get schema() {
return {
...super.schema,
data: [
['string'],
['varuint'],
],
};
}
}

View File

@ -1,12 +0,0 @@
import {Packet} from '@avocado/packet';
export class StatePacket extends Packet {
static get schema() {
return {
...super.schema,
data: 'buffer',
};
}
}

View File

@ -3,10 +3,10 @@ import msgpack from 'msgpack-lite';
import {performance} from 'perf_hooks';
// 3rd party.
// 2nd party.
import {InputPacket} from '@avocado/input';
import {Synchronizer} from '@avocado/state';
import {Ticker} from '@avocado/timing';
// 1st party.
import {InputPacket} from '../common/packet/input.packet';
import {WorldTime} from '../common/world-time';
import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';

View File

@ -5,10 +5,12 @@ import isPlainObject from 'is-plain-object';
import {compose} from '@avocado/core';
import {Trait} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math';
import {Packer, Synchronizer} from '@avocado/state';
import {KeysPacket} from '../../common/packet/keys.packet';
import {StatePacket} from '../../common/packet/state.packet';
import {
Packer,
StateKeysPacket,
StatePacket,
Synchronizer,
} from '@avocado/state';
const decorate = compose(
);
@ -161,7 +163,7 @@ export class Informed extends decorate(Trait) {
// Emit!
const keys = this._packer.computeNewKeys(steps);
if (0 !== keys[0].length) {
this._socket.send(new KeysPacket(keys));
this._socket.send(new StateKeysPacket(keys));
}
const packed = this._packer.pack(steps);
this._socket.send(new StatePacket(packed));