avocado-old/packages/input/packet/input.packet.js
2019-04-24 18:01:17 -05:00

37 lines
717 B
JavaScript

import * as I from 'immutable';
import {Packet} from '@avocado/net';
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]);
}
});
}
}