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;
var msg;
try { try {
players = JSON.parse(json); msg = JSON.parse(json);
} }
catch (err) { catch (err) {
return; return;
} }
if (players.players) { if (msg.players) {
me.options.layer.clearLayers(); me.options.layer.clearLayers();
me.options.layer.addData(players.players); me.options.layer.addData(msg.players);
} }
}(evt.data);
var invalidate = function(json) { var tilesData = msg.tiles;
var invalidateAll = function(x, y, z) { return true; }; if (!tilesData) {
return;
}
if (!(typeof json === "string")) { console.log("tiles found")
return invalidateAll;
} var invalidate = function(td) {
var tiles;
var tileData;
try {
var tileData = JSON.parse(json);
} catch (err) {
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]+).*/;