')
.css({
top: 50,
left: 10,
width: 200,
color: '#FFF',
fontSize: 20,
position: 'fixed',
fontWeight: 600,
background: 'rgba(128, 128, 128, 0.58)',
textAlign: 'center',
padding: 10
})
.attr('id', 'mini-map-party')
.appendTo(window.document.body)
.append(
$('
').css({
margin: 0,
padding: 0
}).text('Party')
);
var mini_map_party_list = $('')
.attr('id', 'mini-map-party-list')
.css({
listStyle: 'none',
padding: 0,
margin: 0
})
.appendTo(mini_map_party);
mini_map_party.on('update-list', function(e) {
mini_map_party_list.empty();
for (var p in players) {
var player = players[p];
var name = String.fromCharCode.apply(null, player.name);
name = (name == '' ? 'anonymous' : name);
$('- ')
.text(player.no + 1 + '. ' + name)
.appendTo(mini_map_party_list);
}
});
mini_map_party.hide();
}
}
// cell constructor
function Cell(id, x, y, size, color, name) {
cells[id] = this;
this.id = id;
this.ox = this.x = x;
this.oy = this.y = y;
this.oSize = this.size = size;
this.color = color;
this.points = [];
this.pointsAcc = [];
this.setName(name);
}
Cell.prototype = {
id: 0,
points: null,
pointsAcc: null,
name: null,
nameCache: null,
sizeCache: null,
x: 0,
y: 0,
size: 0,
ox: 0,
oy: 0,
oSize: 0,
nx: 0,
ny: 0,
nSize: 0,
updateTime: 0,
updateCode: 0,
drawTime: 0,
destroyed: false,
isVirus: false,
isAgitated: false,
wasSimpleDrawing: true,
destroy: function() {
delete cells[this.id];
id = current_cell_ids.indexOf(this.id);
-1 != id && current_cell_ids.splice(id, 1);
this.destroyed = true;
if (map_server === null || map_server.readyState !== window._WebSocket.OPEN) {
miniMapUnregisterToken(this.id);
}
},
setName: function(name) {
this.name = name;
},
updatePos: function() {
if (map_server === null || map_server.readyState !== window._WebSocket.OPEN) {
if (options.enableMultiCells || -1 != current_cell_ids.indexOf(this.id)) {
if (! miniMapIsRegisteredToken(this.id))
{
miniMapRegisterToken(
this.id,
miniMapCreateToken(this.id, this.color)
);
}
var size_n = this.nSize/length_x;
miniMapUpdateToken(this.id, (this.nx - start_x)/length_x, (this.ny - start_y)/length_y, size_n);
}
}
if (options.enablePosition && -1 != current_cell_ids.indexOf(this.id)) {
window.mini_map_pos.show();
miniMapUpdatePos(this.nx, this.ny);
} else {
window.mini_map_pos.hide();
}
}
};
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
function camel2cap(str) {
return str.replace(/([A-Z])/g, function(s){return ' ' + s.toLowerCase();}).capitalize();
};
// create a linked property from slave object
// whenever master[prop] update, slave[prop] update
function refer(master, slave, prop) {
Object.defineProperty(master, prop, {
get: function(){
return slave[prop];
},
set: function(val) {
slave[prop] = val;
},
enumerable: true,
configurable: true
});
};
// extract a websocket packet which contains the information of cells
function extractCellPacket(data, offset) {
////
var dataToSend = {
destroyQueue : [],
nodes : [],
nonVisibleNodes : []
};
////
var I = +new Date;
var qa = false;
var b = Math.random(), c = offset;
var size = data.getUint16(c, true);
c = c + 2;
function getName() {
for (var string = ""; ; ) {
var ch = data.getUint16(c, true);
c += 2;
if (0 == ch)
break;
string += String.fromCharCode(ch)
}
return string;
}
function getSkin() {
for (var string = ""; ; ) {
var ch = data.getUint8(c++);
if (0 == ch)
break;
string += String.fromCharCode(ch)
}
return string;
}
// Nodes to be destroyed (killed)
for (var e = 0; e < size; ++e) {
var p = cells[data.getUint32(c, true)],
f = cells[data.getUint32(c + 4, true)],
c = c + 8;
p && f && (
f.destroy(),
f.ox = f.x,
f.oy = f.y,
f.oSize = f.size,
f.nx = p.x,
f.ny = p.y,
f.nSize = f.size,
f.updateTime = I,
dataToSend.destroyQueue.push(f.id));
}
// Nodes to be updated
for (e = 0; ; ) {
var d = data.getUint32(c, true);
c += 4;
if (0 == d) {
break;
}
++e;
var p = data.getInt32(c, true),
c = c + 4,
f = data.getInt32(c, true),
c = c + 4;
g = data.getInt16(c, true);
c = c + 2;
for (var h = data.getUint8(c++), m = data.getUint8(c++), q = data.getUint8(c++), h = (h << 16 | m << 8 | q).toString(16); 6 > h.length; )
h = "0" + h;
var h = "#" + h,
k = data.getUint8(c++),
m = !!(k & 1),
q = !!(k & 16);
var skin = null;
var name = null;
k & 2 && (c += 4 + data.getUint32(c, true));
k & 4 && (skin = getSkin());
//k & 8 && (c += 16);
name = getName();
k = null;
var updated = false;
// if d in cells then modify it, otherwise create a new cell
cells.hasOwnProperty(d)
? (k = cells[d],
k.updatePos(),
k.ox = k.x,
k.oy = k.y,
k.oSize = k.size,
k.color = h,
updated = true)
: (k = new Cell(d, p, f, g, h, name),
k.pX = p,
k.pY = f);
k.isVirus = m;
k.isAgitated = q;
k.nx = p;
k.ny = f;
k.nSize = g;
k.updateCode = b;
k.updateTime = I;
skin && (k.skin = skin);
name && k.setName(name);
// ignore food creation
if (updated) {
dataToSend.nodes.push({
id: k.id,
x: k.nx,
y: k.ny,
size: k.nSize,
color: k.color
});
}
}
// Destroy queue + nonvisible nodes
b = data.getUint32(c, true);
c += 4;
for (e = 0; e < b; e++) {
d = data.getUint32(c, true);
c += 4, k = cells[d];
null != k && k.destroy();
dataToSend.nonVisibleNodes.push(d);
}
var packet = {
type: 16,
data: dataToSend
}
miniMapSendRawData(msgpack.pack(packet));
}
// extract the type of packet and dispatch it to a corresponding extractor
function extractPacket(event) {
var c = 0;
var data = new DataView(event.data);
240 == data.getUint8(c) && (c += 5);
var opcode = data.getUint8(c);
c++;
switch (opcode) {
case 16: // cells data
extractCellPacket(data, c);
break;
case 20: // cleanup ids
current_cell_ids = [];
break;
case 32: // cell id belongs me
var id = data.getUint32(c, true);
if (current_cell_ids.indexOf(id) === -1)
current_cell_ids.push(id);
miniMapSendRawData(msgpack.pack({
type: 32,
data: id
}));
break;
case 64: // get borders
start_x = data.getFloat64(c, !0), c += 8,
start_y = data.getFloat64(c, !0), c += 8,
end_x = data.getFloat64(c, !0), c += 8,
end_y = data.getFloat64(c, !0), c += 8,
center_x = (start_x + end_x) / 2,
center_y = (start_y + end_y) / 2,
length_x = Math.abs(start_x - end_x),
length_y = Math.abs(start_y - end_y);
}
};
function extractSendPacket(data) {
var view = new DataView(data);
switch (view.getUint8(0, true)) {
case 0:
player_name = [];
for (var i=1; i < data.byteLength; i+=2) {
player_name.push(view.getUint16(i, true));
}
miniMapSendRawData(msgpack.pack({
type: 0,
data: player_name
}));
break;
}
}
// the injected point, overwriting the WebSocket constructor
window.WebSocket = function(url, protocols) {
console.log('Listen');
if (protocols === undefined) {
protocols = [];
}
var ws = new _WebSocket(url, protocols);
refer(this, ws, 'binaryType');
refer(this, ws, 'bufferedAmount');
refer(this, ws, 'extensions');
refer(this, ws, 'protocol');
refer(this, ws, 'readyState');
refer(this, ws, 'url');
this.send = function(data){
extractSendPacket(data);
return ws.send.call(ws, data);
};
this.close = function(){
return ws.close.call(ws);
};
this.onopen = function(event){};
this.onclose = function(event){};
this.onerror = function(event){};
this.onmessage = function(event){};
ws.onopen = function(event) {
miniMapInit();
var real_url = null;
if (url.split("://")[0] == "wss") {
real_url = agar_server;
} else {
real_url = url;
}
agar_server = real_url;
miniMapSendRawData(msgpack.pack({
type: 100,
data: {url: real_url, region: $('#region').val(), gamemode: $('#gamemode').val(), party: location.hash}
}));
if (this.onopen)
return this.onopen.call(ws, event);
}.bind(this);
ws.onmessage = function(event) {
extractPacket(event);
if (this.onmessage)
return this.onmessage.call(ws, event);
}.bind(this);
ws.onclose = function(event) {
if (this.onclose)
return this.onclose.call(ws, event);
}.bind(this);
ws.onerror = function(event) {
if (this.onerror)
return this.onerror.call(ws, event);
}.bind(this);
};
window.WebSocket.prototype = _WebSocket;
$(window.document).ready(function() {
miniMapInit();
});
$(window).load(function() {
var main_canvas = document.getElementById('canvas');
if (main_canvas && main_canvas.onmousemove) {
document.onmousemove = main_canvas.onmousemove;
main_canvas.onmousemove = null;
}
});
})();