Added client-side events to map.

This commit is contained in:
FrozenCow 2011-03-21 19:07:30 +01:00
parent 777b55f27b
commit 15a8e03e9e

View File

@ -291,13 +291,24 @@ DynMap.prototype = {
selectMap: function(map, completed) { selectMap: function(map, completed) {
if (!map) { throw "Cannot select map " + map; } if (!map) { throw "Cannot select map " + map; }
var me = this; var me = this;
if (me.maptype === map) {
return;
}
var worldChanged = me.world !== map.world;
me.map.setMapTypeId('none'); me.map.setMapTypeId('none');
me.world = map.world; me.world = map.world;
me.maptype = map; me.maptype = map;
me.maptype.updateTileSize(me.map.zoom); me.maptype.updateTileSize(me.map.zoom);
window.setTimeout(function() { window.setTimeout(function() {
me.map.setMapTypeId(map.world.name + '.' + map.name); me.map.setMapTypeId(map.world.name + '.' + map.name);
if (completed) { completed(); } if (completed) {
if (worldChanged) {
$(me).trigger('worldchanged');
}
$(me).trigger('mapchanged');
completed();
}
}, 1); }, 1);
$('.map', me.worldlist).removeClass('selected'); $('.map', me.worldlist).removeClass('selected');
$(map.element).addClass('selected'); $(map.element).addClass('selected');
@ -323,16 +334,18 @@ DynMap.prototype = {
// TODO: is there a better place for this? // TODO: is there a better place for this?
this.cleanPopups(); this.cleanPopups();
$(me).trigger('worldupdating');
$.getJSON(me.options.updateUrl + "world/" + me.world.name + "/" + me.lasttimestamp, function(update) { $.getJSON(me.options.updateUrl + "world/" + me.world.name + "/" + me.lasttimestamp, function(update) {
me.alertbox.hide(); me.alertbox.hide();
if (!me.options.jsonfile) if (!me.options.jsonfile) {
me.lasttimestamp = update.timestamp; me.lasttimestamp = update.timestamp;
}
me.clock.setTime(update.servertime); me.clock.setTime(update.servertime);
me.clockdigital.setTime(update.servertime); me.clockdigital.setTime(update.servertime);
var newplayers = {}; var newplayers = {};
$.each(update.players, function(index, playerUpdate) { $.each(update.players, function(index, playerUpdate) {
var name = playerUpdate.name; var name = playerUpdate.name;
@ -355,6 +368,8 @@ DynMap.prototype = {
$.each(update.updates, function(index, update) { $.each(update.updates, function(index, update) {
// Only handle updates that are actually new. // Only handle updates that are actually new.
if(!me.options.jsonfile || me.lasttimestamp <= update.timestamp) { if(!me.options.jsonfile || me.lasttimestamp <= update.timestamp) {
$(me).trigger('worldupdate', [ update ]);
swtch(update.type, { swtch(update.type, {
tile: function() { tile: function() {
me.onTileUpdated(update.name); me.onTileUpdated(update.name);
@ -374,12 +389,17 @@ DynMap.prototype = {
//var divs = $('div[rel]'); //var divs = $('div[rel]');
//divs.filter(function(i){return parseInt(divs[i].attr('rel')) > timestamp+me.options.messagettl;}).remove(); //divs.filter(function(i){return parseInt(divs[i].attr('rel')) > timestamp+me.options.messagettl;}).remove();
}); });
$(me).trigger('worldupdated', [ update ]);
me.lasttimestamp = update.timestamp; me.lasttimestamp = update.timestamp;
setTimeout(function() { me.update(); }, me.options.updaterate); setTimeout(function() { me.update(); }, me.options.updaterate);
}, function(status, statusText, request) { }, function(status, statusText, request) {
me.alertbox me.alertbox
.text('Could not update map: ' + (statusText || 'Could not connect to server')) .text('Could not update map: ' + (statusText || 'Could not connect to server'))
.show(); .show();
$(me).trigger('worldupdatefailed');
setTimeout(function() { me.update(); }, me.options.updaterate); setTimeout(function() { me.update(); }, me.options.updaterate);
} }
); );
@ -517,6 +537,9 @@ DynMap.prototype = {
name: update.name, name: update.name,
location: new Location(me.worlds[update.world], parseFloat(update.x), parseFloat(update.y), parseFloat(update.z)) location: new Location(me.worlds[update.world], parseFloat(update.x), parseFloat(update.y), parseFloat(update.z))
}; };
$(me).trigger('playeradded', [ player ]);
var location = player.location; var location = player.location;
// Create the player-marker. // Create the player-marker.
var markerPosition = me.map.getProjection().fromWorldToLatLng(location.x, location.y, location.z); var markerPosition = me.map.getProjection().fromWorldToLatLng(location.x, location.y, location.z);
@ -579,6 +602,8 @@ DynMap.prototype = {
var me = this; var me = this;
var location = player.location = new Location(me.worlds[update.world], parseFloat(update.x), parseFloat(update.y), parseFloat(update.z)); var location = player.location = new Location(me.worlds[update.world], parseFloat(update.x), parseFloat(update.y), parseFloat(update.z));
$(me).trigger('playerupdated', [ player ]);
// Update the marker. // Update the marker.
var markerPosition = me.map.getProjection().fromWorldToLatLng(location.x, location.y, location.z); var markerPosition = me.map.getProjection().fromWorldToLatLng(location.x, location.y, location.z);
player.marker.toggle(me.world === location.world); player.marker.toggle(me.world === location.world);
@ -597,6 +622,8 @@ DynMap.prototype = {
delete me.players[player.name]; delete me.players[player.name];
$(me).trigger('playerremoved', [ player ]);
// Remove the marker. // Remove the marker.
player.marker.remove(); player.marker.remove();