// string, object, function(object) function post(url, data, callback) { console.log("POST " + url + JSON.stringify(data)); var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var json = JSON.parse(xhr.responseText); callback(json); } }; xhr.send(JSON.stringify(data)); } // function to sort arrays of objects after a keys value function sortByKey(array, key) { return array.sort(function(a, b) { var x = Number(a[key]); var y = Number(b[key]); return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }); } function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } /// code below here // declaring globals var chartsByID = {}; // update the online indicator dot setInterval(function(){ getJSON("/api/slaves", function(err, slaveData){ let indicator = document.querySelector("#online-indicator"); $(indicator).removeClass('fa-signal'); $(indicator).removeClass('fa-dizzy'); $(indicator).removeClass('fa-plug'); if(err){ // our request threw an error, most likely master is unavailable so we show yellow indicator indicator.style.color = "yellow"; $(indicator).addClass('fa-plug'); indicator.title = "Master server unavailable"; // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other } else if(Math.floor(slaveData[getParameterByName("instanceID")].time/100000) === Math.floor(Date.now()/100000)){ indicator.style.color = "green"; $(indicator).addClass('fa-signal'); indicator.title = "Slave is online"; } else { indicator.style.color = "red"; $(indicator).addClass('fa-dizzy'); indicator.title = "Slave is offline"; } }); }, 1000); function regexModDetails(modFileName) { let m; let results = []; const regex = /(.+)_(\d+\.\d+\.\d+)\.zip/gm; while ((m = regex.exec(modFileName)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, _) => { results.push(match); }); } return results } populateSlaveInfo(); function populateSlaveInfo(){ let instanceID = getParameterByName("instanceID"); if(!instanceID){ throw "We need instanceID! Also, *funny joke* but it doesn't work after I standardized on calling it instanceID"; } getJSON("/api/slaves", function(err, slaveData){ /* {"-1530507550":{ "time":"1495475926375", "rconPort":"8714", "rconPassword":"hidden", "serverPort":"14345", "unique":"-1530507550", "publicIP":"localhost", "mods":[ { "modName":"clusterio_1.0.0.zip", "hash":"341feb6c60918c83f7f3a6a14a4a308aef18dda6" },{ "modName":"clusterio_1.0.1.zip", "hash":"aee7a4b67ece19bddf236312fe408a5d4b67248d" },{ "modName":"clusterio_1.1.0.zip", "hash":"18e2f7e402d716e3dd1c1458b7fab2d0bb29eccf" }], "playerCount":"0", "instanceName":"t", "mac":"44-85-00-D2-7E-D6"} } */ let slave = slaveData[instanceID]; let HTML = '
| # | \n' + 'Version | \n' + 'Name | \n' + 'Summary | \n' + '
|---|---|---|---|
| ${i + 1} | ${modRegexResults[2]} | ${modRegexResults[1]} |