Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ButtonClick {
- constructor({ machineId, buttonId, screenId, data }) {
- this.machineId = machineId;
- this.buttonId = buttonId;
- this.screenId = screenId;
- this.data = data;
- this.data = this.data || {};
- this.fieldId = 0xffffffff;
- for (let i = 0; i < 8; i++) {
- if (!(i in this.data)) {
- this.data[i] = '';
- }
- if (typeof this.data[i] !== 'string') {
- this.data[i] = `${this.data[i]}`;
- }
- }
- }
- encode(encoding) {
- const varLenList = (() => {
- const result = [];
- for (let k in this.data) {
- result.push(this.data[k]);
- }
- return result;
- })().map(v => v.length + 1);
- const varsLen = varLenList.reduce((prev, curr) => prev + curr, 0);
- const length = 25 + varsLen;
- const buffer = new Buffer(length);
- const writer = new BufferWriter(buffer, 0, encoding);
- writer.writeUInt32LE(length | messageTypes.button);
- writer.writeUInt32LE(this.machineId);
- writer.writeUInt32LE(this.buttonId);
- writer.writeUInt32LE(this.fieldId);
- writer.writeUInt32LE(this.screenId);
- writer.writeUInt8(0);
- // Your Tom is so smart he counts 2, 3, 4, 1, 5, 6, 7, 8
- const positions = [2, 3, 4, 1, 5, 6, 7, 8];
- for (var i of positions) {
- writer.writeString(this.data[i - 1]);
- }
- for (i = 0; i <= 2; i++) {
- writer.writeUInt8(0);
- }
- return buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement