Skip to content

Commit 213a1e0

Browse files
committed
refactor: update scripts to use new global variables
Hopefully this makes the code more bareable to read
1 parent 0b7ef8d commit 213a1e0

File tree

5 files changed

+59
-72
lines changed

5 files changed

+59
-72
lines changed

js/src/alerter.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ function createAlert(data, settings){
3737
data.title = "<strong>Warning!</strong>";
3838
}
3939

40-
settings = Object.assign(settings, {
41-
newsest_on_top: true,
42-
placement: {
43-
from: "bottom",
44-
align: "left"
45-
},
46-
delay: 10000,
47-
type: "warning"
48-
});
40+
settings = Object.assign({ newsest_on_top: true, placement: { from: "bottom", align: "left" }, delay: 10000, type: "warning" }, settings);
4941

5042
//console.log(JSON.stringify(data));
5143
//console.log(JSON.stringify(settings));

js/src/controls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ $(document).ready(function(){
5454
return;
5555
}
5656

57-
_MAP_map.setZoom(6);// zoom in!
57+
Map.setZoom(3);// zoom in!
5858
_trackPlayer = this.value;
5959
});
6060

js/src/map.js

+45-47
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,59 @@
1616
// along with this program in the file "LICENSE". If not, see <http://www.gnu.org/licenses/>.
1717
// ************************************************************************** //
1818

19-
var _MAP_currentMarker;
20-
var _MAP_markerStore = [];
21-
var _MAP_map;
22-
23-
var _MAP_tiles = {
24-
"Normal": L.tileLayer("images/tiles/normal/minimap_sea_{y}_{x}.png", { minZoom: -2, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 1024 }), //Originally had the "tileSize" attribute set. Removing it allows for the "zoomed out" look
25-
"Postal": L.tileLayer("images/tiles/postal/minimap_sea_{y}_{x}.png", { minZoom: -4, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 3072 })
26-
};
27-
28-
var _MAP_currentLayer = _MAP_tiles["Normal"];
19+
window.MarkerStore = [];
20+
window.CurrentLayer = undefined;
2921

