ServerStatus/server-status.js

105 lines
4.2 KiB
JavaScript

String.prototype.trunc = String.prototype.trunc ||
function(n){
return (this.length > n) ? this.substr(0,n-1)+'…' : this;
};
jQuery(function() {
jQuery.fn.extend({
serverStatus: function(str, d, set, ref) {
new jQuery.serverStatus(this, str, d, set, ref);
}
});
jQuery.serverStatus = function(container, str, d, set, ref) {
$serverStatus = this;
//Structure
var structure = jQuery.extend([], str); //Structure
//Data
var data = jQuery.extend([], d); //Data
//Settings
var settings = jQuery.extend({
type : "status" //Can also be: counter (counts total players online)
}, set); //Settings
//Refresh time
var refresh = ref; //Refresh time
$container = container;
$structure = structure;
$data = data;
$settings = settings;
$refresh = refresh;
//Initialize server-status plugin
$serverStatus._init = function() {
$container.html('');
//Add header bar
var content = '<div class="server-status-header">';
for (var y=0; y<$structure.length; y++) {
content += '<div class="server-status-header-row" data-id="'+$structure[y].id+'">'+$structure[y].title+'</div>';
}
content += '</div>\
<div class="server-status-servers">\
</div>';
$container.append(content);
//Add data
for (var i=0; i<$data.length; i++) {
if (!$data[i].hidden) {
var row = '<div class="server-status-row" data-id="'+i+'">';
for (var y=0; y<$structure.length; y++) {
if ($structure[y].type == "status") {row += '<div class="server-status-'+$structure[y].id+'" data-type="'+$structure[y].type+'"><i class="fa fa-circle"></i></div>';}
else if ($structure[y].type == "counter") {row += '<div class="server-status-'+$structure[y].id+'" data-type="'+$structure[y].type+'">0/0</div>';}
else if ($structure[y].type == "map") {row += '<div class="server-status-'+$structure[y].id+'" data-type="'+$structure[y].type+'">-</div>';}
else {row += '<div class="server-status-'+$structure[y].id+'" data-type="'+$structure[y].type+'">'+$data[i][$structure[y].id]+'</div>';}
}
$container.find('.server-status-servers').append(row);
}
}
} //Initialize
//Update row
$serverStatus._updateRow = function(response) {
$this = jQuery('#' + response["container"] + ' .server-status-servers .server-status-row[data-id="'+response["id"]+'"]');
if (response["status"] == "offline") {
$this.find('div[data-type="status"] i').removeClass('online').addClass('offline');
$this.find('div[data-type="counter"]').html('0/0');
$this.find('div[data-type="map"]').html('');
}
else {
$this.find('div[data-type="status"] i').removeClass('offline').addClass('online');
$this.find('div[data-type="counter"]').html(response["players"]);
$this.find('div[data-type="map"]').html(response["map"]);
/*totalPlayersOnline = totalPlayersOnline + parseInt(response["players"].split('\/')[0]);
jQuery('.server-status-total-num').html(totalPlayersOnline);*/
}
} //Update row
//Update server-status
$serverStatus._updateStatus = function() {
$container.find('.server-status-servers .server-status-row').each(function() {
$this = jQuery(this);
$id = $this.data('id');
$this.find('div[data-type="status"] i').removeClass('online').removeClass('offline');
$this.find('div[data-type="counter"]').html('<img src="//static.moow.info/server-status/loading.gif" alt="loading" />');
jQuery.getJSON('https://static.moow.info/server-status/query.php?type='+$data[$id].type+'&ip=' + $data[$id].ip + '&container=' + $container.attr('id') + '&id=' + $id, function(response) {$serverStatus._updateRow(response);});
});
} //Update status
//Init after creation
$serverStatus._init();
$serverStatus._updateStatus();
if ($refresh > 0) {setInterval(function(){ $serverStatus._updateStatus(); }, $refresh);}
};
});