From 213a1e02e349ce287c2a357f66aa102871c5c70d Mon Sep 17 00:00:00 2001 From: Jordan Dalton Date: Fri, 8 Feb 2019 20:28:18 +0000 Subject: [PATCH] refactor: update scripts to use new global variables Hopefully this makes the code more bareable to read --- js/src/alerter.js | 10 +---- js/src/controls.js | 2 +- js/src/map.js | 92 +++++++++++++++++++++++----------------------- js/src/socket.js | 19 ++++------ js/src/utils.js | 8 ++-- 5 files changed, 59 insertions(+), 72 deletions(-) diff --git a/js/src/alerter.js b/js/src/alerter.js index 6966391a..da67997b 100644 --- a/js/src/alerter.js +++ b/js/src/alerter.js @@ -37,15 +37,7 @@ function createAlert(data, settings){ data.title = "Warning!"; } - settings = Object.assign(settings, { - newsest_on_top: true, - placement: { - from: "bottom", - align: "left" - }, - delay: 10000, - type: "warning" - }); + settings = Object.assign({ newsest_on_top: true, placement: { from: "bottom", align: "left" }, delay: 10000, type: "warning" }, settings); //console.log(JSON.stringify(data)); //console.log(JSON.stringify(settings)); diff --git a/js/src/controls.js b/js/src/controls.js index d833be71..35418e9a 100644 --- a/js/src/controls.js +++ b/js/src/controls.js @@ -54,7 +54,7 @@ $(document).ready(function(){ return; } - _MAP_map.setZoom(6);// zoom in! + Map.setZoom(3);// zoom in! _trackPlayer = this.value; }); diff --git a/js/src/map.js b/js/src/map.js index d8d63272..61586b6c 100644 --- a/js/src/map.js +++ b/js/src/map.js @@ -16,61 +16,59 @@ // along with this program in the file "LICENSE". If not, see . // ************************************************************************** // -var _MAP_currentMarker; -var _MAP_markerStore = []; -var _MAP_map; - -var _MAP_tiles = { - "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 - "Postal": L.tileLayer("images/tiles/postal/minimap_sea_{y}_{x}.png", { minZoom: -4, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 3072 }) -}; - -var _MAP_currentLayer = _MAP_tiles["Normal"]; +window.MarkerStore = []; +window.CurrentLayer = undefined; function mapInit(elementID) { // Create the different layers + var tileLayers = {}; var maps = window.config.maps; - console.log("maps:" + maps); + maps.forEach(map => { + console.log(map); + tileLayers[map.name] = L.tileLayer(map.url, + Object.assign( + { minZoom: -2, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 1024, tileDirectory: config.tileDirectory }, + map) + ); + console.log(tileLayers[map.name]); + }); + + CurrentLayer = tileLayers[Object.keys(tileLayers)[0]]; - _MAP_map = L.map(elementID, { + window.Map = L.map(elementID, { crs: L.CRS.Simple, - layers: [_MAP_currentLayer] + layers: [CurrentLayer] }).setView([0,0], 0); - // Use the "normal" bounds since, it's default. - var h = _MAP_currentLayer.options.tileSize * 3, - w = _MAP_currentLayer.options.tileSize * 2; + var h = CurrentLayer.options.tileSize * 3, + w = CurrentLayer.options.tileSize * 2; - var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom()); - var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom()); + var southWest = Map.unproject([0, h], Map.getMaxZoom()); + var northEast = Map.unproject([w, 0], Map.getMaxZoom()); var mapBounds = new L.LatLngBounds(southWest, northEast); - _MAP_map.setMaxBounds(mapBounds); - L.control.layers(_MAP_tiles).addTo(_MAP_map); + Map.setMaxBounds(mapBounds); + L.control.layers(tileLayers).addTo(Map); - _MAP_map.on("baselayerchange", function (e) { + Map.on("baselayerchange", function (e) { var h = e.layer.options.tileSize * 3, w = e.layer.options.tileSize * 2; - var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom()); - var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom()); + var southWest = Map.unproject([0, h], Map.getMaxZoom()); + var northEast = Map.unproject([w, 0], Map.getMaxZoom()); var mapBounds = new L.LatLngBounds(southWest, northEast); - _MAP_map.setMaxBounds(mapBounds); - _MAP_currentLayer = e.layer; + Map.setMaxBounds(mapBounds); + CurrentLayer = e.layer; clearAllMarkers(); toggleBlips(); }); - _MAP_map.on('click', function (e) { - //console.log(e); + Map.on('click', function (e) { }); - //L.tileLayer("images/tiles/minimap_{y}_{x}.png", {tileSize: 512}).addTo(_MAP_map); - - ///_MAP_map.setCenter([w * 0.5, h * 0.5]); } function createMarker(animated, draggable, objectRef, title) { @@ -101,33 +99,33 @@ function createMarker(animated, draggable, objectRef, title) { var marker = L.marker(coord, { title: title, - id: _MAP_markerStore.length, + id: MarkerStore.length, icon: image, object: objectRef, draggable: draggable ? true : false - }).addTo(_MAP_map).bindPopup(infoContent); + }).addTo(Map).bindPopup(infoContent); - _MAP_markerStore.push(marker); - return _MAP_markerStore.length; + MarkerStore.push(marker); + return MarkerStore.length; } function setMapCenter(lat, lng) { - _MAP_map.setCenter([lat, lng]); - _MAP_map.setZoom(6); + Map.setCenter([lat, lng]); + Map.setZoom(6); } function setMapCenterLeaflet(coord) { - _MAP_map.setCenter(coord); - _MAP_map.setZoom(6); + Map.setCenter(coord); + Map.setZoom(6); } function clearAllMarkers() { - for (var i = 0; i < _MAP_markerStore.length; i++) { - if (_MAP_markerStore[i] != "NULL") { - _MAP_markerStore[i].remove(); + for (var i = 0; i < MarkerStore.length; i++) { + if (MarkerStore[i] != "NULL") { + MarkerStore[i].remove(); } } - _MAP_markerStore.length = 0; + MarkerStore.length = 0; // Re-do player markers for(var id in localCache){ localCache[id].marker = null; @@ -135,15 +133,15 @@ function clearAllMarkers() { } function clearMarker(id) { - if (_MAP_markerStore[id] != "NULL") { - _MAP_markerStore[id].remove(); - _MAP_markerStore[id] = "NULL"; + if (MarkerStore[id] != "NULL") { + MarkerStore[id].remove(); + MarkerStore[id] = "NULL"; $("#marker_" + id).remove(); } } function getMarker(id) { - if (_MAP_markerStore[id] != "NULL") { - return _MAP_markerStore[id]; + if (MarkerStore[id] != "NULL") { + return MarkerStore[id]; } }; diff --git a/js/src/socket.js b/js/src/socket.js index c12d0776..670ac963 100644 --- a/js/src/socket.js +++ b/js/src/socket.js @@ -37,9 +37,8 @@ function connect(connectionString) { } function onOpen(e) { - _isConnected = true; if (config.debug) { - console.log("_isConnected: " + _isConnected); + console.log("_isConnected: " + webSocket.readyState == WebSocket.OPEN); } $("#connection").removeClass("badge-danger") @@ -147,8 +146,6 @@ function onClose(e) { $("#connection").removeClass("badge-success") .removeClass("badge-warning") .addClass("badge-danger").text("disconnected"); - - _isConnected = false; } var localCache = {}; @@ -222,7 +219,7 @@ function updateBlip(blipObj) { if (blipObj.hasOwnProperty("new_pos")) { // Blips are supposed to be static so, why this would even be fucking needed it beyond me // Still, better to prepare for the inevitability that someone wants this fucking feature - marker.setLatLng(convertToMapLeaflet(blipObj.new_pos.x, blipObj.new_pos.y, blipObj.new_pos.z)); + marker.setLatLng(convertToMap(blipObj.new_pos.x, blipObj.new_pos.y, blipObj.new_pos.z)); blipObj.pos = blipObj.new_pos; delete blipObj.new_pos; } @@ -289,7 +286,7 @@ function getPlayerInfoHtml(plr) { if (!(key === "identifer")) { html += '
' + key + ': ' + plr[key] + '
'; - } else if (_SETTINGS_showIdentifiers && key == "identifer") { + } else if (config.showIdentifiers && key == "identifer") { html += '
Identifer: ' + plr[key] + '
'; } else { continue; @@ -324,7 +321,7 @@ function doPlayerUpdate(players) { if (_trackPlayer != null && _trackPlayer == plr.identifer) { // If we're tracking a player, make sure we center them - _MAP_map.panTo(convertToMapLeaflet(plr.pos.x, plr.pos.y)); + Map.panTo(convertToMap(plr.pos.x, plr.pos.y)); } if (localCache[plr.identifer].marker != null || localCache[plr.identifer].marker != undefined) { @@ -334,18 +331,18 @@ function doPlayerUpdate(players) { //console.log("Got icon of :" + plr.icon); - _MAP_markerStore[localCache[plr.identifer].marker].setIcon(L.icon(t)); + MarkerStore[localCache[plr.identifer].marker].setIcon(L.icon(t)); } // Update the player's location on the map :) - _MAP_markerStore[localCache[plr.identifer].marker].setLatLng(convertToMapLeaflet(plr.pos.x, plr.pos.y)); + MarkerStore[localCache[plr.identifer].marker].setLatLng(convertToMapLeaflet(plr.pos.x, plr.pos.y)); //update popup with the information we have been sent var html = getPlayerInfoHtml(plr); var infoContent = '
' + plr.name + '
' + html + "
"; - _MAP_markerStore[localCache[plr.identifer].marker].bindPopup(infoContent); + MarkerStore[localCache[plr.identifer].marker].bindPopup(infoContent); } else { @@ -356,7 +353,7 @@ function doPlayerUpdate(players) { var infoContent = '
' + plr.name + '
' + html + "
"; - _MAP_markerStore[m].bindPopup(infoContent); + MarkerStore[m].bindPopup(infoContent); } diff --git a/js/src/utils.js b/js/src/utils.js index c82e73f6..6ad1cd23 100644 --- a/js/src/utils.js +++ b/js/src/utils.js @@ -30,11 +30,11 @@ var game_2_x = -3778.16; var game_2_y = -4549.6; function convertToMap(x, y) { - var h = _MAP_currentLayer.options.tileSize * 3, - w = _MAP_currentLayer.options.tileSize * 2; + var h = CurrentLayer.options.tileSize * 3, + w = CurrentLayer.options.tileSize * 2; - var latLng1 = _MAP_map.unproject([w * 0.5, h * 0.5], _MAP_map.getMaxZoom()); - var latLng2 = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom()); + var latLng1 = Map.unproject([w * 0.5, h * 0.5], Map.getMaxZoom()); + var latLng2 = Map.unproject([0, h], Map.getMaxZoom()); var rLng = latLng1.lng + (x - game_1_x) * (latLng1.lng - latLng2.lng) / (game_1_x - game_2_x); var rLat = latLng1.lat + (y - game_1_y) * (latLng1.lat - latLng2.lat) / (game_1_y - game_2_y);