window.address = location.hostname + (location.port ? ':' + location.port : '');
window.origin = location.protocol + '//' + window.address;
function toBool (data)
{
if (data > 0)
{
return "Yes";
}
return "No";
}
function getFfType (data)
{
switch(data)
{
case 0:
return "Disabled";
case 1:
return "Enabled";
case 2:
return "Reflected";
case 3:
return "Shared";
default:
return "Disabled";
}
}
function getServerInfo(status, countPlayers)
{
var mod = status.fs_game;
if (!mod)
{
mod = "";
}
var row = "
";
row += "Map: " + status.mapname + " | Gametype: " + status.g_gametype + " | Players: " + countPlayers + "/" + status.sv_maxclients + " | ";
row += "
"
row += "Mod: " + mod.replace(/^mods\//g, "") + " | Security Level: " + status.sv_securityLevel + " | Password Protected: " + toBool(status.isPrivate) + " | ";
row += "
"
row += "Hardcore Mode: " + toBool(status.g_hardcore) + " | KillCam: " + toBool(status.scr_game_allowkillcam) + " | Friendly Fire: " + getFfType(status.scr_team_fftype) + " | ";
row += "
"
$("#serverinfo").append(row);
}
function buildPlayerList(players)
{
for (var i = 0; i < players.length; ++i)
{
var row = "";
row += "" + players[i].name + " | ";
row += "" + players[i].test_client + " | ";
row += "" + players[i].score + " | ";
row += "" + players[i].ping + " | ";
row += "
";
$("#players").append(row);
}
if (players.length == 0)
{
$("#playertable").fadeOut(1);
}
}
function buildPage(data)
{
// Get dvar sv_hostname and remove color codes
var hostname = (data["status"]["sv_hostname"]).replace(/\^[0-9:;c]/g, "");
// Set title
document.title = hostname
$("#servername").text(document.title);
getServerInfo(data["status"], data["players"].length);
buildPlayerList(data["players"]);
$('body').fadeIn(300);
}
$(document).ready(function()
{
$("#connect").click(function()
{
location.replace("iw4x://" + window.address);
});
$.getJSON(origin + "/info", function(data)
{
console.log(data);
setTimeout(buildPage.bind(undefined, data), 10);
}).fail(function()
{
alert("An error occured while updating the server information!");
});
$("#footer").click(function()
{
location.replace("https://xlabs.dev");
});
});