Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zursor master fleet class
- // updated may 26, 2019
- zM.dependenciesLoaded |= 0b000100
- zM.Bot = class {
- constructor (fleet) {
- this.fleet = fleet;
- this.socket = new WebSocket(this.fleet.wsIp);
- this.socket.binaryType = 'arraybuffer';
- this.opened = false;
- this.isOpen = false;
- this.level = -1;
- this.id = -1;
- this.obj = [];
- this.players = [];
- this.clicks = [];
- this.drawings = [];
- this.realX = 400;
- this.realY = 300;
- this.local = 0;
- this.packets = {
- receivedTotal: 0,
- sentTotal: 0,
- receivedPS: 0,
- sentPS: 0
- }
- this.toHelp = [];
- this.toDo = [];
- this.timestampStart = 0;
- this.helping = false;
- this.deployed = false;
- this.prevLevels = [];
- this.socket.addEventListener('open', () => {
- ++this.fleet.botsOpened;
- this.opened = true;
- this.isOpen = true;
- ++this.fleet.botsPinged;
- zM.updateTabs();
- });
- this.socket.addEventListener('close', () => {
- if (this.opened) {
- --this.fleet.botsOpened;
- var i = this.fleet.ids.indexOf(this.id);
- if (i != -1) this.fleet.ids.splice(i, 1);
- }
- else ++this.fleet.botsPinged;
- this.isOpen = false;
- 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; }
- zM.updateTabs();
- });
- this.socket.addEventListener('message', ev => {
- var buf = ev.data,
- dat = new DataView(buf);
- ++this.packets.receivedTotal;
- ++this.packets.receivedPS;
- setTimeout(()=>{--this.packets.receivedPS},1000);
- var type = dat.getUint8(0);
- if (type === 0) this.id = dat.getUint32(1, 1), ++this.fleet.botsIds, this.fleet.ids.push(this.id);
- else if (type === 1) {
- var out = zM.parse.cursors(buf, 1);
- this.local = out.shift();
- var players = out.shift();
- var idsHere = [];
- // create players for all new ids
- for (var i = 0; i < players.length; ++i) {
- if (players[i].id === this.id) this.realX = players[i].x, this.realY = players[i].y;
- var index = -1;
- for (var j = 0; j < this.players.length; ++j) {
- if (this.players[j].id === players[i].id) {index = j; break;}
- }
- if (index === -1) this.players.push({
- x: players[i].x,
- y: players[i].y,
- lastUpdate: Date.now(),
- ox: players[i].x,
- oy: players[i].y,
- id: players[i].id
- });
- }
- for (var i = 0; i < players.length; ++i) {
- for (var j = 0; j < this.players.length; ++j) {
- if (this.players[j].id === players[i].id) {
- this.players[j].ox = zM.ease(this.players[j].x, this.players[j].ox, this.players[j].lastUpdate);
- this.players[j].oy = zM.ease(this.players[j].y, this.players[j].oy, this.players[j].lastUpdate);
- this.players[j].x = players[i].x;
- this.players[j].y = players[i].y;
- this.players[j].lastUpdate = Date.now();
- idsHere.push(players[i].id);
- }
- }
- }
- var nPl = [];
- for (var i = 0; i < idsHere.length; ++i) {
- for (var j = 0; j < this.players.length; ++j) {
- if (idsHere[i] === this.players[j].id) nPl.push(this.players[j]);
- }
- }
- this.players = nPl;
- var off = out.shift();
- out = zM.parse.clicks(buf, off);
- for (;0 < out[0].length;) {
- this.clicks.push(out[0].shift());
- }
- off = out.pop();
- out = zM.parse.remove(buf, off);
- for (var i = 0; i < out[0].length; ++i) {
- for (var j = 0; j < this.obj.length; ++j) {
- if (this.obj[j].id === out[0][i]) {this.obj.splice(j, 1); break;}
- }
- }
- off = out.pop();
- out = zM.parse.objects(buf, off);
- var obj = zM.parse.objData(out.shift());
- for (var i = 0; i < obj.length; ++i) {
- var index = -1;
- for (var j = 0; j < this.obj.length; ++j) {
- if (this.obj[j].id === obj[i].id) {index = j; break;};
- }
- if (index === -1) this.obj.push(obj[i]);
- else this.obj[j] = obj[i];
- }
- off = out.pop();
- out = zM.parse.drawing(buf, off);
- for (var i = 0; i < out[0].length; ++i) {
- var todo = out[0][i];
- this.drawings.push(todo);
- }
- this.online = dat.getUint32(dat.byteLength-4, true);
- } else if (type === 4) {
- if (this.level === -1) ++this.fleet.botsLevels;
- this.drawings = [];
- this.realX = dat.getUint16(1, true);
- this.realY = dat.getUint16(3, true);
- var objdata = zM.parse.objects(buf, 5);
- this.obj = zM.parse.objData(objdata[0]);
- var grid = 100;
- for (var i = 0; i < this.obj.length; ++i) {
- if (grid <= 1) {grid = 1; break;}
- if (this.obj[i].type === 1) if (
- (this.obj[i].x/grid|0) != (this.obj[i].x/grid) ||
- (this.obj[i].y/grid|0) != (this.obj[i].y/grid) ||
- (this.obj[i].w/grid|0) != (this.obj[i].w/grid) ||
- (this.obj[i].h/grid|0) != (this.obj[i].h/grid)
- ) --grid, i = -1;
- }
- this.grid = grid;
- var compare = [];
- for (var i = 0; i < this.obj.length; ++i) {
- var o = this.obj[i];
- if (o.type === 0) {
- compare.push({
- x: o.x,
- y: o.y,
- size: o.size,
- content: o.content
- });
- } else if (o.type === 1) {
- if (o.color === '#000000') compare.push({
- x: o.x,
- y: o.y,
- w: o.w,
- h: o.h
- });
- } else if (o.type === 2) {
- compare.push({
- x: o.x,
- y: o.y,
- w: o.w,
- h: o.h,
- isBad: o.isBad
- });
- } else {
- compare.push({
- x: o.x,
- y: o.y,
- w: o.w,
- h: o.h,
- color: o.color
- });
- }
- }
- compare = JSON.stringify(compare);
- var i = this.prevLevels.indexOf(compare);
- if (i != -1) this.level = i;
- else ++this.level, this.prevLevels.push(compare);
- this.fleet.updateTabs();
- } else if (type === 5) {
- this.realX = dat.getUint16(1, true);
- this.realY = dat.getUint16(3, true);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment