Simplified auto update code. Removed pointless tile updating when there are only player updates. Removed redundant JSON parsing.

This commit is contained in:
Sascha L. Teichmann 2015-03-13 15:43:59 +01:00
parent 71636a5b4c
commit c3b634669a

View File

@ -74,48 +74,39 @@ L.Control.AutoUpdate = L.Control.extend({
this.socket.onmessage = function(evt) { this.socket.onmessage = function(evt) {
var updatePlayers = function(json) { var json = evt.data;
if (!(typeof json === "string")) { if (!(typeof json === "string")) {
return; return;
} }
var players;
try {
players = JSON.parse(json);
}
catch (err) {
return;
}
if (players.players) { var msg;
me.options.layer.clearLayers(); try {
me.options.layer.addData(players.players); msg = JSON.parse(json);
} }
}(evt.data); catch (err) {
return;
}
var invalidate = function(json) { if (msg.players) {
var invalidateAll = function(x, y, z) { return true; }; me.options.layer.clearLayers();
me.options.layer.addData(msg.players);
}
if (!(typeof json === "string")) { var tilesData = msg.tiles;
return invalidateAll; if (!tilesData) {
} return;
var tiles; }
var tileData;
try { console.log("tiles found")
var tileData = JSON.parse(json);
} catch (err) { var invalidate = function(td) {
return invalidateAll;
}
if (!tileData.tiles) {
return invalidateAll;
}
tiles = tileData.tiles;
var pyramid = new Array(9); var pyramid = new Array(9);
var last = new Object(); var last = new Object();
pyramid[8] = last; pyramid[8] = last;
for (var i = 0; i < tiles.length; i++) { for (var i = 0; i < td.length; i++) {
var xz = tiles[i]; var xz = td[i];
last[xz.X + "#" + xz.Z] = xz; last[xz.X + "#" + xz.Z] = xz;
} }
for (var p = 7; p >= 0; p--) { for (var p = 7; p >= 0; p--) {
@ -140,7 +131,8 @@ L.Control.AutoUpdate = L.Control.extend({
var k = x + "#" + z; var k = x + "#" + z;
return level.hasOwnProperty(k); return level.hasOwnProperty(k);
}; };
} (evt.data); } (tilesData);
var tiles = document.getElementsByTagName('img'); var tiles = document.getElementsByTagName('img');
var re = /\/map\/([0-9]+)\/([0-9]+)\/([0-9]+).*/; var re = /\/map\/([0-9]+)\/([0-9]+)\/([0-9]+).*/;