1 | var mapinfo = {
|
---|
2 | regionsize: 512,
|
---|
3 | chunksize: 16,
|
---|
4 | tilesize: 128,
|
---|
5 | maxzoom: 4
|
---|
6 | }
|
---|
7 |
|
---|
8 | function InitMap() {
|
---|
9 | // ===============================================================================================
|
---|
10 | // 7dtd coordinate transformations
|
---|
11 |
|
---|
12 | SDTD_Projection = {
|
---|
13 | project: function (latlng) {
|
---|
14 | return new L.Point(
|
---|
15 | (latlng.lat) / Math.pow(2, mapinfo.maxzoom),
|
---|
16 | (latlng.lng) / Math.pow(2, mapinfo.maxzoom) );
|
---|
17 | },
|
---|
18 |
|
---|
19 | unproject: function (point) {
|
---|
20 | return new L.LatLng(
|
---|
21 | point.x * Math.pow(2, mapinfo.maxzoom),
|
---|
22 | point.y * Math.pow(2, mapinfo.maxzoom) );
|
---|
23 | }
|
---|
24 | };
|
---|
25 |
|
---|
26 | SDTD_CRS = L.extend({}, L.CRS.Simple, {
|
---|
27 | projection: SDTD_Projection,
|
---|
28 | transformation: new L.Transformation(1, 0, -1, 0),
|
---|
29 |
|
---|
30 | scale: function (zoom) {
|
---|
31 | return Math.pow(2, zoom);
|
---|
32 | }
|
---|
33 | });
|
---|
34 |
|
---|
35 | // ===============================================================================================
|
---|
36 | // Map and basic tile layers
|
---|
37 |
|
---|
38 | map = L.map('tab_map', {
|
---|
39 | zoomControl: false, // Added by Zoomslider
|
---|
40 | zoomsliderControl: true,
|
---|
41 | attributionControl: false,
|
---|
42 | crs: SDTD_CRS
|
---|
43 | }).setView([0, 0], Math.max(0, mapinfo.maxzoom - 5));
|
---|
44 |
|
---|
45 |
|
---|
46 | var initTime = new Date().getTime();
|
---|
47 | var tileLayer = GetSdtdTileLayer (mapinfo, initTime);
|
---|
48 | var tileLayerMiniMap = GetSdtdTileLayer (mapinfo, initTime, true);
|
---|
49 |
|
---|
50 | // player icon
|
---|
51 | var playerIcon = L.icon({
|
---|
52 | iconUrl: '/static/leaflet/images/marker-survivor.png',
|
---|
53 | iconRetinaUrl: '/static/leaflet/images/marker-survivor-2x.png',
|
---|
54 | iconSize: [25, 48],
|
---|
55 | iconAnchor: [12, 24],
|
---|
56 | popupAnchor: [0, -20]
|
---|
57 | });
|
---|
58 |
|
---|
59 | // hostile icon
|
---|
60 | var hostileIcon = L.icon({
|
---|
61 | iconUrl: '/static/leaflet/images/marker-zombie.png',
|
---|
62 | iconRetinaUrl: '/static/leaflet/images/marker-zombie-2x.png',
|
---|
63 | iconSize: [25, 33],
|
---|
64 | iconAnchor: [12, 16],
|
---|
65 | popupAnchor: [0, -10]
|
---|
66 | });
|
---|
67 |
|
---|
68 | // animal icon
|
---|
69 | var animalIcon = L.icon({
|
---|
70 | iconUrl: '/static/leaflet/images/marker-animal.png',
|
---|
71 | iconRetinaUrl: '/static/leaflet/images/marker-animal-2x.png',
|
---|
72 | iconSize: [25, 26],
|
---|
73 | iconAnchor: [12, 13],
|
---|
74 | popupAnchor: [0, -10]
|
---|
75 | });
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | // ===============================================================================================
|
---|
80 | // Overlays and controls
|
---|
81 |
|
---|
82 | var playersOnlineMarkerGroup = L.markerClusterGroup({
|
---|
83 | maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
|
---|
84 | });
|
---|
85 | var playersOfflineMarkerGroup = L.markerClusterGroup({
|
---|
86 | maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
|
---|
87 | });
|
---|
88 | var hostilesMarkerGroup = L.markerClusterGroup({
|
---|
89 | maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
|
---|
90 | });
|
---|
91 | var animalsMarkerGroup = L.markerClusterGroup({
|
---|
92 | maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
|
---|
93 | });
|
---|
94 |
|
---|
95 |
|
---|
96 | var layerControl = L.control.layers({
|
---|
97 | //"Map": tileLayer
|
---|
98 | }, null, {
|
---|
99 | collapsed: false
|
---|
100 | }
|
---|
101 | );
|
---|
102 |
|
---|
103 | var layerCount = 0;
|
---|
104 |
|
---|
105 |
|
---|
106 | tileLayer.addTo(map);
|
---|
107 |
|
---|
108 | new L.Control.Coordinates({}).addTo(map);
|
---|
109 |
|
---|
110 | new L.Control.ReloadTiles({
|
---|
111 | autoreload_enable: true,
|
---|
112 | autoreload_minInterval: 30,
|
---|
113 | autoreload_interval: 120,
|
---|
114 | autoreload_defaultOn: false,
|
---|
115 | layers: [tileLayer, tileLayerMiniMap]
|
---|
116 | }).addTo(map);
|
---|
117 |
|
---|
118 | layerControl.addOverlay (GetRegionLayer (mapinfo), "Region files");
|
---|
119 |
|
---|
120 | var miniMap = new L.Control.MiniMap(tileLayerMiniMap, {
|
---|
121 | zoomLevelOffset: -6,
|
---|
122 | toggleDisplay: true
|
---|
123 | }).addTo(map);
|
---|
124 |
|
---|
125 | var measure = L.control.measure({
|
---|
126 | //primaryLengthUnit: "meters",
|
---|
127 | //primaryAreaUnit: "sqmeters",
|
---|
128 | //activeColor: "#ABE67E",
|
---|
129 | //completedColor: "#C8F2BE",
|
---|
130 | position: "bottomleft"
|
---|
131 | });
|
---|
132 | //measure.addTo(map);
|
---|
133 |
|
---|
134 | //new L.Control.GameTime({}).addTo(map);
|
---|
135 |
|
---|
136 | if (HasPermission ("webapi.getlandclaims")) {
|
---|
137 | layerControl.addOverlay (GetLandClaimsLayer (map, mapinfo), "Land claims");
|
---|
138 | layerCount++;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (HasPermission ("webapi.gethostilelocation")) {
|
---|
142 | layerControl.addOverlay (hostilesMarkerGroup, "Hostiles (<span id='mapControlHostileCount'>0</span>)");
|
---|
143 | layerCount++;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (HasPermission ("webapi.getanimalslocation")) {
|
---|
147 | layerControl.addOverlay (animalsMarkerGroup, "Animals (<span id='mapControlAnimalsCount'>0</span>)");
|
---|
148 | layerCount++;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (HasPermission ("webapi.getplayerslocation")) {
|
---|
152 | layerControl.addOverlay (playersOfflineMarkerGroup, "Players (offline) (<span id='mapControlOfflineCount'>0</span>)");
|
---|
153 | layerControl.addOverlay (playersOnlineMarkerGroup, "Players (online) (<span id='mapControlOnlineCount'>0</span>)");
|
---|
154 | layerCount++;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (layerCount > 0) {
|
---|
158 | layerControl.addTo(map);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | var hostilesMappingList = {};
|
---|
165 | var animalsMappingList = {};
|
---|
166 | var playersMappingList = {};
|
---|
167 |
|
---|
168 |
|
---|
169 |
|
---|
170 | // ===============================================================================================
|
---|
171 | // Player markers
|
---|
172 |
|
---|
173 | $(".leaflet-popup-pane").on('click.action', '.inventoryButton', function(event) {
|
---|
174 | ShowInventoryDialog ($(this).data('steamid'));
|
---|
175 | });
|
---|
176 |
|
---|
177 | var openedPopup = null;
|
---|
178 | var updatingMarkers = false;
|
---|
179 |
|
---|
180 | map.on ("popupopen", function (event) {
|
---|
181 | console.log ("open");
|
---|
182 | console.log (event.popup._source);
|
---|
183 | openedPopup = event.popup._source;
|
---|
184 | });
|
---|
185 | map.on ("popupclose", function (event) {
|
---|
186 | if (!updatingMarkers) {
|
---|
187 | console.log ("close");
|
---|
188 | openedPopup = null;
|
---|
189 | }
|
---|
190 | });
|
---|
191 |
|
---|
192 | var setPlayerMarkers = function(data) {
|
---|
193 | var online = 0;
|
---|
194 | var offline = 0;
|
---|
195 | updatingMarkers = true;
|
---|
196 | $.each( data, function( key, val ) {
|
---|
197 | var marker;
|
---|
198 | if (playersMappingList.hasOwnProperty(val.steamid)) {
|
---|
199 | marker = playersMappingList[val.steamid].currentPosMarker;
|
---|
200 | } else {
|
---|
201 | marker = L.marker([val.position.x, val.position.z], {icon: playerIcon}).bindPopup(
|
---|
202 | "Player: " + val.name +
|
---|
203 | (HasPermission ("webapi.getplayerinventory") ?
|
---|
204 | "<br/><a class='inventoryButton' data-steamid='"+val.steamid+"'>Show inventory</a>"
|
---|
205 | : "")
|
---|
206 | );
|
---|
207 | playersMappingList[val.steamid] = { online: !val.online };
|
---|
208 | }
|
---|
209 |
|
---|
210 | oldpos = marker.getLatLng ();
|
---|
211 | if ( playersMappingList[val.steamid].online != val.online || oldpos.lat != val.position.x || oldpos.lng != val.position.z ) {
|
---|
212 | if (playersMappingList[val.steamid].online) {
|
---|
213 | playersOnlineMarkerGroup.removeLayer(marker);
|
---|
214 | } else {
|
---|
215 | playersOfflineMarkerGroup.removeLayer(marker);
|
---|
216 | }
|
---|
217 | marker.setLatLng([val.position.x, val.position.z]);
|
---|
218 | if (val.online) {
|
---|
219 | marker.setOpacity(1.0);
|
---|
220 | playersOnlineMarkerGroup.addLayer(marker);
|
---|
221 | } else {
|
---|
222 | marker.setOpacity(0.5);
|
---|
223 | playersOfflineMarkerGroup.addLayer(marker);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | val.currentPosMarker = marker;
|
---|
228 | playersMappingList[val.steamid] = val;
|
---|
229 |
|
---|
230 | if (val.online)
|
---|
231 | online++;
|
---|
232 | else
|
---|
233 | offline++;
|
---|
234 | });
|
---|
235 | updatingMarkers = false;
|
---|
236 | if (openedPopup != null) {
|
---|
237 | openedPopup.openPopup ();
|
---|
238 | }
|
---|
239 | $( "#mapControlOnlineCount" ).text( online );
|
---|
240 | $( "#mapControlOfflineCount" ).text( offline );
|
---|
241 | }
|
---|
242 |
|
---|
243 | var updatePlayerTimeout;
|
---|
244 | var updatePlayerEvent = function() {
|
---|
245 | $.getJSON( "../api/getplayerslocation")
|
---|
246 | .done(setPlayerMarkers)
|
---|
247 | .fail(function(jqxhr, textStatus, error) {
|
---|
248 | console.log("Error fetching players list");
|
---|
249 | })
|
---|
250 | .always(function() {
|
---|
251 | updatePlayerTimeout = window.setTimeout(updatePlayerEvent, 4000);
|
---|
252 | });
|
---|
253 | }
|
---|
254 |
|
---|
255 | tabs.on ("tabbedcontenttabopened", function (event, data) {
|
---|
256 | if (data.newTab === "#tab_map") {
|
---|
257 | if (HasPermission ("webapi.getplayerslocation")) {
|
---|
258 | updatePlayerEvent ();
|
---|
259 | }
|
---|
260 | } else {
|
---|
261 | window.clearTimeout (updatePlayerTimeout);
|
---|
262 | }
|
---|
263 | });
|
---|
264 |
|
---|
265 | if (tabs.tabbedContent ("isTabOpen", "tab_map")) {
|
---|
266 | if (HasPermission ("webapi.getplayerslocation")) {
|
---|
267 | updatePlayerEvent ();
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 |
|
---|
274 | // ===============================================================================================
|
---|
275 | // Hostiles markers
|
---|
276 |
|
---|
277 | var setHostileMarkers = function(data) {
|
---|
278 | updatingMarkersHostile = true;
|
---|
279 |
|
---|
280 | var hostileCount = 0;
|
---|
281 |
|
---|
282 | hostilesMarkerGroup.clearLayers();
|
---|
283 |
|
---|
284 | $.each( data, function( key, val ) {
|
---|
285 | var marker;
|
---|
286 | if (hostilesMappingList.hasOwnProperty(val.id)) {
|
---|
287 | marker = hostilesMappingList[val.id].currentPosMarker;
|
---|
288 | } else {
|
---|
289 | marker = L.marker([val.position.x, val.position.z], {icon: hostileIcon}).bindPopup(
|
---|
290 | "Hostile: " + val.name
|
---|
291 | );
|
---|
292 | //hostilesMappingList[val.id] = { };
|
---|
293 | hostilesMarkerGroup.addLayer(marker);
|
---|
294 | }
|
---|
295 |
|
---|
296 | var bAbort = false;
|
---|
297 |
|
---|
298 | oldpos = marker.getLatLng ();
|
---|
299 |
|
---|
300 | //if ( oldpos.lat != val.position.x || oldpos.lng != val.position.z ) {
|
---|
301 | // hostilesMarkerGroup.removeLayer(marker);
|
---|
302 | marker.setLatLng([val.position.x, val.position.z]);
|
---|
303 | marker.setOpacity(1.0);
|
---|
304 | hostilesMarkerGroup.addLayer(marker);
|
---|
305 | //}
|
---|
306 |
|
---|
307 | val.currentPosMarker = marker;
|
---|
308 | hostilesMappingList[val.id] = val;
|
---|
309 |
|
---|
310 | hostileCount++;
|
---|
311 | });
|
---|
312 |
|
---|
313 | $( "#mapControlHostileCount" ).text( hostileCount );
|
---|
314 |
|
---|
315 | updatingMarkersHostile = false;
|
---|
316 | }
|
---|
317 |
|
---|
318 | var updateHostileTimeout;
|
---|
319 | var updateHostileEvent = function() {
|
---|
320 | $.getJSON( "../api/gethostilelocation")
|
---|
321 | .done(setHostileMarkers)
|
---|
322 | .fail(function(jqxhr, textStatus, error) {
|
---|
323 | console.log("Error fetching hostile list");
|
---|
324 | })
|
---|
325 | .always(function() {
|
---|
326 | updateHostileTimeout = window.setTimeout(updateHostileEvent, 4000);
|
---|
327 | });
|
---|
328 | }
|
---|
329 |
|
---|
330 | tabs.on ("tabbedcontenttabopened", function (event, data) {
|
---|
331 | if (data.newTab === "#tab_map") {
|
---|
332 | if (HasPermission ("webapi.gethostilelocation")) {
|
---|
333 | updateHostileEvent ();
|
---|
334 | }
|
---|
335 | } else {
|
---|
336 | window.clearTimeout (updateHostileTimeout);
|
---|
337 | }
|
---|
338 | });
|
---|
339 |
|
---|
340 | if (tabs.tabbedContent ("isTabOpen", "tab_map")) {
|
---|
341 | if (HasPermission ("webapi.gethostilelocation")) {
|
---|
342 | updateHostileEvent ();
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 |
|
---|
348 | // ===============================================================================================
|
---|
349 | // Animals markers
|
---|
350 |
|
---|
351 | var setAnimalMarkers = function(data) {
|
---|
352 | updatingMarkersAnimals = true;
|
---|
353 |
|
---|
354 | var animalsCount = 0;
|
---|
355 |
|
---|
356 | animalsMarkerGroup.clearLayers();
|
---|
357 |
|
---|
358 | $.each( data, function( key, val ) {
|
---|
359 | var marker;
|
---|
360 | if (animalsMappingList.hasOwnProperty(val.id)) {
|
---|
361 | marker = animalsMappingList[val.id].currentPosMarker;
|
---|
362 | } else {
|
---|
363 | marker = L.marker([val.position.x, val.position.z], {icon: animalIcon}).bindPopup(
|
---|
364 | "Animal: " + val.name
|
---|
365 | );
|
---|
366 | //animalsMappingList[val.id] = { };
|
---|
367 | animalsMarkerGroup.addLayer(marker);
|
---|
368 | }
|
---|
369 |
|
---|
370 | var bAbort = false;
|
---|
371 |
|
---|
372 | oldpos = marker.getLatLng ();
|
---|
373 |
|
---|
374 | //if ( oldpos.lat != val.position.x || oldpos.lng != val.position.z ) {
|
---|
375 | // animalsMarkerGroup.removeLayer(marker);
|
---|
376 | marker.setLatLng([val.position.x, val.position.z]);
|
---|
377 | marker.setOpacity(1.0);
|
---|
378 | animalsMarkerGroup.addLayer(marker);
|
---|
379 | //}
|
---|
380 |
|
---|
381 | val.currentPosMarker = marker;
|
---|
382 | animalsMappingList[val.id] = val;
|
---|
383 |
|
---|
384 | animalsCount++;
|
---|
385 | });
|
---|
386 |
|
---|
387 | $( "#mapControlAnimalsCount" ).text( animalsCount );
|
---|
388 |
|
---|
389 | updatingMarkersAnimals = false;
|
---|
390 | }
|
---|
391 |
|
---|
392 | var updateAnimalsTimeout;
|
---|
393 | var updateAnimalsEvent = function() {
|
---|
394 | $.getJSON( "../api/getanimalslocation")
|
---|
395 | .done(setAnimalMarkers)
|
---|
396 | .fail(function(jqxhr, textStatus, error) {
|
---|
397 | console.log("Error fetching animals list");
|
---|
398 | })
|
---|
399 | .always(function() {
|
---|
400 | updateAnimalsTimeout = window.setTimeout(updateAnimalsEvent, 4000);
|
---|
401 | });
|
---|
402 | }
|
---|
403 |
|
---|
404 | tabs.on ("tabbedcontenttabopened", function (event, data) {
|
---|
405 | if (data.newTab === "#tab_map") {
|
---|
406 | if (HasPermission ("webapi.getanimalslocation")) {
|
---|
407 | updateAnimalsEvent ();
|
---|
408 | }
|
---|
409 | } else {
|
---|
410 | window.clearTimeout (updateAnimalsTimeout);
|
---|
411 | }
|
---|
412 | });
|
---|
413 |
|
---|
414 | if (tabs.tabbedContent ("isTabOpen", "tab_map")) {
|
---|
415 | if (HasPermission ("webapi.getanimalslocation")) {
|
---|
416 | updateAnimalsEvent ();
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 |
|
---|
424 |
|
---|
425 |
|
---|
426 | function StartMapModule () {
|
---|
427 | $.getJSON( "../map/mapinfo.json")
|
---|
428 | .done(function(data) {
|
---|
429 | mapinfo.tilesize = data.blockSize;
|
---|
430 | mapinfo.maxzoom = data.maxZoom;
|
---|
431 | })
|
---|
432 | .fail(function(jqxhr, textStatus, error) {
|
---|
433 | console.log ("Error fetching map information");
|
---|
434 | })
|
---|
435 | .always(function() {
|
---|
436 | InitMap ();
|
---|
437 | });
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|