-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
67 lines (59 loc) · 1.89 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var url = "./"
var currentStatus = undefined;
const HTMLTAGS = {
SERVERSTATUS: "ServerStatus",
SERVERNAME: "ServerName",
PLAYERSONLINE: "PlayersOnline",
PLAYERSCONTAINER: "PlayersContainer",
STATUSDUMP: "dumpStatus"
}
async function getJSONData() {
fetchUrl = url + "status.json";
const response = await fetch(fetchUrl);
const jsonData = await response.json();
if (!_.isEqual(currentStatus, jsonData))
{
//console.log('new data pulled... updating')
currentStatus = jsonData;
updateHTMLStatus();
}
}
async function init(){
createCollapsible();
//await getJSONData();
beginFetchLoop();
}
function updateHTMLStatus(){
//console.log(currentStatus);
if(currentStatus.error != null){ //server offline
elementbyIdUpdate(HTMLTAGS.SERVERSTATUS, '<span class="offline">Server Offline</span>');
}
else{
elementbyIdUpdate(HTMLTAGS.SERVERSTATUS, '<span class="online">Online</span>');
elementbyIdUpdate(HTMLTAGS.STATUSDUMP, JSON.stringify(currentStatus));
elementbyIdUpdate(HTMLTAGS.SERVERNAME, currentStatus.server_name);
elementbyIdUpdate(HTMLTAGS.PLAYERSONLINE, "<span class='tag'>Players online: </span>" + currentStatus.player_count);
}
}
function elementbyIdUpdate(el, html){
document.getElementById(el).innerHTML = html;
}
function beginFetchLoop(){
setInterval(getJSONData, 2000);
}
function createCollapsible(){
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
}
init();