Skip to content

Commit

Permalink
refactor: update scripts to use new global variables
Browse files Browse the repository at this point in the history
Hopefully this makes the code more bareable to read
  • Loading branch information
TGRHavoc committed May 17, 2019
1 parent 0b7ef8d commit 213a1e0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 72 deletions.
10 changes: 1 addition & 9 deletions js/src/alerter.js
Expand Up @@ -37,15 +37,7 @@ function createAlert(data, settings){
data.title = "<strong>Warning!</strong>";
}

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));
Expand Down
2 changes: 1 addition & 1 deletion js/src/controls.js
Expand Up @@ -54,7 +54,7 @@ $(document).ready(function(){
return;
}

_MAP_map.setZoom(6);// zoom in!
Map.setZoom(3);// zoom in!
_trackPlayer = this.value;
});

Expand Down
92 changes: 45 additions & 47 deletions js/src/map.js
Expand Up @@ -16,61 +16,59 @@
// along with this program in the file "LICENSE". If not, see <http://www.gnu.org/licenses/>.
// ************************************************************************** //

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) {
Expand Down Expand Up @@ -101,49 +99,49 @@ 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;
}
}

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];
}
};
19 changes: 8 additions & 11 deletions js/src/socket.js
Expand Up @@ -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")
Expand Down Expand Up @@ -147,8 +146,6 @@ function onClose(e) {
$("#connection").removeClass("badge-success")
.removeClass("badge-warning")
.addClass("badge-danger").text("disconnected");

_isConnected = false;
}

var localCache = {};
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -289,7 +286,7 @@ function getPlayerInfoHtml(plr) {

if (!(key === "identifer")) {
html += '<div class="row info-body-row"><strong>' + key + ':</strong>&nbsp;' + plr[key] + '</div>';
} else if (_SETTINGS_showIdentifiers && key == "identifer") {
} else if (config.showIdentifiers && key == "identifer") {
html += '<div class="row info-body-row"><strong>Identifer:</strong>&nbsp;' + plr[key] + '</div>';
} else {
continue;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = '<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>";

_MAP_markerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
MarkerStore[localCache[plr.identifer].marker].bindPopup(infoContent);

} else {

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

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>";

_MAP_markerStore[m].bindPopup(infoContent);
MarkerStore[m].bindPopup(infoContent);

}

Expand Down
8 changes: 4 additions & 4 deletions js/src/utils.js
Expand Up @@ -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);
Expand Down

0 comments on commit 213a1e0

Please sign in to comment.