Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ButtonClick {
  2.     constructor({ machineId, buttonId, screenId, data }) {
  3.         this.machineId = machineId;
  4.         this.buttonId = buttonId;
  5.         this.screenId = screenId;
  6.         this.data = data;
  7.         this.data = this.data || {};
  8.         this.fieldId = 0xffffffff;
  9.         for (let i = 0; i < 8; i++) {
  10.             if (!(i in this.data)) {
  11.                 this.data[i] = '';
  12.             }
  13.             if (typeof this.data[i] !== 'string') {
  14.                 this.data[i] = `${this.data[i]}`;
  15.             }
  16.         }
  17.     }
  18.  
  19.     encode(encoding) {
  20.         const varLenList = (() => {
  21.             const result = [];
  22.             for (let k in this.data) {
  23.                 result.push(this.data[k]);
  24.             }
  25.             return result;
  26.         })().map(v => v.length + 1);
  27.         const varsLen = varLenList.reduce((prev, curr) => prev + curr, 0);
  28.         const length = 25 + varsLen;
  29.         const buffer = new Buffer(length);
  30.         const writer = new BufferWriter(buffer, 0, encoding);
  31.         writer.writeUInt32LE(length | messageTypes.button);
  32.         writer.writeUInt32LE(this.machineId);
  33.         writer.writeUInt32LE(this.buttonId);
  34.         writer.writeUInt32LE(this.fieldId);
  35.         writer.writeUInt32LE(this.screenId);
  36.         writer.writeUInt8(0);
  37.  
  38.         // Your Tom is so smart he counts 2, 3, 4, 1, 5, 6, 7, 8
  39.         const positions = [2, 3, 4, 1, 5, 6, 7, 8];
  40.         for (var i of positions) {
  41.             writer.writeString(this.data[i - 1]);
  42.         }
  43.  
  44.         for (i = 0; i <= 2; i++) {
  45.             writer.writeUInt8(0);
  46.         }
  47.         return buffer;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement