2011-02-17 20:08:50 +01:00
"use strict" ;
2011-01-15 02:11:31 +01:00
//if (!console) console = { log: function() {} };
2011-01-10 23:58:39 +01:00
2011-03-21 20:46:53 +01:00
var componentconstructors = { } ;
2011-02-06 15:09:15 +01:00
var maptypes = { } ;
2011-08-16 02:08:18 +02:00
var map = null ; // Leaflet assumes top-level 'map'...
2011-02-06 15:09:15 +01:00
2011-03-21 20:46:53 +01:00
componentconstructors [ 'testcomponent' ] = function ( dynmap , configuration ) {
2011-03-31 15:52:35 +02:00
console . log ( 'initialize' ) ;
$ ( dynmap ) . bind ( 'worldchanged' , function ( ) { console . log ( 'worldchanged' ) ; } ) ;
2011-06-03 07:39:21 +02:00
$ ( dynmap ) . bind ( 'mapchanging' , function ( ) { console . log ( 'mapchanging' ) ; } ) ;
2011-03-31 15:52:35 +02:00
$ ( dynmap ) . bind ( 'mapchanged' , function ( ) { console . log ( 'mapchanged' ) ; } ) ;
$ ( dynmap ) . bind ( 'zoomchanged' , function ( ) { console . log ( 'zoomchanged' ) ; } ) ;
$ ( dynmap ) . bind ( 'worldupdating' , function ( ) { console . log ( 'worldupdating' ) ; } ) ;
$ ( dynmap ) . bind ( 'worldupdate' , function ( ) { console . log ( 'worldupdate' ) ; } ) ;
$ ( dynmap ) . bind ( 'worldupdated' , function ( ) { console . log ( 'worldupdated' ) ; } ) ;
$ ( dynmap ) . bind ( 'worldupdatefailed' , function ( ) { console . log ( 'worldupdatefailed' ) ; } ) ;
$ ( dynmap ) . bind ( 'playeradded' , function ( ) { console . log ( 'playeradded' ) ; } ) ;
$ ( dynmap ) . bind ( 'playerremoved' , function ( ) { console . log ( 'playerremoved' ) ; } ) ;
$ ( dynmap ) . bind ( 'playerupdated' , function ( ) { console . log ( 'playerupdated' ) ; } ) ;
2011-03-21 20:46:53 +01:00
} ;
2011-01-14 20:08:23 +01:00
function DynMap ( options ) {
2011-02-01 15:16:54 +01:00
var me = this ;
me . options = options ;
2011-05-26 13:54:08 +02:00
$ . getJSON ( me . options . url . configuration , function ( configuration ) {
2011-02-01 15:16:54 +01:00
me . configure ( configuration ) ;
me . initialize ( ) ;
2011-02-26 20:27:54 +01:00
} , function ( status , statusMessage ) {
alert ( 'Could not retrieve configuration: ' + statusMessage ) ;
2011-02-22 15:38:49 +01:00
} ) ;
2010-12-31 13:33:51 +01:00
}
2011-01-14 20:08:23 +01:00
DynMap . prototype = {
2011-03-21 20:46:53 +01:00
components : [ ] ,
2011-02-17 20:08:50 +01:00
worlds : { } ,
2011-02-22 15:38:49 +01:00
registeredTiles : [ ] ,
2011-02-22 14:49:03 +01:00
players : { } ,
2011-06-03 15:42:24 +02:00
lasttimestamp : new Date ( ) . getUTCMilliseconds ( ) , /* Pseudorandom - prevent cached '?0' */
2011-05-30 00:24:46 +02:00
servertime : 0 ,
serverday : false ,
2011-06-02 20:14:38 +02:00
inittime : new Date ( ) . getTime ( ) ,
2011-01-14 20:08:23 +01:00
followingPlayer : '' ,
2011-07-26 23:24:04 +02:00
missedupdates : 0 ,
2011-08-26 00:07:00 +02:00
layercontrol : undefined ,
2011-05-26 13:54:08 +02:00
formatUrl : function ( name , options ) {
var url = this . options . url [ name ] ;
$ . each ( options , function ( n , v ) {
url = url . replace ( "{" + n + "}" , v ) ;
} ) ;
return url ;
} ,
2011-02-01 14:17:08 +01:00
configure : function ( configuration ) {
2011-02-01 15:16:54 +01:00
var me = this ;
$ . extend ( me . options , configuration ) ;
2011-02-17 20:08:50 +01:00
$ . each ( me . options . worlds , function ( index , worldentry ) {
var world = me . worlds [ worldentry . name ] = $ . extend ( { } , worldentry , {
maps : { }
} ) ;
$ . each ( worldentry . maps , function ( index , mapentry ) {
var map = $ . extend ( { } , mapentry , {
world : world ,
dynmap : me
} ) ;
map = world . maps [ mapentry . name ] = maptypes [ mapentry . type ] ( map ) ;
2011-07-26 04:16:42 +02:00
if ( me . options . defaultmap && me . options . defaultmap == mapentry . name )
world . defaultmap = map ;
2011-02-17 20:08:50 +01:00
world . defaultmap = world . defaultmap || map ;
} ) ;
me . defaultworld = me . defaultworld || world ;
2011-02-01 14:17:08 +01:00
} ) ;
2011-06-27 05:05:43 +02:00
var urlarg = me . getParameterByName ( 'worldname' ) ;
2011-11-01 13:51:10 +01:00
if ( urlarg == "" )
urlarg = me . options . defaultworld || "" ;
2011-06-27 05:05:43 +02:00
if ( urlarg != "" ) {
me . defaultworld = me . worlds [ urlarg ] || me . defaultworld ;
}
urlarg = me . getParameterByName ( 'mapname' ) ;
if ( urlarg != "" ) {
me . defaultworld . defaultmap = me . defaultworld . maps [ urlarg ] || me . defaultworld . defaultmap ;
}
2011-06-28 03:38:26 +02:00
urlarg = me . getIntParameterByName ( 'x' ) ;
if ( urlarg != null )
2011-06-27 05:05:43 +02:00
me . defaultworld . center . x = urlarg ;
2011-06-28 03:38:26 +02:00
urlarg = me . getIntParameterByName ( 'y' ) ;
if ( urlarg != null )
2011-06-27 05:05:43 +02:00
me . defaultworld . center . y = urlarg ;
2011-06-28 03:38:26 +02:00
urlarg = me . getIntParameterByName ( 'z' ) ;
if ( urlarg != null )
2011-06-27 05:05:43 +02:00
me . defaultworld . center . z = urlarg ;
2011-02-01 14:17:08 +01:00
} ,
2011-01-14 20:08:23 +01:00
initialize : function ( ) {
var me = this ;
2010-12-31 13:33:51 +01:00
2011-01-14 20:08:23 +01:00
var container = $ ( me . options . container ) ;
2011-02-17 20:08:50 +01:00
container . addClass ( 'dynmap' ) ;
2011-01-14 20:08:23 +01:00
var mapContainer ;
( mapContainer = $ ( '<div/>' ) )
. addClass ( 'map' )
. appendTo ( container ) ;
2011-06-27 05:05:43 +02:00
2011-06-28 03:38:26 +02:00
var urlzoom = me . getIntParameterByName ( 'zoom' ) ;
if ( urlzoom != null )
me . options . defaultzoom = urlzoom ;
2011-08-03 15:13:59 +02:00
if ( typeof me . options . defaultzoom == 'undefined' )
me . options . defaultzoom = 1 ;
2011-01-14 20:08:23 +01:00
2011-07-16 23:53:15 +02:00
var map = this . map = new L . Map ( mapContainer . get ( 0 ) , {
2011-08-03 15:13:59 +02:00
zoom : me . options . defaultzoom ,
2011-07-16 23:53:15 +02:00
center : new L . LatLng ( 0 , 0 ) ,
zoomAnimation : true ,
2011-08-04 04:33:53 +02:00
attributionControl : false ,
2011-07-16 23:53:15 +02:00
crs : L . Util . extend ( { } , L . CRS , {
code : 'simple' ,
projection : {
project : function ( latlng ) {
return new L . Point ( latlng . lat , latlng . lng ) ;
} ,
unproject : function ( point , unbounded ) {
return new L . LatLng ( point . x , point . y , true ) ;
}
} ,
transformation : new L . Transformation ( 1 , 0 , 1 , 0 )
} ) ,
scale : function ( zoom ) {
return ( 1 << zoom ) ;
}
2011-01-14 20:08:23 +01:00
} ) ;
2011-08-16 02:08:18 +02:00
window . map = map ; // Placate Leaflet need for top-level 'map'....
2011-02-17 20:08:50 +01:00
map . zoom _changed = function ( ) {
me . maptype . updateTileSize ( me . map . zoom ) ;
2011-03-21 20:46:53 +01:00
$ ( me ) . trigger ( 'zoomchanged' ) ;
2011-02-17 20:08:50 +01:00
} ;
2011-08-02 01:31:04 +02:00
2011-07-16 23:53:15 +02:00
/ * g o o g l e . m a p s . e v e n t . a d d L i s t e n e r ( m a p , ' d r a g s t a r t ' , f u n c t i o n ( m E v e n t ) {
2011-02-22 17:58:39 +01:00
me . followPlayer ( null ) ;
2011-07-16 23:53:15 +02:00
} ) ; * /
2011-08-26 00:07:00 +02:00
2011-02-17 20:08:50 +01:00
// Sidebar
2011-06-02 20:14:38 +02:00
var panel ;
var sidebar ;
var pinbutton ;
2011-10-02 04:32:57 +02:00
var nopanel = ( me . getParameterByName ( 'nopanel' ) == 'true' ) ;
2011-08-30 04:00:05 +02:00
if ( me . options . sidebaropened != 'true' ) { // false or pinned
var pincls = 'pinned'
if ( me . options . sidebaropened == 'false' )
pincls = '' ;
2011-06-02 20:14:38 +02:00
sidebar = me . sidebar = $ ( '<div/>' )
2011-10-02 04:32:57 +02:00
. addClass ( 'sidebar ' + pincls ) ;
2011-06-02 20:14:38 +02:00
panel = $ ( '<div/>' )
. addClass ( 'panel' )
. appendTo ( sidebar ) ;
2011-02-13 01:19:47 +01:00
2011-06-02 20:14:38 +02:00
// Pin button.
pinbutton = $ ( '<div/>' )
. addClass ( 'pin' )
. click ( function ( ) {
sidebar . toggleClass ( 'pinned' ) ;
} )
. appendTo ( panel ) ;
}
else {
sidebar = me . sidebar = $ ( '<div/>' )
2011-10-02 04:32:57 +02:00
. addClass ( 'sidebar pinned' ) ;
2011-06-02 20:14:38 +02:00
panel = $ ( '<div/>' )
. addClass ( 'panel' )
. appendTo ( sidebar ) ;
2011-05-30 16:56:10 +02:00
}
2011-10-02 04:32:57 +02:00
if ( ! nopanel )
sidebar . appendTo ( container ) ;
2011-11-06 03:29:33 +01:00
// World scrollbuttons
var upbtn _world = $ ( '<div/>' )
. addClass ( 'scrollup' )
. bind ( 'mousedown mouseup' , function ( event ) {
if ( event . type == 'mousedown' ) {
worldlist . animate ( { "scrollTop" : "-=300px" } , 3000 , 'linear' ) ;
} else {
worldlist . stop ( ) ;
}
} ) ;
var downbtn _world = $ ( '<div/>' )
. addClass ( 'scrolldown' )
. bind ( 'mousedown mouseup' , function ( event ) {
if ( event . type == 'mousedown' ) {
worldlist . animate ( { "scrollTop" : "+=300px" } , 3000 , 'linear' ) ;
} else {
worldlist . stop ( ) ;
}
} ) ;
2011-05-30 16:56:10 +02:00
2011-02-17 20:08:50 +01:00
// Worlds
var worldlist ;
$ ( '<fieldset/>' )
. append ( $ ( '<legend/>' ) . text ( 'Map Types' ) )
2011-11-06 03:29:33 +01:00
. append ( upbtn _world )
. append ( me . worldlist = worldlist = $ ( '<ul/>' ) . addClass ( 'worldlist' )
. bind ( 'mousewheel' , function ( event , delta ) {
this . scrollTop -= ( delta * 10 ) ;
event . preventDefault ( ) ;
} )
)
. append ( downbtn _world )
2011-02-17 20:08:50 +01:00
. appendTo ( panel ) ;
$ . each ( me . worlds , function ( index , world ) {
var maplist ;
world . element = $ ( '<li/>' )
. addClass ( 'world' )
. text ( world . title )
. append ( maplist = $ ( '<ul/>' )
. addClass ( 'maplist' )
2011-02-13 01:19:47 +01:00
)
2011-02-17 20:08:50 +01:00
. data ( 'world' , world )
2011-02-13 01:19:47 +01:00
. appendTo ( worldlist ) ;
2011-01-12 01:45:56 +01:00
2011-07-28 04:27:35 +02:00
$ . each ( world . maps , function ( mapindex , map ) {
2011-07-16 23:53:15 +02:00
//me.map.mapTypes.set(map.world.name + '.' + map.name, map);
2011-02-17 20:08:50 +01:00
map . element = $ ( '<li/>' )
. addClass ( 'map' )
. append ( $ ( '<a/>' )
2011-07-28 04:27:35 +02:00
. attr ( { title : map . options . title , href : '#' } )
2011-02-17 20:08:50 +01:00
. addClass ( 'maptype' )
2011-07-28 04:27:35 +02:00
. css ( { backgroundImage : 'url(' + ( map . options . icon || ( 'images/block_' + mapindex + '.png' ) ) + ')' } )
. text ( map . options . title )
2011-02-17 20:08:50 +01:00
)
. click ( function ( ) {
me . selectMap ( map ) ;
2011-01-12 01:45:56 +01:00
} )
2011-02-17 20:08:50 +01:00
. data ( 'map' , map )
. appendTo ( maplist ) ;
} ) ;
2011-01-14 20:08:23 +01:00
} ) ;
2011-02-19 20:51:21 +01:00
2011-03-17 15:43:28 +01:00
// The scrollbuttons
// we need to show/hide them depending: if (me.playerlist.scrollHeight() > me.playerlist.innerHeight()) or something.
var upbtn = $ ( '<div/>' )
. addClass ( 'scrollup' )
. bind ( 'mousedown mouseup' , function ( event ) {
if ( event . type == 'mousedown' ) {
playerlist . animate ( { "scrollTop" : "-=300px" } , 3000 , 'linear' ) ;
} else {
playerlist . stop ( ) ;
}
} ) ;
var downbtn = $ ( '<div/>' )
. addClass ( 'scrolldown' )
. bind ( 'mousedown mouseup' , function ( event ) {
if ( event . type == 'mousedown' ) {
playerlist . animate ( { "scrollTop" : "+=300px" } , 3000 , 'linear' ) ;
} else {
playerlist . stop ( ) ;
}
} ) ;
2011-01-14 20:08:23 +01:00
// The Player List
2011-02-17 20:08:50 +01:00
var playerlist ;
$ ( '<fieldset/>' )
. append ( $ ( '<legend/>' ) . text ( 'Players' ) )
2011-03-17 15:43:28 +01:00
. append ( upbtn )
. append ( me . playerlist = playerlist = $ ( '<ul/>' ) . addClass ( 'playerlist' )
. bind ( 'mousewheel' , function ( event , delta ) {
this . scrollTop -= ( delta * 10 ) ;
event . preventDefault ( ) ;
} )
)
. append ( downbtn )
2011-02-17 20:08:50 +01:00
. appendTo ( panel ) ;
2011-01-14 20:08:23 +01:00
2011-03-17 21:10:35 +01:00
var updateHeight = function ( ) {
2011-11-06 03:29:33 +01:00
if ( sidebar . innerHeight ( ) > ( 2 * worldlist . scrollHeight ( ) ) ) { /* Big enough */
worldlist . height ( worldlist . scrollHeight ( ) ) ;
upbtn _world . toggle ( false ) ;
downbtn _world . toggle ( false ) ;
}
else {
worldlist . height ( sidebar . innerHeight ( ) / 2 ) ;
upbtn _world . toggle ( true ) ;
downbtn _world . toggle ( true ) ;
}
2011-03-17 15:43:28 +01:00
playerlist . height ( sidebar . innerHeight ( ) - ( playerlist . offset ( ) . top - worldlist . offset ( ) . top ) - 64 ) ; // here we need a fix to avoid the static value, but it works fine this way :P
2011-08-16 18:59:08 +02:00
var scrollable = playerlist . scrollHeight ( ) > playerlist . height ( ) ;
2011-03-17 21:10:35 +01:00
upbtn . toggle ( scrollable ) ;
downbtn . toggle ( scrollable ) ;
} ;
updateHeight ( ) ;
$ ( window ) . resize ( updateHeight ) ;
2011-07-13 23:05:08 +02:00
$ ( dynmap ) . bind ( 'playeradded' , function ( ) {
updateHeight ( ) ;
} ) ;
$ ( dynmap ) . bind ( 'playerremoved' , function ( ) {
updateHeight ( ) ;
} ) ;
2011-02-13 07:25:28 +01:00
// The Compass
2011-02-20 01:58:53 +01:00
var compass = $ ( '<div/>' )
. addClass ( 'compass' )
2011-02-22 15:38:49 +01:00
. appendTo ( container ) ;
2011-02-13 07:25:28 +01:00
2011-01-14 20:08:23 +01:00
// TODO: Enable hash-links.
/ *
var link ;
var linkbox = me . linkbox = $ ( '<div/>' )
. addClass ( 'linkbox' )
. append ( link = $ ( '<input type="text" />' ) )
. data ( 'link' , link )
. appendTo ( container ) ; * /
2011-08-30 04:00:05 +02:00
if ( me . options . sidebaropened != 'true' ) {
2011-05-30 16:56:10 +02:00
$ ( '<div/>' )
. addClass ( 'hitbar' )
. appendTo ( panel ) ;
}
2011-01-14 20:08:23 +01:00
var alertbox = me . alertbox = $ ( '<div/>' )
. addClass ( 'alertbox' )
2011-02-26 21:33:02 +01:00
. hide ( )
2011-01-14 20:08:23 +01:00
. appendTo ( container ) ;
2011-09-08 03:44:21 +02:00
if ( dynmapversion != me . options . dynmapversion ) {
me . alertbox
2011-09-08 04:07:30 +02:00
. text ( 'Web files are not matched with plugin version: All files need to be same version (' + me . options . dynmapversion + ')' )
2011-09-08 03:44:21 +02:00
. show ( ) ;
2011-09-08 04:07:30 +02:00
return ;
2011-09-08 03:44:21 +02:00
}
2011-01-14 20:08:23 +01:00
2011-02-17 20:08:50 +01:00
me . selectMap ( me . defaultworld . defaultmap ) ;
2011-08-26 08:22:03 +02:00
var componentstoload = 0 ;
2011-08-26 07:55:53 +02:00
var configset = { } ;
2011-03-21 20:46:53 +01:00
$ . each ( me . options . components , function ( index , configuration ) {
2011-08-26 08:22:03 +02:00
if ( ! configset [ configuration . type ] ) {
2011-08-26 07:55:53 +02:00
configset [ configuration . type ] = [ ] ;
2011-08-26 08:22:03 +02:00
componentstoload ++ ;
}
2011-08-26 07:55:53 +02:00
configset [ configuration . type ] . push ( configuration ) ;
} ) ;
2011-09-08 04:44:11 +02:00
var tobeloaded = { } ;
2011-08-26 07:55:53 +02:00
$ . each ( configset , function ( type , configlist ) {
2011-09-08 04:44:11 +02:00
tobeloaded [ type ] = true ;
2011-08-26 07:55:53 +02:00
loadjs ( 'js/' + type + '.js' , function ( ) {
var componentconstructor = componentconstructors [ type ] ;
2011-04-07 15:46:50 +02:00
if ( componentconstructor ) {
2011-08-26 07:55:53 +02:00
$ . each ( configlist , function ( idx , configuration ) {
me . components . push ( new componentconstructor ( me , configuration ) ) ;
} ) ;
2011-04-07 15:46:50 +02:00
} else {
// Could not load component. We'll ignore this for the moment.
}
2011-09-08 04:44:11 +02:00
delete tobeloaded [ type ] ;
2011-03-31 13:06:34 +02:00
componentstoload -- ;
if ( componentstoload == 0 ) {
// Actually start updating once all components are loaded.
setTimeout ( function ( ) { me . update ( ) ; } , me . options . updaterate ) ;
}
} ) ;
2011-03-21 20:46:53 +01:00
} ) ;
2011-09-08 04:44:11 +02:00
setTimeout ( function ( ) {
$ . each ( configset , function ( type , configlist ) {
if ( tobeloaded [ type ] ) {
me . alertbox
. text ( 'Error loading js/' + type + '.js' )
. show ( ) ;
}
} ) ;
if ( componentstoload > 0 )
setTimeout ( function ( ) { me . update ( ) ; } , me . options . updaterate ) ;
} , 15000 ) ;
2011-01-14 20:08:23 +01:00
} ,
2011-07-16 23:53:15 +02:00
getProjection : function ( ) { return this . maptype . getProjection ( ) ; } ,
2011-08-04 04:14:15 +02:00
selectMapAndPan : function ( map , location , completed ) {
2011-02-18 00:07:59 +01:00
if ( ! map ) { throw "Cannot select map " + map ; }
2011-02-17 20:08:50 +01:00
var me = this ;
2011-03-21 19:07:30 +01:00
if ( me . maptype === map ) {
return ;
}
2011-06-03 07:39:21 +02:00
$ ( me ) . trigger ( 'mapchanging' ) ;
2011-07-16 23:53:15 +02:00
var mapWorld = map . options . world ;
2011-04-09 08:24:52 +02:00
if ( me . maptype ) {
2011-07-28 07:43:13 +02:00
$ ( '.compass' ) . removeClass ( 'compass_' + me . maptype . options . compassview ) ;
$ ( '.compass' ) . removeClass ( 'compass_' + me . maptype . options . name ) ;
2011-04-09 08:24:52 +02:00
}
2011-07-28 07:43:13 +02:00
$ ( '.compass' ) . addClass ( 'compass_' + map . options . compassview ) ;
$ ( '.compass' ) . addClass ( 'compass_' + map . options . name ) ;
2011-07-28 04:27:35 +02:00
var worldChanged = me . world !== map . options . world ;
2011-07-16 23:53:15 +02:00
var projectionChanged = ( me . maptype && me . maptype . getProjection ( ) ) !== ( map && map . projection ) ;
2011-07-28 04:27:35 +02:00
var prevzoom = me . map . getZoom ( ) ;
2011-11-06 04:03:45 +01:00
var prevworld = me . world ;
if ( worldChanged && me . world ) {
me . world . lastcenter = me . maptype . getProjection ( ) . fromLatLngToLocation ( me . map . getCenter ( ) , 64 ) ;
}
2011-07-16 23:53:15 +02:00
if ( me . maptype ) {
2011-08-26 07:55:53 +02:00
me . map . removeLayer ( me . maptype ) ;
2011-07-16 23:53:15 +02:00
}
2011-07-28 04:27:35 +02:00
2011-08-03 17:21:29 +02:00
var prevmap = me . maptype ;
2011-07-16 23:53:15 +02:00
me . world = mapWorld ;
2011-02-17 20:08:50 +01:00
me . maptype = map ;
2011-07-28 04:27:35 +02:00
2011-07-29 06:51:53 +02:00
if ( me . maptype . options . maxZoom < prevzoom )
prevzoom = me . maptype . options . maxZoom ;
me . map . options . maxZoom = me . maptype . options . maxZoom ;
me . map . options . minZoom = me . maptype . options . minZoom ;
2011-07-28 04:27:35 +02:00
2011-08-04 04:14:15 +02:00
if ( projectionChanged || worldChanged || location ) {
2011-07-28 04:27:35 +02:00
var centerPoint ;
2011-08-04 04:14:15 +02:00
if ( location ) {
centerPoint = me . getProjection ( ) . fromLocationToLatLng ( location ) ;
}
else if ( worldChanged ) {
2011-11-06 04:03:45 +01:00
var centerLocation ;
if ( mapWorld . lastcenter )
centerLocation = mapWorld . lastcenter ;
else
centerLocation = $ . extend ( { x : 0 , y : 64 , z : 0 } , mapWorld . center ) ;
2011-07-28 04:27:35 +02:00
centerPoint = me . getProjection ( ) . fromLocationToLatLng ( centerLocation ) ;
}
else {
2011-08-03 17:21:29 +02:00
var prevloc = null ;
if ( prevmap != null )
prevloc = prevmap . getProjection ( ) . fromLatLngToLocation ( me . map . getCenter ( ) , 64 ) ;
if ( prevloc != null )
centerPoint = me . getProjection ( ) . fromLocationToLatLng ( prevloc ) ;
else
centerPoint = me . map . getCenter ( ) ;
2011-07-28 04:27:35 +02:00
}
2011-07-29 06:51:53 +02:00
me . map . setView ( centerPoint , prevzoom , true ) ;
}
else {
me . map . setZoom ( prevzoom ) ;
2011-07-16 23:53:15 +02:00
}
2011-08-03 15:13:59 +02:00
me . map . addLayer ( me . maptype ) ;
2011-07-28 04:27:35 +02:00
2011-07-21 23:59:14 +02:00
if ( worldChanged ) {
$ ( me ) . trigger ( 'worldchanged' ) ;
}
2011-07-16 23:53:15 +02:00
$ ( me ) . trigger ( 'mapchanged' ) ;
2011-02-17 20:08:50 +01:00
$ ( '.map' , me . worldlist ) . removeClass ( 'selected' ) ;
$ ( map . element ) . addClass ( 'selected' ) ;
2011-06-03 10:24:21 +02:00
me . updateBackground ( ) ;
2011-07-16 23:53:15 +02:00
if ( completed ) {
completed ( ) ;
}
2011-02-17 20:08:50 +01:00
} ,
2011-08-04 04:14:15 +02:00
selectMap : function ( map , completed ) {
this . selectMapAndPan ( map , null , completed ) ;
} ,
selectWorldAndPan : function ( world , location , completed ) {
2011-02-22 14:49:03 +01:00
var me = this ;
2011-02-22 15:38:49 +01:00
if ( typeof ( world ) === 'String' ) { world = me . worlds [ world ] ; }
if ( me . world === world ) {
2011-08-04 04:14:15 +02:00
if ( location ) {
var latlng = me . maptype . getProjection ( ) . fromLocationToLatLng ( location ) ;
me . panToLatLng ( latlng , completed ) ;
}
else {
if ( completed ) { completed ( ) ; }
}
2011-02-22 14:49:03 +01:00
return ;
}
2011-08-04 04:14:15 +02:00
me . selectMapAndPan ( world . defaultmap , location , completed ) ;
} ,
selectWorld : function ( world , completed ) {
this . selectWorldAndPan ( world , null , completed ) ;
2011-02-22 14:49:03 +01:00
} ,
2011-07-16 23:53:15 +02:00
panToLocation : function ( location , completed ) {
2011-02-22 14:49:03 +01:00
var me = this ;
2011-07-16 23:53:15 +02:00
if ( location . world ) {
2011-08-04 04:14:15 +02:00
me . selectWorldAndPan ( location . world , location , function ( ) {
if ( completed ) completed ( ) ;
2011-07-16 23:53:15 +02:00
} ) ;
} else {
2011-08-04 04:14:15 +02:00
var latlng = me . maptype . getProjection ( ) . fromLocationToLatLng ( location ) ;
me . panToLatLng ( latlng , completed ) ;
2011-07-16 23:53:15 +02:00
}
} ,
panToLayerPoint : function ( point , completed ) {
var me = this ;
var latlng = me . map . layerPointToLatLng ( point ) ;
me . map . panToLatLng ( latlng ) ;
if ( completed ) {
completed ( ) ;
}
} ,
panToLatLng : function ( latlng , completed ) {
this . map . panTo ( latlng ) ;
if ( completed ) {
completed ( ) ;
}
2011-02-22 14:49:03 +01:00
} ,
2011-01-14 20:08:23 +01:00
update : function ( ) {
var me = this ;
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'worldupdating' ) ;
2011-05-26 13:54:08 +02:00
$ . getJSON ( me . formatUrl ( 'update' , { world : me . world . name , timestamp : me . lasttimestamp } ) , function ( update ) {
2011-03-29 01:16:52 +02:00
if ( ! update ) {
setTimeout ( function ( ) { me . update ( ) ; } , me . options . updaterate ) ;
return ;
}
2011-01-14 20:08:23 +01:00
me . alertbox . hide ( ) ;
2011-03-21 19:07:30 +01:00
if ( ! me . options . jsonfile ) {
2011-02-22 17:58:39 +01:00
me . lasttimestamp = update . timestamp ;
2011-03-21 19:07:30 +01:00
}
2011-05-30 16:56:10 +02:00
2011-06-02 20:14:38 +02:00
me . servertime = update . servertime ;
2011-07-24 14:56:23 +02:00
var newserverday = ( me . servertime > 23100 || me . servertime < 12900 ) ;
if ( me . serverday != newserverday ) {
me . serverday = newserverday ;
me . updateBackground ( ) ;
if ( me . maptype . options . nightandday ) {
// Readd map.
me . map . removeLayer ( me . maptype ) ;
me . map . addLayer ( me . maptype ) ;
}
}
2011-05-30 00:24:46 +02:00
2011-02-22 14:49:03 +01:00
var newplayers = { } ;
$ . each ( update . players , function ( index , playerUpdate ) {
var name = playerUpdate . name ;
var player = me . players [ name ] ;
if ( player ) {
me . updatePlayer ( player , playerUpdate ) ;
} else {
me . addPlayer ( playerUpdate ) ;
}
newplayers [ name ] = player ;
2011-02-06 03:00:51 +01:00
} ) ;
2011-02-22 15:38:49 +01:00
var name ;
for ( name in me . players ) {
2011-02-22 14:49:03 +01:00
var player = me . players [ name ] ;
if ( ! ( name in newplayers ) ) {
me . removePlayer ( player ) ;
}
}
2011-02-06 03:00:51 +01:00
$ . each ( update . updates , function ( index , update ) {
2011-03-07 22:07:34 +01:00
// Only handle updates that are actually new.
if ( ! me . options . jsonfile || me . lasttimestamp <= update . timestamp ) {
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'worldupdate' , [ update ] ) ;
2011-03-07 22:07:34 +01:00
swtch ( update . type , {
tile : function ( ) {
2011-05-15 06:25:59 +02:00
me . onTileUpdated ( update . name , update . timestamp ) ;
2011-04-05 22:58:46 +02:00
} ,
playerjoin : function ( ) {
$ ( me ) . trigger ( 'playerjoin' , [ update . playerName ] ) ;
} ,
playerquit : function ( ) {
$ ( me ) . trigger ( 'playerquit' , [ update . playerName ] ) ;
2011-09-03 23:49:18 +02:00
} ,
component : function ( ) {
$ ( me ) . trigger ( 'component.' + update . ctype , [ update ] ) ;
2011-03-07 22:07:34 +01:00
}
} ) ;
}
2011-02-20 01:58:53 +01:00
/* remove older messages from chat*/
//var timestamp = event.timeStamp;
//var divs = $('div[rel]');
//divs.filter(function(i){return parseInt(divs[i].attr('rel')) > timestamp+me.options.messagettl;}).remove();
2011-02-06 03:00:51 +01:00
} ) ;
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'worldupdated' , [ update ] ) ;
2011-03-10 07:11:43 +01:00
me . lasttimestamp = update . timestamp ;
2011-07-26 23:24:04 +02:00
me . missedupdates = 0 ;
2011-02-01 15:16:54 +01:00
setTimeout ( function ( ) { me . update ( ) ; } , me . options . updaterate ) ;
2011-02-28 19:23:25 +01:00
} , function ( status , statusText , request ) {
2011-07-26 23:24:04 +02:00
me . missedupdates ++ ;
if ( me . missedupdates > 2 ) {
me . alertbox
. text ( 'Could not update map: ' + ( statusText || 'Could not connect to server' ) )
. show ( ) ;
$ ( me ) . trigger ( 'worldupdatefailed' ) ;
}
2011-02-01 15:16:54 +01:00
setTimeout ( function ( ) { me . update ( ) ; } , me . options . updaterate ) ;
2011-01-14 20:08:23 +01:00
}
2011-02-06 03:00:51 +01:00
) ;
2011-01-14 20:08:23 +01:00
} ,
2011-02-17 20:08:50 +01:00
getTileUrl : function ( tileName , always ) {
var me = this ;
var tile = me . registeredTiles [ tileName ] ;
if ( tile ) {
return me . options . tileUrl + me . world . name + '/' + tileName + '?' + tile . lastseen ;
} else {
2011-06-02 20:14:38 +02:00
return me . options . tileUrl + me . world . name + '/' + tileName + '?' + me . inittime ; /* Browser cache fix on reload */
2011-02-17 20:08:50 +01:00
}
} ,
registerTile : function ( mapType , tileName , tile ) {
this . registeredTiles [ tileName ] = {
tileElement : tile ,
mapType : mapType ,
lastseen : '0'
} ;
} ,
unregisterTile : function ( mapType , tileName ) {
delete this . registeredTiles [ tileName ] ;
} ,
2011-05-15 06:25:59 +02:00
onTileUpdated : function ( tileName , timestamp ) {
2011-01-14 20:08:23 +01:00
var me = this ;
var tile = this . registeredTiles [ tileName ] ;
if ( tile ) {
2011-05-15 06:25:59 +02:00
tile . lastseen = timestamp ;
2011-01-14 20:08:23 +01:00
tile . mapType . onTileUpdated ( tile . tileElement , tileName ) ;
}
2011-07-16 23:53:15 +02:00
me . maptype . updateNamedTile ( tileName ) ;
2011-01-14 20:08:23 +01:00
} ,
2011-02-22 14:49:03 +01:00
addPlayer : function ( update ) {
2011-01-14 20:08:23 +01:00
var me = this ;
2011-02-22 14:49:03 +01:00
var player = me . players [ update . name ] = {
name : update . name ,
2011-05-18 16:38:50 +02:00
location : new Location ( me . worlds [ update . world ] , parseFloat ( update . x ) , parseFloat ( update . y ) , parseFloat ( update . z ) ) ,
2011-05-21 07:59:08 +02:00
health : update . health ,
2011-05-22 16:09:50 +02:00
armor : update . armor ,
account : update . account
2011-02-22 14:49:03 +01:00
} ;
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'playeradded' , [ player ] ) ;
2011-02-22 14:49:03 +01:00
// Create the player-menu-item.
var playerIconContainer ;
var menuitem = player . menuitem = $ ( '<li/>' )
. addClass ( 'player' )
. append ( playerIconContainer = $ ( '<span/>' )
. addClass ( 'playerIcon' )
2011-03-01 00:01:35 +01:00
. append ( $ ( '<img/>' ) . attr ( { src : 'images/player_face.png' } ) )
2011-02-22 14:49:03 +01:00
. attr ( { title : 'Follow ' + player . name } )
. click ( function ( ) {
2011-02-22 15:38:49 +01:00
var follow = player !== me . followingPlayer ;
me . followPlayer ( follow ? player : null ) ;
2011-02-22 14:49:03 +01:00
} )
)
. append ( $ ( '<a/>' )
. attr ( {
href : '#' ,
title : 'Center on ' + player . name
} )
. text ( player . name )
)
2011-03-05 17:43:41 +01:00
. click ( function ( e ) {
2011-03-08 01:22:03 +01:00
if ( me . followingPlayer !== player ) {
me . followPlayer ( null ) ;
}
2011-07-16 23:53:15 +02:00
me . panToLocation ( player . location ) ;
2011-03-05 17:43:41 +01:00
} )
2011-02-22 14:49:03 +01:00
. appendTo ( me . playerlist ) ;
if ( me . options . showplayerfacesinmenu ) {
2011-05-21 07:59:08 +02:00
getMinecraftHead ( player . account , 16 , function ( head ) {
2011-02-22 14:49:03 +01:00
$ ( 'img' , playerIconContainer ) . remove ( ) ;
$ ( head ) . appendTo ( playerIconContainer ) ;
} ) ;
2011-01-14 20:08:23 +01:00
}
2011-02-22 14:49:03 +01:00
} ,
updatePlayer : function ( player , update ) {
var me = this ;
var location = player . location = new Location ( me . worlds [ update . world ] , parseFloat ( update . x ) , parseFloat ( update . y ) , parseFloat ( update . z ) ) ;
2011-05-18 16:38:50 +02:00
player . health = update . health ;
2011-05-22 16:09:50 +02:00
player . armor = update . armor ;
2011-01-14 20:08:23 +01:00
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'playerupdated' , [ player ] ) ;
2011-02-22 14:49:03 +01:00
// Update menuitem.
2011-02-22 15:38:49 +01:00
player . menuitem . toggleClass ( 'otherworld' , me . world !== location . world ) ;
2011-02-22 14:49:03 +01:00
2011-02-22 15:38:49 +01:00
if ( player === me . followingPlayer ) {
2011-02-22 14:49:03 +01:00
// Follow the updated player.
2011-07-16 23:53:15 +02:00
me . panToLocation ( player . location ) ;
2011-01-14 20:08:23 +01:00
}
} ,
2011-02-22 14:49:03 +01:00
removePlayer : function ( player ) {
var me = this ;
delete me . players [ player . name ] ;
2011-03-21 19:07:30 +01:00
$ ( me ) . trigger ( 'playerremoved' , [ player ] ) ;
2011-02-22 14:49:03 +01:00
// Remove menu item.
player . menuitem . remove ( ) ;
} ,
followPlayer : function ( player ) {
2011-01-14 20:08:23 +01:00
var me = this ;
2011-02-17 20:08:50 +01:00
$ ( '.following' , me . playerlist ) . removeClass ( 'following' ) ;
2011-01-14 20:08:23 +01:00
2011-02-22 14:49:03 +01:00
if ( player ) {
$ ( player . menuitem ) . addClass ( 'following' ) ;
2011-07-16 23:53:15 +02:00
me . panToLocation ( player . location ) ;
2011-01-14 20:08:23 +01:00
}
2011-02-22 14:49:03 +01:00
this . followingPlayer = player ;
2011-06-03 10:24:21 +02:00
} ,
updateBackground : function ( ) {
var me = this ;
var col = "#000000" ;
if ( me . serverday ) {
2011-08-02 07:07:17 +02:00
if ( me . maptype . options . backgroundday )
col = me . maptype . options . backgroundday ;
else if ( me . maptype . options . background )
col = me . maptype . options . background ;
2011-06-03 10:24:21 +02:00
}
else {
2011-08-02 07:07:17 +02:00
if ( me . maptype . options . backgroundnight )
col = me . maptype . options . backgroundnight ;
else if ( me . maptype . options . background )
col = me . maptype . options . background ;
2011-06-03 10:24:21 +02:00
}
$ ( '.map' ) . css ( 'background' , col ) ;
2011-08-02 07:07:17 +02:00
$ ( '.leaflet-tile' ) . css ( 'background' , col ) ;
2011-06-27 05:05:43 +02:00
} ,
getParameterByName : function ( name ) {
name = name . replace ( /[\[]/ , "\\\[" ) . replace ( /[\]]/ , "\\\]" ) ;
var regexS = "[\\?&]" + name + "=([^&#]*)" ;
var regex = new RegExp ( regexS ) ;
var results = regex . exec ( window . location . href ) ;
if ( results == null )
return "" ;
else
return decodeURIComponent ( results [ 1 ] . replace ( /\+/g , " " ) ) ;
2011-06-28 03:38:26 +02:00
} ,
getIntParameterByName : function ( name ) {
var v = this . getParameterByName ( name ) ;
if ( v != "" ) {
v = parseInt ( v , 10 ) ;
if ( v != NaN ) {
return v ;
}
}
return null ;
2011-09-14 07:00:35 +02:00
} ,
layersetlist : [ ] ,
addToLayerSelector : function ( layer , name , priority ) {
var me = this ;
2011-10-02 04:32:57 +02:00
if ( ! me . layercontrol ) {
me . layercontrol = new DynmapLayerControl ( ) ;
map . addControl ( me . layercontrol ) ;
}
2011-09-14 07:00:35 +02:00
var i ;
for ( i = 0 ; i < me . layersetlist . length ; i ++ ) {
if ( me . layersetlist [ i ] . layer === layer ) {
me . layersetlist [ i ] . priority = priority ;
me . layersetlist [ i ] . name = name ;
break ;
}
}
if ( i >= me . layersetlist . length ) {
me . layersetlist [ i ] = { layer : layer , priority : priority , name : name } ;
}
me . layersetlist . sort ( function ( a , b ) {
if ( a . priority != b . priority )
return a . priority - b . priority ;
else
return ( ( a . name < b . name ) ? - 1 : ( ( a . name > b . name ) ? 1 : 0 ) ) ;
} ) ;
for ( i = 0 ; i < me . layersetlist . length ; i ++ ) {
me . layercontrol . removeLayer ( me . layersetlist [ i ] . layer ) ;
}
for ( i = 0 ; i < me . layersetlist . length ; i ++ ) {
me . layercontrol . addOverlay ( me . layersetlist [ i ] . layer , me . layersetlist [ i ] . name ) ;
}
} ,
removeFromLayerSelector : function ( layer ) {
var me = this ;
var i ;
for ( i = 0 ; i < me . layersetlist . length ; i ++ ) {
if ( me . layersetlist [ i ] . layer === layer ) {
me . layersetlist . splice ( i , 1 ) ;
me . layercontrol . removeLayer ( layer ) ;
break ;
}
}
2011-06-27 05:05:43 +02:00
}
2011-01-14 20:08:23 +01:00
// TODO: Enable hash-links.
/ * u p d a t e L i n k : f u n c t i o n ( ) {
var me = this ;
var url = location . href . match ( /^[^#]+/ ) ;
var a = url
+ "#lat=" + me . map . getCenter ( ) . lat ( ) . toFixed ( 6 )
+ "&lng=" + me . map . getCenter ( ) . lng ( ) . toFixed ( 6 )
+ "&zoom=" + me . map . getZoom ( ) ;
me . linkbox . data ( 'link' ) . val ( a ) ;
} * /
2011-02-22 17:58:39 +01:00
} ;