runBots = function(ip, token) { if (jQuery('#token').val().length > 8) { alert('You must to create a Party first or connect to a private server.'); return false; } else if (jQuery('#token').val().length > 0) { mAB = 4; } else { mAB = 10; } bots = []; currentServer = ip; for (i = 0; i < mAB; i++) { bots.push(new WebSocket(ip)); bots[i].binaryType = "arraybuffer"; bots[i].bot = i; bots[i].id = null; bots[i].onopen = function() { a = new DataView(new ArrayBuffer(5)); a.setUint8(0, 254); a.setUint32(1, 5, !0); this.send(a.buffer); a = new DataView(new ArrayBuffer(5)); a.setUint8(0, 255); a.setUint32(1, 154669603, !0); this.send(a.buffer); a = new DataView(new ArrayBuffer(1 + token.length)); a.setUint8(0, 80); for (var c = 0; c < token.length; ++c) a.setUint8(c + 1, token.charCodeAt(c)); this.send(a.buffer); console.log("Bot " + this.bot + " connected."); }; bots[i].onmessage = function() { if (playing()) { this.setNick(jQuery("#botsNick").val()); xx = jQuery("[name='botsFollow']:checked").val() == "mouse" ? ra : localStorage["x"]; // ra: target x yy = jQuery("[name='botsFollow']:checked").val() == "mouse" ? sa : localStorage["y"]; // sa: target y this.setDirection(xx, yy); } }; bots[i].onclose = function() { bots.splice(this.bot, 1); console.log("Bot " + this.bot + " closed."); }; bots[i].onerror = function() { bots.splice(this.bot, 1); console.log("Bot " + this.bot + " got an error."); }; bots[i].setNick = function(nick) { if (typeof bots[this.bot] == "undefined") { return false; } var a = new DataView(new ArrayBuffer(1 + 2 * nick.length)); a.setUint8(0, 0); for (var b = 0; b < nick.length; ++b) a.setUint16(1 + 2 * b, nick.charCodeAt(b), !0); bots[this.bot].send(a.buffer); }; bots[i].setDirection = function(x, y) { if (typeof bots[this.bot] == "undefined") { return false; } a = new DataView(new ArrayBuffer(13)); a.setUint8(0, 16); a.setInt32(1, x, !0); a.setInt32(5, y, !0); a.setUint32(9, 0, !0); bots[this.bot].send(a.buffer); } } }; stopBots = function() { if (typeof bots != "undefined") for (i = 0; i < bots.length; i++) { bots[i].close(); } bots = []; };