3022
function mapInit(elementID) {
3123

3224
// Create the different layers
25+
var tileLayers = {};
3326
var maps = window.config.maps;
34-
console.log("maps:" + maps);
27+
maps.forEach(map => {
28+
console.log(map);
29+
tileLayers[map.name] = L.tileLayer(map.url,
30+
Object.assign(
31+
{ minZoom: -2, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 1024, tileDirectory: config.tileDirectory },
32+
map)
33+
);
34+
console.log(tileLayers[map.name]);
35+
});
36+
37+
CurrentLayer = tileLayers[Object.keys(tileLayers)[0]];
3538

36-
_MAP_map = L.map(elementID, {
39+
window.Map = L.map(elementID, {
3740
crs: L.CRS.Simple,
38-
layers: [_MAP_currentLayer]
41+
layers: [CurrentLayer]
3942
}).setView([0,0], 0);
4043

41-
// Use the "normal" bounds since, it's default.
42-
var h = _MAP_currentLayer.options.tileSize * 3,
43-
w = _MAP_currentLayer.options.tileSize * 2;
44+
var h = CurrentLayer.options.tileSize * 3,
45+
w = CurrentLayer.options.tileSize * 2;
4446

45-
var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
46-
var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom());
47+
var southWest = Map.unproject([0, h], Map.getMaxZoom());
48+
var northEast = Map.unproject([w, 0], Map.getMaxZoom());
4749
var mapBounds = new L.LatLngBounds(southWest, northEast);
4850

49-
_MAP_map.setMaxBounds(mapBounds);
50-
L.control.layers(_MAP_tiles).addTo(_MAP_map);
51+
Map.setMaxBounds(mapBounds);
52+
L.control.layers(tileLayers).addTo(Map);
5153

52-
_MAP_map.on("baselayerchange", function (e) {
54+
Map.on("baselayerchange", function (e) {
5355
var h = e.layer.options.tileSize * 3,
5456
w = e.layer.options.tileSize * 2;
5557

56-
var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
57-
var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom());
58+
var southWest = Map.unproject([0, h], Map.getMaxZoom());
59+
var northEast = Map.unproject([w, 0], Map.getMaxZoom());
5860

5961
var mapBounds = new L.LatLngBounds(southWest, northEast);
6062

61-
_MAP_map.setMaxBounds(mapBounds);
62-
_MAP_currentLayer = e.layer;
63+
Map.setMaxBounds(mapBounds);
64+
CurrentLayer = e.layer;
6365

6466
clearAllMarkers();
6567
toggleBlips();
6668
});
6769

68-
_MAP_map.on('click', function (e) {
69-
//console.log(e);
70+
Map.on('click', function (e) {
7071
});
71-
//L.tileLayer("images/tiles/minimap_{y}_{x}.png", {tileSize: 512}).addTo(_MAP_map);
72-
73-
///_MAP_map.setCenter([w * 0.5, h * 0.5]);
7472
}
7573

7674
function createMarker(animated, draggable, objectRef, title) {
@@ -101,49 +99,49 @@ function createMarker(animated, draggable, objectRef, title) {
10199

102100
var marker = L.marker(coord, {
103101
title: title,
104-
id: _MAP_markerStore.length,
102+
id: MarkerStore.length,
105103
icon: image,
106104
object: objectRef,
107105
draggable: draggable ? true : false
108-
}).addTo(_MAP_map).bindPopup(infoContent);
106+
}).addTo(Map).bindPopup(infoContent);
109107

110-
_MAP_markerStore.push(marker);
111-
return _MAP_markerStore.length;
108+
MarkerStore.push(marker);
109+
return MarkerStore.length;
112110
}
113111

114112
function setMapCenter(lat, lng) {
115-
_MAP_map.setCenter([lat, lng]);
116-
_MAP_map.setZoom(6);
113+
Map.setCenter([lat, lng]);
114+
Map.setZoom(6);
117115
}
118116

119117
function setMapCenterLeaflet(coord) {
120-
_MAP_map.setCenter(coord);
121-
_MAP_map.setZoom(6);
118+
Map.setCenter(coord);
119+
Map.setZoom(6);
122120
}
123121

124122
function clearAllMarkers() {
125-
for (var i = 0; i < _MAP_markerStore.length; i++) {
126-
if (_MAP_markerStore[i] != "NULL") {
127-
_MAP_markerStore[i].remove();
123+
for (var i = 0; i < MarkerStore.length; i++) {
124+
if (MarkerStore[i] != "NULL") {
125+
MarkerStore[i].remove();
128126
}
129127
}
130-
_MAP_markerStore.length = 0;
128+
MarkerStore.length = 0;
131129
// Re-do player markers
132130
for(var id in localCache){
133131
localCache[id].marker = null;
134132
}
135133
}
136134

137135
function clearMarker(id) {
138-
if (_MAP_markerStore[id] != "NULL") {
139-
_MAP_markerStore[id].remove();
140-
_MAP_markerStore[id] = "NULL";
136+
if (MarkerStore[id] != "NULL") {
137+
MarkerStore[id].remove();
138+
MarkerStore[id] = "NULL";
141139
$("#marker_" + id).remove();
142140
}
143141
}
144142

145143
function getMarker(id) {
146-
if (_MAP_markerStore[id] != "NULL") {
147-
return _MAP_markerStore[id];
144+
if (MarkerStore[id] != "NULL") {
145+
return MarkerStore[id];
148146
}
149147
};

js/src/socket.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ function connect(connectionString) {
3737
}
3838

3939
function onOpen(e) {
40-
_isConnected = true;
4140
if (config.debug) {
42-
console.log("_isConnected: " + _isConnected);
41+
console.log("_isConnected: " + webSocket.readyState == WebSocket.OPEN);
4342
}
4443

4544
$("#connection").removeClass("badge-danger")
@@ -147,8 +146,6 @@ function onClose(e) {
147146
$("#connection").removeClass("badge-success")
148147
.removeClass("badge-warning")
149148
.addClass("badge-danger").text("disconnected");
150-
151-
_isConnected = false;
152149
}
153150

154151
var localCache = {};
@@ -222,7 +219,7 @@ function updateBlip(blipObj) {
222219
if (blipObj.hasOwnProperty("new_pos")) {
223220
// Blips are supposed to be static so, why this would even be fucking needed it beyond me
224221
// Still, better to prepare for the inevitability that someone wants this fucking feature
225-
marker.setLatLng(convertToMapLeaflet(blipObj.new_pos.x, blipObj.new_pos.y, blipObj.new_pos.z));
222+
marker.setLatLng(convertToMap(blipObj.new_pos.x, blipObj.new_pos.y, blipObj.new_pos.z));
226223
blipObj.pos = blipObj.new_pos;
227224
delete blipObj.new_pos;
228225
}
@@ -289,7 +286,7 @@ function getPlayerInfoHtml(plr) {
289286

290287
if (!(key === "identifer")) {
291288
html += '<div class="row info-body-row"><strong>' + key + ':</strong>&nbsp;' + plr[key] + '</div>';
292-
} else if (_SETTINGS_showIdentifiers && key == "identifer") {
289+
} else if (config.showIdentifiers && key == "identifer") {
293290
html += '<div class="row info-body-row"><strong>Identifer:</strong>&nbsp;' + plr[key] + '</div>';
294291
} else {
295292
continue;
@@ -324,7 +321,7 @@ function doPlayerUpdate(players) {
324321

325322
if (_trackPlayer != null && _trackPlayer == plr.identifer) {
326323
// If we're tracking a player, make sure we center them
327-
_MAP_map.panTo(convertToMapLeaflet(plr.pos.x, plr.pos.y));
324+
Map.panTo(convertToMap(plr.pos.x, plr.pos.y));
328325
}
329326

330327
if (localCache[plr.identifer].marker != null || localCache[plr.identifer].marker != undefined) {
@@ -334,18 +331,18 @@ function doPlayerUpdate(players) {
334331

335332
//console.log("Got icon of :" + plr.icon);
336333

337-
_MAP_markerStore[localCache[plr.identifer].marker].setIcon(L.icon(t));
334+
MarkerStore[localCache[plr.identifer].marker].setIcon(L.icon(t));
338335
}
339336

340337
// Update the player's location on the map :)
341-
_MAP_markerStore[localCache[plr.identifer].marker].setLatLng(convertToMapLeaflet(plr.pos.x, plr.pos.y));
338+
MarkerStore[localCache[plr.identifer].marker].setLatLng(convertToMapLeaflet(plr.pos.x, plr.pos.y));
342339

343340
//update popup with the information we have been sent
344341
var html = getPlayerInfoHtml(plr);
345342

346343
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
347344

348-
_MAP_markerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
345+
MarkerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
349346

350347
} else {
351348

@@ -356,7 +353,7 @@ function doPlayerUpdate(players) {
356353

357354
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-icon"></div><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
358355

359-
_MAP_markerStore[m].bindPopup(infoContent);
356+
MarkerStore[m].bindPopup(infoContent);
360357

361358
}
362359

js/src/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ var game_2_x = -3778.16;
3030
var game_2_y = -4549.6;
3131

3232
function convertToMap(x, y) {
33-
var h = _MAP_currentLayer.options.tileSize * 3,
34-
w = _MAP_currentLayer.options.tileSize * 2;
33+
var h = CurrentLayer.options.tileSize * 3,
34+
w = CurrentLayer.options.tileSize * 2;
3535

36-
var latLng1 = _MAP_map.unproject([w * 0.5, h * 0.5], _MAP_map.getMaxZoom());
37-
var latLng2 = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
36+
var latLng1 = Map.unproject([w * 0.5, h * 0.5], Map.getMaxZoom());
37+
var latLng2 = Map.unproject([0, h], Map.getMaxZoom());
3838

3939
var rLng = latLng1.lng + (x - game_1_x) * (latLng1.lng - latLng2.lng) / (game_1_x - game_2_x);
4040
var rLat = latLng1.lat + (y - game_1_y) * (latLng1.lat - latLng2.lat) / (game_1_y - game_2_y);

0 commit comments

Comments
 (0)