Added players layer and update data in manual and auto mode.

This commit is contained in:
Raimund Renkert
2015-03-09 17:55:19 +01:00
parent b3ba074e28
commit 3c63fbf185
2 changed files with 59 additions and 12 deletions

View File

@ -1,7 +1,8 @@
L.Control.AutoUpdate = L.Control.extend({
options: {
position: 'topleft',
label: 'Automatic update'
label: 'Automatic update',
layer: undefined
},
pressed: true,
@ -37,27 +38,46 @@ L.Control.AutoUpdate = L.Control.extend({
},
autoUpdate: function() {
var me = this;
this.socket = new WebSocket('ws://' + window.location.host + '/ws');
this.socket.onmessage = function(evt) {
var updatePlayers = function(json) {
if (!(typeof json === "string")) {
return;
}
var players;
try {
players = JSON.parse(json);
}
catch (err) {
return;
}
if (players.players) {
me.options.layer.clearLayers();
me.options.layer.addData(players.players);
}
}(evt.data);
var invalidate = function(json) {
var invalidateAll = function(x, y, z) { return true; };
if (!(typeof json === "string")) {
return invalidateAll;
}
var msg;
var tiles;
var tileData;
try {
msg = JSON.parse(json);
var tileData = JSON.parse(json);
} catch (err) {
return invalidateAll;
}
var tiles = msg.tiles;
if !tiles {
continue;
if (!tileData.tiles) {
return invalidateAll;
}
tiles = tileData.tiles;
var pyramid = new Array(9);
var last = new Object();
@ -119,7 +139,7 @@ L.Control.AutoUpdate = L.Control.extend({
}
});
L.autoUpdate = function(cbLabel, cbFunc, cbMap) {
L.autoUpdate = function(cbLabel, cbFunc, layer, cbMap) {
var control = new L.Control.AutoUpdate();
if (cbLabel) {
control.options.label = cbLabel;
@ -129,6 +149,10 @@ L.autoUpdate = function(cbLabel, cbFunc, cbMap) {
control.intendedFunction = cbFunc;
}
if (layer) {
control.options.layer = layer;
}
if (cbMap === '') {
return control;
}