vnx5

zM bot class

May 5th, 2019
8,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // zursor master fleet class
  2. // updated may 26, 2019
  3.  
  4. zM.dependenciesLoaded |= 0b000100
  5.  
  6. zM.Bot = class {
  7.     constructor (fleet) {
  8.         this.fleet = fleet;
  9.  
  10.         this.socket = new WebSocket(this.fleet.wsIp);
  11.         this.socket.binaryType = 'arraybuffer';
  12.  
  13.         this.opened = false;
  14.         this.isOpen = false;
  15.  
  16.         this.level = -1;
  17.         this.id = -1;
  18.  
  19.         this.obj = [];
  20.         this.players = [];
  21.         this.clicks = [];
  22.         this.drawings = [];
  23.  
  24.         this.realX = 400;
  25.         this.realY = 300;
  26.  
  27.         this.local = 0;
  28.  
  29.         this.packets = {
  30.             receivedTotal: 0,
  31.             sentTotal: 0,
  32.             receivedPS: 0,
  33.             sentPS: 0
  34.         }
  35.  
  36.         this.toHelp = [];
  37.         this.toDo = [];
  38.  
  39.         this.timestampStart = 0;
  40.  
  41.         this.helping = false;
  42.         this.deployed = false;
  43.  
  44.         this.prevLevels = [];
  45.  
  46.         this.socket.addEventListener('open', () => {
  47.             ++this.fleet.botsOpened;
  48.             this.opened = true;
  49.             this.isOpen = true;
  50.             ++this.fleet.botsPinged;
  51.             zM.updateTabs();
  52.         });
  53.  
  54.         this.socket.addEventListener('close', () => {
  55.             if (this.opened) {
  56.                 --this.fleet.botsOpened;
  57.                 var i = this.fleet.ids.indexOf(this.id);
  58.                 if (i != -1) this.fleet.ids.splice(i, 1);
  59.             }
  60.             else ++this.fleet.botsPinged;
  61.             this.isOpen = false;
  62.  
  63.             for (var i = 0; i < this.fleet.bots.length; ++i) if (this.fleet.bots[i] == this) { console.log('y'); this.fleet.bots.splice(i,1); break; }
  64.  
  65.             zM.updateTabs();
  66.         });
  67.  
  68.         this.socket.addEventListener('message', ev => {
  69.             var buf = ev.data,
  70.                 dat = new DataView(buf);
  71.  
  72.             ++this.packets.receivedTotal;
  73.             ++this.packets.receivedPS;
  74.             setTimeout(()=>{--this.packets.receivedPS},1000);
  75.  
  76.             var type = dat.getUint8(0);
  77.             if (type === 0) this.id = dat.getUint32(1, 1), ++this.fleet.botsIds, this.fleet.ids.push(this.id);
  78.             else if (type === 1) {
  79.                 var out = zM.parse.cursors(buf, 1);
  80.                 this.local = out.shift();
  81.                 var players = out.shift();
  82.                 var idsHere = [];
  83.                 // create players for all new ids
  84.                 for (var i = 0; i < players.length; ++i) {
  85.                     if (players[i].id === this.id) this.realX = players[i].x, this.realY = players[i].y;
  86.                     var index = -1;
  87.                     for (var j = 0; j < this.players.length; ++j) {
  88.                         if (this.players[j].id === players[i].id) {index = j; break;}
  89.                     }
  90.                     if (index === -1) this.players.push({
  91.                         x: players[i].x,
  92.                         y: players[i].y,
  93.                         lastUpdate: Date.now(),
  94.                         ox: players[i].x,
  95.                         oy: players[i].y,
  96.                         id: players[i].id
  97.                     });
  98.                 }
  99.                 for (var i = 0; i < players.length; ++i) {
  100.                     for (var j = 0; j < this.players.length; ++j) {
  101.                         if (this.players[j].id === players[i].id) {
  102.                             this.players[j].ox = zM.ease(this.players[j].x, this.players[j].ox, this.players[j].lastUpdate);
  103.                             this.players[j].oy = zM.ease(this.players[j].y, this.players[j].oy, this.players[j].lastUpdate);
  104.                             this.players[j].x = players[i].x;
  105.                             this.players[j].y = players[i].y;
  106.                             this.players[j].lastUpdate = Date.now();
  107.                             idsHere.push(players[i].id);
  108.                         }
  109.                     }
  110.                 }
  111.                 var nPl = [];
  112.                 for (var i = 0; i < idsHere.length; ++i) {
  113.                     for (var j = 0; j < this.players.length; ++j) {
  114.                         if (idsHere[i] === this.players[j].id) nPl.push(this.players[j]);
  115.                     }
  116.                 }
  117.  
  118.                 this.players = nPl;
  119.                 var off = out.shift();
  120.  
  121.                 out = zM.parse.clicks(buf, off);
  122.                 for (;0 < out[0].length;) {
  123.                     this.clicks.push(out[0].shift());
  124.                 }
  125.  
  126.                 off = out.pop();
  127.  
  128.                 out = zM.parse.remove(buf, off);
  129.                 for (var i = 0; i < out[0].length; ++i) {
  130.                     for (var j = 0; j < this.obj.length; ++j) {
  131.                         if (this.obj[j].id === out[0][i]) {this.obj.splice(j, 1); break;}
  132.                     }
  133.                 }
  134.  
  135.                 off = out.pop();
  136.  
  137.                 out = zM.parse.objects(buf, off);
  138.                 var obj = zM.parse.objData(out.shift());
  139.  
  140.                 for (var i = 0; i < obj.length; ++i) {
  141.                     var index = -1;
  142.                     for (var j = 0; j < this.obj.length; ++j) {
  143.                         if (this.obj[j].id === obj[i].id) {index = j; break;};
  144.                     }
  145.                     if (index === -1) this.obj.push(obj[i]);
  146.                     else this.obj[j] = obj[i];
  147.                 }
  148.  
  149.                 off = out.pop();
  150.  
  151.                 out = zM.parse.drawing(buf, off);
  152.  
  153.                 for (var i = 0; i < out[0].length; ++i) {
  154.                     var todo = out[0][i];
  155.                     this.drawings.push(todo);
  156.                 }
  157.  
  158.                 this.online = dat.getUint32(dat.byteLength-4, true);
  159.  
  160.             } else if (type === 4) {
  161.                 if (this.level === -1) ++this.fleet.botsLevels;
  162.  
  163.                 this.drawings = [];
  164.  
  165.                 this.realX = dat.getUint16(1, true);
  166.                 this.realY = dat.getUint16(3, true);
  167.  
  168.                 var objdata = zM.parse.objects(buf, 5);
  169.                 this.obj = zM.parse.objData(objdata[0]);
  170.  
  171.                 var grid = 100;
  172.                 for (var i = 0; i < this.obj.length; ++i) {
  173.                     if (grid <= 1) {grid = 1; break;}
  174.                     if (this.obj[i].type === 1) if (
  175.                         (this.obj[i].x/grid|0) != (this.obj[i].x/grid) ||
  176.                         (this.obj[i].y/grid|0) != (this.obj[i].y/grid) ||
  177.                         (this.obj[i].w/grid|0) != (this.obj[i].w/grid) ||
  178.                         (this.obj[i].h/grid|0) != (this.obj[i].h/grid)
  179.                     ) --grid, i = -1;
  180.                 }
  181.  
  182.                 this.grid = grid;
  183.  
  184.                 var compare = [];
  185.                 for (var i = 0; i < this.obj.length; ++i) {
  186.                     var o = this.obj[i];
  187.                     if (o.type === 0) {
  188.                         compare.push({
  189.                             x: o.x,
  190.                             y: o.y,
  191.                             size: o.size,
  192.                             content: o.content
  193.                         });
  194.                     } else if (o.type === 1) {
  195.                         if (o.color === '#000000') compare.push({
  196.                             x: o.x,
  197.                             y: o.y,
  198.                             w: o.w,
  199.                             h: o.h
  200.                         });
  201.                     } else if (o.type === 2) {
  202.                         compare.push({
  203.                             x: o.x,
  204.                             y: o.y,
  205.                             w: o.w,
  206.                             h: o.h,
  207.                             isBad: o.isBad
  208.                         });
  209.                     } else {
  210.                         compare.push({
  211.                             x: o.x,
  212.                             y: o.y,
  213.                             w: o.w,
  214.                             h: o.h,
  215.                             color: o.color
  216.                         });
  217.                     }
  218.                 }
  219.                 compare = JSON.stringify(compare);
  220.                 var i = this.prevLevels.indexOf(compare);
  221.                 if (i != -1) this.level = i;
  222.                 else ++this.level, this.prevLevels.push(compare);
  223.  
  224.                 this.fleet.updateTabs();
  225.             } else if (type === 5) {
  226.                 this.realX = dat.getUint16(1, true);
  227.                 this.realY = dat.getUint16(3, true);
  228.             }
  229.         });
  230.     }
  231. }
Add Comment
Please, Sign In to add comment