source: binary-improvements/webserver/js/map.js@ 250

Last change on this file since 250 was 250, checked in by alloc, 9 years ago

Fixes 5_7_9

File size: 6.1 KB
Line 
1var mapinfo = {
2 regionsize: 512,
3 chunksize: 16,
4 tilesize: 128,
5 maxzoom: 4
6}
7
8function 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
51
52
53 // ===============================================================================================
54 // Overlays and controls
55
56 var playersOnlineMarkerGroup = L.markerClusterGroup({
57 maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
58 });
59 var playersOfflineMarkerGroup = L.markerClusterGroup({
60 maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
61 });
62
63
64 var layerControl = L.control.layers({
65 //"Map": tileLayer
66 }, null, {
67 collapsed: false
68 }
69 );
70
71 var layerCount = 0;
72
73
74 tileLayer.addTo(map);
75
76 new L.Control.Coordinates({}).addTo(map);
77
78 new L.Control.ReloadTiles({
79 autoreload_enable: true,
80 autoreload_minInterval: 30,
81 autoreload_interval: 120,
82 autoreload_defaultOn: false,
83 layers: [tileLayer, tileLayerMiniMap]
84 }).addTo(map);
85
86 layerControl.addOverlay (GetRegionLayer (mapinfo), "Region files");
87
88 var miniMap = new L.Control.MiniMap(tileLayerMiniMap, {
89 zoomLevelOffset: -6,
90 toggleDisplay: true
91 }).addTo(map);
92
93 var measure = L.control.measure({
94 //primaryLengthUnit: "meters",
95 //primaryAreaUnit: "sqmeters",
96 //activeColor: "#ABE67E",
97 //completedColor: "#C8F2BE",
98 position: "bottomleft"
99 });
100 //measure.addTo(map);
101
102 //new L.Control.GameTime({}).addTo(map);
103
104 if (HasPermission ("webapi.getlandclaims")) {
105 layerControl.addOverlay (GetLandClaimsLayer (map, mapinfo), "Land claims");
106 layerCount++;
107 }
108
109 if (HasPermission ("webapi.getplayerslocation")) {
110 layerControl.addOverlay (playersOfflineMarkerGroup, "Players (offline) (<span id='mapControlOfflineCount'>0</span>)");
111 layerControl.addOverlay (playersOnlineMarkerGroup, "Players (online) (<span id='mapControlOnlineCount'>0</span>)");
112 layerCount++;
113 }
114
115 if (layerCount > 0) {
116 layerControl.addTo(map);
117 }
118
119
120
121
122 var playersMappingList = {};
123
124
125
126 // ===============================================================================================
127 // Player markers
128
129 $(".leaflet-popup-pane").on('click.action', '.inventoryButton', function(event) {
130 ShowInventoryDialog ($(this).data('steamid'));
131 });
132
133 var openedPopup = null;
134 var updatingMarkers = false;
135
136 map.on ("popupopen", function (event) {
137 console.log ("open");
138 console.log (event.popup._source);
139 openedPopup = event.popup._source;
140 });
141 map.on ("popupclose", function (event) {
142 if (!updatingMarkers) {
143 console.log ("close");
144 openedPopup = null;
145 }
146 });
147
148 var setPlayerMarkers = function(data) {
149 var online = 0;
150 var offline = 0;
151 updatingMarkers = true;
152 $.each( data, function( key, val ) {
153 var marker;
154 if (playersMappingList.hasOwnProperty(val.steamid)) {
155 marker = playersMappingList[val.steamid].currentPosMarker;
156 } else {
157 marker = L.marker([val.position.x, val.position.z]).bindPopup(
158 "Player: " + val.name +
159 (HasPermission ("webapi.getplayerinventory") ?
160 "<br/><a class='inventoryButton' data-steamid='"+val.steamid+"'>Show inventory</a>"
161 : "")
162 );
163 playersMappingList[val.steamid] = { online: !val.online };
164 }
165
166 oldpos = marker.getLatLng ();
167 if ( playersMappingList[val.steamid].online != val.online || oldpos.lat != val.position.x || oldpos.lng != val.position.z ) {
168 if (playersMappingList[val.steamid].online) {
169 playersOnlineMarkerGroup.removeLayer(marker);
170 } else {
171 playersOfflineMarkerGroup.removeLayer(marker);
172 }
173 marker.setLatLng([val.position.x, val.position.z]);
174 if (val.online) {
175 marker.setOpacity(1.0);
176 playersOnlineMarkerGroup.addLayer(marker);
177 } else {
178 marker.setOpacity(0.5);
179 playersOfflineMarkerGroup.addLayer(marker);
180 }
181 }
182
183 val.currentPosMarker = marker;
184 playersMappingList[val.steamid] = val;
185
186 if (val.online)
187 online++;
188 else
189 offline++;
190 });
191 updatingMarkers = false;
192 if (openedPopup != null) {
193 openedPopup.openPopup ();
194 }
195 $( "#mapControlOnlineCount" ).text( online );
196 $( "#mapControlOfflineCount" ).text( offline );
197 }
198
199 var updatePlayerTimeout;
200 var updatePlayerEvent = function() {
201 $.getJSON( "../api/getplayerslocation")
202 .done(setPlayerMarkers)
203 .fail(function(jqxhr, textStatus, error) {
204 console.log("Error fetching players list");
205 })
206 .always(function() {
207 updatePlayerTimeout = window.setTimeout(updatePlayerEvent, 4000);
208 });
209 }
210
211 tabs.on ("tabbedcontenttabopened", function (event, data) {
212 if (data.newTab === "#tab_map") {
213 if (HasPermission ("webapi.getplayerslocation")) {
214 updatePlayerEvent ();
215 }
216 } else {
217 window.clearTimeout (updatePlayerTimeout);
218 }
219 });
220
221 if (tabs.tabbedContent ("isTabOpen", "tab_map")) {
222 if (HasPermission ("webapi.getplayerslocation")) {
223 updatePlayerEvent ();
224 }
225 }
226
227
228}
229
230
231
232
233
234function StartMapModule () {
235 $.getJSON( "../map/mapinfo.json")
236 .done(function(data) {
237 mapinfo.tilesize = data.blockSize;
238 mapinfo.maxzoom = data.maxZoom;
239 })
240 .fail(function(jqxhr, textStatus, error) {
241 console.log ("Error fetching map information");
242 })
243 .always(function() {
244 InitMap ();
245 });
246}
247
248
Note: See TracBrowser for help on using the repository browser.