Minetrack/lib/util.js

174 lines
4.3 KiB
JavaScript
Raw Normal View History

Minetrack 5 (#143) * remove unused #getServer methods, inline #roundToPoint * replace #safeName regex with incremental ids * remove legacy #setInterval based #updateMojangServices handling * add Tooltip class, move faviconSize to css instead of js * move server id assignment to ServerRegistry * move printPort logic to formatMinecraftServerAddress, add MINECRAFT_DEFAULT_PORTS * simplify ping tracking * rework perc-bar tooltip to not use mousemove event * begin moving graphing logic to GraphDisplayManager * begin merge graph point tracking into graphDisplayManager * centralizing graphing logic into GraphDisplayManager * properly reset GraphDisplayManager when handling disconnects * move individual server graph data into ServerGraph class * constantly run sortServers loop to simplify logic * inline #updateMojangServices method * resize performance improvements * remove legacy bootTime refresh behavior, require manual user refresh * move class defs to core.js * remove unused #isGraphDataVisible arg * remove #toggleControlsDrawer * dont call #updatePercentageBar in #updateServerStatus calls * centralize caption handling * inline #msToTime * remove hackish seconds handling for timestamps * reduce #forEach calls with filter/map * safely fallback to errorMessage if errno/description does not match * Add /images/missing_favicon.png path instead of putting base64 in js * remove debug * cleanup mojang status handling * move historyPlot instance into GraphDisplayManager * cleanup checkbox html generation * cleanup #updateServerStatus * fix up tooltip styling * move jquery code out of core.js * fix add server race condition when initially pinging servers * send error.placeholder=true for pending pings so the frontend can discard later * filter placeholder pings sent by the backend * del assets/images/logo_2014.png * move graph code into graph.js * merge pingTracker into ServerRegistry+ServerGraph * remove todos * simplify getVisibleGraphData * fix potential sortServers race condition when adding * use #show instead of #fadeIn(0) * remove publicConfig.json, send over socket * update docs/CHANGELOG.md * getOrAssign -> getOrCreateId * dont delete graph controls when disconnected * early work cleaning up HTML+CSS structures * cleanup server css elements * cleanup graph control css elements * move base CSS color values into @media(prefers-color-scheme: light) * move CSS magic colors to vars * reduce duplicated CSS color rules * inline body text color CSS * WIP replacing jQuery calls with vanilla JS * WIP replacing jQuery calls with vanilla JS * replace getElementsByClass with querySelectorAll * typeMarker -> serverTypeHTML * use jQuery slim for remaining flot.js dependency * merge setAllGraphVisibility into GraphDisplayManager * break apart element update and redraw logic * add eslint + parcel bundler * auto lint assets/js when building * statically serve favicons/ for faviconOverrides outside of dist/ * only send favicons when changed * move faviconOverride behavior into entry in servers.json * add warning to backend server files * remove .server-favicon-missing class * add Minetrack 5 migration guide * add npm run build step to install.sh * adjust package.json version to 5.0.0 * remove js references from index.html * move logic and behavior out of site.js * cleanup ServerRegistry methods * prevent multiple history graph redraws * add comments * cleanup #addServer usage, move to App * move graph control bindings into GraphDisplayManager * site.js -> main.js, core.js -> servers.js * move Tooltip/Caption into util.js * spacing tweak * format index.html * ensure the frontend does not handling updateHistoryGraph events * prevent versions/record updates if the same value * avoid empty percbar updates, ensure versions are sorted * only include main.js ref in index.html * serve minified copy of font awesome directly * bundle icons.css into main.css, remove Open Sans 400 * add new SVG logo * update docs/CHANGELOG.md * new design, server version grouping * remove start.sh call from install.sh * move graph controls into header with new button * move #handleSettingsToggle back to graph * fix legacy code behavior of currentVersionIndex applying globally * fix header text color in light mode * fix mojang status text color in light mode * fix toggle settings and checkbox colors * tweak button hover color * tweak button hover color * add new status-overlay to avoid complicated DOM management during loading * fix initial graph rendering bug * add comments * update default graph tick sizes * prevent #tooltip from overflowing page * remove localhost spec * prevent minor connection errors from reshuffling layout * update CHANGELOG.md * add message/button for manually loading historical graph on mobile devices * send isGraphVisible to frontend to prevent alert if logToDatabase: false * send timestamp data with record * update docs/CHANGELOG.md * remove clock icon * remove 24h peak timestamp * Only check favicon if present * safely handle undefined/empty knownVersions in #formatMinecraftVersions * merge config.versions and minecraft.json into minecraft_versions.json, simplify index matching behavior * remove localhost url in socket.io config * stub methods/linkage for FocusManager * add #isObjectEqual hack, add event proxying to FocusManager * wip extended stats box * remove server-type badging * tweak mojang unstable color * serve socket.io-client using parcel * fix incorrect mojang status colors * remove legacy capitalization design * redesign focus boxes * update docs/CHANGELOG.md * remove localhost ref * color clock icon * use background-color for hover effect, remove unused var * improve stats focus box icons * change mojang sessions icon to globe * Add favorites system * remove focus boxes * update docs/CHANGELOG.md * remove focus icons from font * simplify graph related event binding * Add Sort By button * store current sortOption in localStorage * update docs/CHANGELOG.md * move magic 0 sortOption to SORT_OPTION_INDEX_DEFAULT * remove localhost ref * merge #settings-toggle, #sort-by and .mojang-status CSS * remove .focus-box CSS * use sortedServerIds for _lastSortedServers * tweak --color-blue * new missing_favicon design to match logo * edit footer CSS/text, remove github icon * replace player count diff counter with GROWTH sort option * italize non-default sort options * add Only Favorites button to auto sync favorites to the visible graph data * add icons to graph control buttons * update docs/CHANGELOG.md * use * to denote non-default sort option instead * remove localhost url in socket.io config * add value highlighting to make sort by easier to read * remove last remaining uppercase text * remove serverTypesVisible from config.json * simplify header CSS, fix spacing with logToDatabase=false * fix inverted text color on highlighted values * remove localhost url in socket.io config * break header into rows on mobile devices Co-authored-by: Hugo Manrique <contact@hugmanrique.me>
2020-04-20 00:27:59 +00:00
/**
* THIS IS LEGACY, UNMAINTAINED CODE
* IT MAY (AND LIKELY DOES) CONTAIN BUGS
* USAGE IS NOT RECOMMENDED
*/
2016-02-01 10:49:54 +00:00
var logger = require('./logger');
var dns = require('dns');
2016-02-01 10:49:54 +00:00
var config = require('../config.json');
var servers = require('../servers.json');
var serverNameLookup = [];
2016-02-01 11:14:33 +00:00
// Finds a server in servers.json with a matching IP.
// If it finds one, it caches the result for faster future lookups.
2016-02-01 10:49:54 +00:00
function getServerNameByIp(ip) {
var lookupName = serverNameLookup[ip];
if (lookupName) {
return lookupName;
}
for (var i = 0; i < servers.length; i++) {
var entry = servers[i];
if (entry.ip === ip) {
2016-02-01 10:53:40 +00:00
serverNameLookup[entry.ip] = entry.name;
2016-02-01 10:49:54 +00:00
return entry.name;
}
}
}
2016-02-01 11:14:33 +00:00
// Returns a list of configured server IPs from servers.json
2016-02-01 10:55:37 +00:00
function getServerIps() {
var ips = [];
for (var i = 0; i < servers.length; i++) {
ips.push(servers[i].ip);
}
return ips;
}
2015-12-18 07:45:38 +00:00
// This method is a monstrosity.
// Since we loaded ALL pings from the database, we need to filter out the pings so each entry is a minute apart.
// This is done by iterating over the list, since the time between each ping can be completely arbitrary.
function trimUselessPings(data) {
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
var listing = data[keys[i]];
var lastTimestamp = 0;
var filteredListing = [];
for (var x = 0; x < listing.length; x++) {
var entry = listing[x];
// 0 is the index of the timestamp.
// See the convertPingsToGraph method.
if (entry[0] - lastTimestamp >= 60 * 1000) {
2015-12-18 08:17:39 +00:00
// This second check tries to smooth out randomly dropped pings.
// By default we only want entries that are online (playerCount > 0).
// This way we'll keep looking forward until we find one that is online.
// However if we can't find one within a reasonable timeframe, select the sucky one.
if (entry[0] - lastTimestamp >= 120 * 1000 || entry[1] > 0) {
filteredListing.push(entry);
lastTimestamp = entry[0];
}
2015-12-18 07:45:38 +00:00
}
}
data[keys[i]] = filteredListing;
}
}
2015-12-19 03:23:24 +00:00
exports.trimOldPings = function(data) {
var keys = Object.keys(data);
var timeMs = exports.getCurrentTimeMs();
for (var x = 0; x < keys.length; x++) {
var listing = data[keys[x]];
var toSplice = [];
for (var i = 0; i < listing.length; i++) {
var entry = listing[i];
if (timeMs - entry[0] > config.graphDuration) {
toSplice.push(i);
}
}
for (var i = 0; i < toSplice.length; i++) {
listing.splice(toSplice[i], 1);
}
}
}
2015-11-09 00:34:17 +00:00
exports.getCurrentTimeMs = function() {
return new Date().getTime();
2015-11-24 23:24:17 +00:00
};
2017-04-04 03:10:46 +00:00
exports.stringToColor = function(base) {
var hash;
for (var i = base.length - 1, hash = 0; i >= 0; i--) {
hash = base.charCodeAt(i) + ((hash << 5) - hash);
}
color = Math.floor(Math.abs((Math.sin(hash) * 10000) % 1 * 16777216)).toString(16);
return '#' + Array(6 - color.length + 1).join('0') + color;
}
2015-11-24 23:24:17 +00:00
exports.setIntervalNoDelay = function(func, delay) {
var task = setInterval(func, delay);
func();
return task;
2015-12-18 07:45:38 +00:00
};
2017-03-11 23:44:21 +00:00
exports.convertServerHistory = function(sqlData) {
2016-02-01 10:55:37 +00:00
var serverIps = getServerIps();
2015-12-18 07:45:38 +00:00
var graphData = {};
2016-02-01 11:44:07 +00:00
var startTime = exports.getCurrentTimeMs();
2016-02-01 11:43:23 +00:00
2015-12-18 07:45:38 +00:00
for (var i = 0; i < sqlData.length; i++) {
var entry = sqlData[i];
2017-03-11 23:44:21 +00:00
if (serverIps.indexOf(entry.ip) === -1) continue;
2016-02-01 10:55:37 +00:00
2016-02-01 10:49:54 +00:00
var name = getServerNameByIp(entry.ip);
2017-03-11 23:44:21 +00:00
if (!graphData[name]) graphData[name] = [];
2016-02-01 10:49:54 +00:00
graphData[name].push([entry.timestamp, entry.playerCount]);
2015-12-18 07:45:38 +00:00
}
// Break it into minutes.
trimUselessPings(graphData);
2015-12-19 03:23:24 +00:00
// Drop old data.
exports.trimOldPings(graphData);
2017-03-11 23:44:21 +00:00
logger.info('Parsed ' + sqlData.length + ' ping records in ' + (exports.getCurrentTimeMs() - startTime) + 'ms');
2016-02-01 11:43:23 +00:00
2017-03-14 22:07:58 +00:00
return graphData;
};
/**
* Attempts to resolve Minecraft PC SRV records from DNS, otherwise falling back to the old hostname.
*
* @param hostname hostname to check
* @param port port to pass to callback if required
* @param callback function with a hostname and port parameter
*/
exports.unfurlSRV = function(hostname, port, callback) {
dns.resolveSrv("_minecraft._tcp."+hostname, function (err, records) {
if(!records||records.length<=0) {
callback(hostname, port);
return;
}
callback(records[0].name, records[0].port);
})
2017-03-11 23:44:21 +00:00
};
exports.getRemoteAddr = function(req) {
let remoteAddress = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
return remoteAddress;
};