From 0955e8dbcd77b2523de0e41c5a5219fcb26046a1 Mon Sep 17 00:00:00 2001 From: Hugo Manrique Date: Mon, 3 Apr 2017 23:10:46 -0400 Subject: [PATCH] Allow custom Graph colors (#76) --- .gitignore | 2 +- app.js | 5 +++++ assets/js/graph.js | 4 ++-- assets/js/site.js | 2 +- assets/js/util.js | 18 ++++++++++++++++-- lib/util.js | 12 ++++++++++++ 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d3a526f..e8e879e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ production/ database.sql database.sql-journal .DS_Store - +yarn.lock \ No newline at end of file diff --git a/app.js b/app.js index 05dae4b..fc7672b 100644 --- a/app.js +++ b/app.js @@ -26,6 +26,11 @@ function pingAll() { for (var i = 0; i < servers.length; i++) { // Make sure we lock our scope. (function(network) { + // Asign auto generated color if not present + if (!network.color) { + network.color = util.stringToColor(network.name); + } + var attemptedVersion = config.versions[network.type][currentVersionIndex[network.type]]; ping.ping(network.ip, network.port, network.type, config.rates.connectTimeout, function(err, res) { // Handle our ping results, if it succeeded. diff --git a/assets/js/graph.js b/assets/js/graph.js index 9de0c59..497326f 100644 --- a/assets/js/graph.js +++ b/assets/js/graph.js @@ -117,12 +117,12 @@ function convertGraphData(rawData) { var keys = Object.keys(rawData); - for (var i = 0; i < keys.length; i++) { + for (var i = 0; i < keys.length; i++) { data.push({ data: rawData[keys[i]], yaxis: 1, label: keys[i], - color: stringToColor(keys[i]) + color: getServerColor(keys[i]) }); } diff --git a/assets/js/site.js b/assets/js/site.js index fbe9219..a6036d9 100644 --- a/assets/js/site.js +++ b/assets/js/site.js @@ -151,7 +151,7 @@ function updatePercentageBar() { id: 'perc_bar_part_' + safeNameCopy, class: 'perc-bar-part', html: '', - style: 'background: ' + stringToColor(server) + ';' + style: 'background: ' + getServerColor(server) + ';' }).appendTo(parent); div = $('#perc_bar_part_' + safeNameCopy); diff --git a/assets/js/util.js b/assets/js/util.js index c7a871b..d111a28 100644 --- a/assets/js/util.js +++ b/assets/js/util.js @@ -76,16 +76,30 @@ function getServersByCategory() { return byCategory; } -function getServerByIp(ip) { +function getServerByField(id, value) { for (var i = 0; i < publicConfig.servers.length; i++) { var entry = publicConfig.servers[i]; - if (entry.ip === ip) { + if (entry[id] === value) { return entry; } } } +function getServerByIp(ip) { + return getServerByField('ip', ip); +} + +function getServerByName(name) { + return getServerByField('name', name); +} + +function getServerColor(name) { + var server = getServerByName(name); + + return server ? server.color : stringToColor(name); +} + // Generate (and set) the HTML that displays Mojang status. // If nothing is passed, re-render the last update. // If something is passed, update and then re-render. diff --git a/lib/util.js b/lib/util.js index 1009bd2..d186e55 100644 --- a/lib/util.js +++ b/lib/util.js @@ -100,6 +100,18 @@ exports.getCurrentTimeMs = function() { return new Date().getTime(); }; +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; +} + exports.setIntervalNoDelay = function(func, delay) { var task = setInterval(func, delay);