Masterserver update

master
proller 2013-10-18 01:32:49 +04:00
parent e232f7311f
commit a924409bd1
8 changed files with 126 additions and 65 deletions

View File

@ -673,7 +673,6 @@ Server::Server(
m_objectdata_timer = 0.0; m_objectdata_timer = 0.0;
m_emergethread_trigger_timer = 0.0; m_emergethread_trigger_timer = 0.0;
m_savemap_timer = 0.0; m_savemap_timer = 0.0;
m_clients_number = 0;
m_env_mutex.Init(); m_env_mutex.Init();
m_con_mutex.Init(); m_con_mutex.Init();
@ -1244,7 +1243,7 @@ void Server::AsyncRunStep()
counter = 0.0; counter = 0.0;
JMutexAutoLock lock2(m_con_mutex); JMutexAutoLock lock2(m_con_mutex);
m_clients_number = 0; m_clients_names.clear();
if(m_clients.size() != 0) if(m_clients.size() != 0)
infostream<<"Players:"<<std::endl; infostream<<"Players:"<<std::endl;
for(std::map<u16, RemoteClient*>::iterator for(std::map<u16, RemoteClient*>::iterator
@ -1258,7 +1257,7 @@ void Server::AsyncRunStep()
continue; continue;
infostream<<"* "<<player->getName()<<"\t"; infostream<<"* "<<player->getName()<<"\t";
client->PrintInfo(infostream); client->PrintInfo(infostream);
++m_clients_number; m_clients_names.push_back(player->getName());
} }
} }
} }
@ -1270,7 +1269,7 @@ void Server::AsyncRunStep()
float &counter = m_masterserver_timer; float &counter = m_masterserver_timer;
if(!isSingleplayer() && (!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true) if(!isSingleplayer() && (!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true)
{ {
ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number, m_uptime.get(), m_gamespec.id, m_mods); ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_names, m_uptime.get(), m_env->getGameTime(), m_gamespec.id, m_mods);
counter = 0.01; counter = 0.01;
} }
counter += dtime; counter += dtime;

View File

@ -685,7 +685,7 @@ private:
JMutex m_con_mutex; JMutex m_con_mutex;
// Connected clients (behind the con mutex) // Connected clients (behind the con mutex)
std::map<u16, RemoteClient*> m_clients; std::map<u16, RemoteClient*> m_clients;
u16 m_clients_number; //for announcing masterserver std::vector<std::string> m_clients_names; //for announcing masterserver
// Ban checking // Ban checking
BanManager *m_banmanager; BanManager *m_banmanager;

View File

@ -194,11 +194,11 @@ static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb,
//((std::string*)userp)->append((char*)contents, size * nmemb); //((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb; return size * nmemb;
} }
void sendAnnounce(std::string action, u16 clients, double uptime, std::string gameid, std::vector<ModSpec> m_mods) { void sendAnnounce(std::string action, const std::vector<std::string> & clients_names, double uptime, u32 game_time, std::string gameid, std::vector<ModSpec> mods) {
Json::Value server; Json::Value server;
if (action.size()) if (action.size())
server["action"] = action; server["action"] = action;
server["port"] = g_settings->get("port"); server["port"] = g_settings->get("port");
server["address"] = g_settings->get("server_address"); server["address"] = g_settings->get("server_address");
if (action != "delete") { if (action != "delete") {
server["name"] = g_settings->get("server_name"); server["name"] = g_settings->get("server_name");
@ -209,10 +209,15 @@ void sendAnnounce(std::string action, u16 clients, double uptime, std::string ga
server["damage"] = g_settings->get("enable_damage"); server["damage"] = g_settings->get("enable_damage");
server["password"] = g_settings->getBool("disallow_empty_password"); server["password"] = g_settings->getBool("disallow_empty_password");
server["pvp"] = g_settings->getBool("enable_pvp"); server["pvp"] = g_settings->getBool("enable_pvp");
server["clients"] = clients; server["clients"] = (int)clients_names.size();
server["clients_max"] = g_settings->get("max_users"); server["clients_max"] = g_settings->get("max_users");
if (uptime >=1) server["uptime"] = (int)uptime; server["clients_list"] = Json::Value(Json::arrayValue);
if (gameid!="") server["gameid"] = gameid; for(u32 i = 0; i < clients_names.size(); ++i) {
server["clients_list"].append(clients_names[i]);
}
if (uptime >= 1) server["uptime"] = (int)uptime;
if (gameid != "") server["gameid"] = gameid;
if (game_time >= 1) server["game_time"] = game_time;
} }
if(server["action"] == "start") { if(server["action"] == "start") {
@ -220,8 +225,8 @@ void sendAnnounce(std::string action, u16 clients, double uptime, std::string ga
server["rollback"] = g_settings->getBool("enable_rollback_recording"); server["rollback"] = g_settings->getBool("enable_rollback_recording");
server["liquid_finite"] = g_settings->getBool("liquid_finite"); server["liquid_finite"] = g_settings->getBool("liquid_finite");
server["mapgen"] = g_settings->get("mg_name"); server["mapgen"] = g_settings->get("mg_name");
server["mods"] = Json::Value(Json::arrayValue); server["mods"] = Json::Value(Json::arrayValue);
for(std::vector<ModSpec>::iterator m = m_mods.begin(); m != m_mods.end(); m++) { for(std::vector<ModSpec>::iterator m = mods.begin(); m != mods.end(); m++) {
server["mods"].append(m->name); server["mods"].append(m->name);
} }
actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl; actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;

View File

@ -40,7 +40,9 @@ namespace ServerList
std::vector<ServerListSpec> deSerializeJson(std::string liststring); std::vector<ServerListSpec> deSerializeJson(std::string liststring);
std::string serializeJson(std::vector<ServerListSpec>); std::string serializeJson(std::vector<ServerListSpec>);
#if USE_CURL #if USE_CURL
void sendAnnounce(std::string action = "", u16 clients = 0, double uptime = 0, std::string gameid = "", std::vector<ModSpec> m_mods = std::vector<ModSpec>()); void sendAnnounce(std::string action = "", const std::vector<std::string> & clients_names = std::vector<std::string>(),
double uptime = 0, u32 game_time = 0,std::string gameid = "",
std::vector<ModSpec> mods = std::vector<ModSpec>());
#endif #endif
} //ServerList namespace } //ServerList namespace

View File

@ -8,4 +8,5 @@
<body><div id="servers_table"></div></body> <body><div id="servers_table"></div></body>
</html> </html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>//var master = {root: 'http://servers.minetest.net/', limit:10, clients_min:1, no_flags:1, no_ping:1, no_uptime:1};</script>
<script src="list.js"></script> <script src="list.js"></script>

View File

@ -1,7 +1,13 @@
var master_root, output_to; var master_root, output_to;
var master;
if (!master) master = {
root: master_root,
output: output_to
};
function e(s) { function e(s) {
if (typeof s === "undefined") s = ''; if (typeof s === "undefined") s = '';
if (typeof s === "number") return s;
return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); //mc" return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); //mc"
} }
@ -41,55 +47,88 @@ function human_time(t, abs) {
function success(r) { function success(r) {
if (!r || !r.list) return; if (!r || !r.list) return;
var h = '<table class="mts_table"><tr class="mts_head"><th>ip[:port]</th><th>clients/max</th><th>version gameid mapgen</th><th>name</th><th>description</th><th>flags</th><th>uptime</th><th>ping</th></tr>'; var h = '';
for (var i = 0; i < r.list.length; ++i) { if (!master.no_total && r.total)
var s = r.list[i]; h += '<div class="mts_total">Now players: ' + r.total.clients + ' servers: ' + r.total.servers + '</div>';
if (!s) continue; if (!master.no_total_max && r.total_max)
if (/:/.test(s.address)) s.address = '[' + s.address + ']'; h += '<div class="mts_total_max">Max players: ' + r.total_max.clients + ' servers: ' + r.total_max.servers + '</div>';
h += '<tr class="mts_row">'; h += '<table class="mts_table">';
h += '<td class="mts_address">' + e(s.address) + (s.port != 30000 ? (':' + e(s.port)) : '') + '</td>'; if (r.list.length) {
h += '<td class="mts_clients">' + e(s.clients) + (s.clients_max ? '/' + e(s.clients_max) : '') + (s.clients_top ? ', ' + e(s.clients_top) : '') + '</td>'; h += '<tr class="mts_head">';
var mods = 0; if (!master.no_address) h += '<th>ip[:port]</th>';
if (s.mods && jQuery.isArray(s.mods)) { if (!master.no_clients) h += '<th>players/max</th>';
mods = s.mods.length; if (!master.no_version) h += '<th>version gameid mapgen</th>';
} if (!master.no_name) h += '<th>name</th>';
h += '<td class="mts_version' + (mods ? ' mts_ismods' : '') + '">' + e(s.version) + ' ' + e(s.gameid) + ' ' + e(s.mapgen); if (!master.no_description) h += '<th>description</th>';
if (mods) { if (!master.no_flags) h += '<th>flags</th>';
h += '<div class="mts_mods">Mods ('+mods+'):<br/>'; if (!master.no_uptime) h += '<th>uptime age</th>';
for (m in s.mods) { if (!master.no_ping) h += '<th>ping</th>';
h += s.mods[m] + '<br/>';
}
h += '</div>';
}
h += '</td>';
h += '<td class="mts_url">';
if (s.url) h += '<a href="' + e(s.url) + '">';
h += e(s.name || s.url);
if (s.url) h += '</a>';
h += '</td>';
h += '<td class="mts_description">' + e(s.description) + '</td>';
h += '<td class="mts_flags">' +
(s.password ? 'Pwd ' : '') +
(s.creative ? 'Cre ' : '') +
(s.damage ? 'Dmg ' : '') +
(s.pvp ? 'Pvp ' : '') +
(s.dedicated ? 'Ded ' : '') +
(s.rollback ? 'Rol ' : '') +
(s.liquid_finite ? 'Liq ' : '') +
'</td>';
if (!s.start || s.start < 0) s.start = 0;
h += '<td class="mts_time">' + (s.uptime ? human_time(s.uptime, 1) : s.start ? human_time(s.start) : '') + '</td>';
h += '<td class="mts_ping">' + (s.ping ? parseFloat(s.ping).toFixed(3) * 1000 : '') + '</td>';
h += '</tr>'; h += '</tr>';
} }
h += '</table>' var count = 0;
jQuery(output_to || '#servers_table').html(h); for (var i = 0; i < r.list.length; ++i) {
if (++count > master.limit && master.limit) break;
var s = r.list[i];
if (!s) continue;
if (master.clients_min && s.clients < master.clients_min) continue;
if (/:/.test(s.address)) s.address = '[' + s.address + ']';
h += '<tr class="mts_row">';
if (!master.no_address) h += '<td class="mts_address">' + e(s.address) + (s.port != 30000 ? (':' + e(s.port)) : '') + '</td>';
if (!master.no_clients) {
h += '<td class="mts_clients' + (s.clients && s.clients_list ? ' mts_is_clients' : '') + '">';
if (!master.no_clients_list && s.clients && s.clients_list) {
h += '<div class="mts_clients_list">Players (' + e(s.clients) + '):<br/>';
for (var ii in s.clients_list)
h += e(s.clients_list[ii]) + '<br/>';
h += '</div>';
}
h += e(s.clients) + (s.clients_max ? '/' + e(s.clients_max) : '') + (s.clients_top ? ', ' + e(s.clients_top) : '') + '</td>';
}
var mods = 0;
if (s.mods && jQuery.isArray(s.mods))
mods = s.mods.length;
if (!master.no_version) {
h += '<td class="mts_version' + (mods ? ' mts_is_mods' : '') + '">' + e(s.version) + ' ' + e(s.gameid) + ' ' + e(s.mapgen);
if (!master.no_mods && mods) {
h += '<div class="mts_mods">Mods (' + mods + '):<br/>';
for (var ii in s.mods)
h += e(s.mods[ii]) + '<br/>';
h += '</div>';
}
h += '</td>';
}
if (!master.no_name) {
h += '<td class="mts_url">';
if (s.url) h += '<a href="' + e(s.url) + '">';
h += e(s.name || s.url);
if (s.url) h += '</a>';
h += '</td>';
}
if (!master.no_description) h += '<td class="mts_description">' + e(s.description) + '</td>';
if (!master.no_flags) {
h += '<td class="mts_flags">' +
(s.password ? 'Pwd ' : '') +
(s.creative ? 'Cre ' : '') +
(s.damage ? 'Dmg ' : '') +
(s.pvp ? 'Pvp ' : '') +
(s.dedicated ? 'Ded ' : '') +
(s.rollback ? 'Rol ' : '') +
(s.liquid_finite ? 'Liq ' : '') +
'</td>';
}
if (!s.start || s.start < 0) s.start = 0;
if (!master.no_uptime) h += '<td class="mts_uptime">' + (s.uptime ? human_time(s.uptime, 1) : s.start ? human_time(s.start) : '') + (s.game_time ? ' ' + human_time(s.game_time, 1) : '') + '</td>';
if (!master.no_ping) h += '<td class="mts_ping">' + (s.ping ? parseFloat(s.ping).toFixed(3) * 1000 : '') + '</td>';
h += '</tr>';
}
h += '</table>';
if (master.clients_min || master.limit)
h += '<a href="#" onclick="delete master.limit;delete master.clients_min; get(1);">more...</a>';
jQuery(master.output || '#servers_table').html(h);
} }
function get() { function get(refresh) {
jQuery.getJSON((master_root || '') + 'list', success); jQuery.getJSON((master.root || '') + 'list', success);
setTimeout(get, 60000); if (!refresh && !master.no_refresh) setTimeout(get, 60000);
} }
get(); get();

View File

@ -3,7 +3,7 @@
=info =info
install: install:
cpan JSON JSON::XS cpan JSON JSON::XS
touch list_full list touch list_full list log.log
chmod a+rw list_full list log.log chmod a+rw list_full list log.log
freebsd: freebsd:
@ -48,6 +48,7 @@ use strict;
no strict qw(refs); no strict qw(refs);
use warnings "NONFATAL" => "all"; use warnings "NONFATAL" => "all";
no warnings qw(uninitialized); no warnings qw(uninitialized);
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
use utf8; use utf8;
use Socket; use Socket;
BEGIN { BEGIN {
@ -72,7 +73,7 @@ our %config = (
list_full => $root_path . 'list_full', list_full => $root_path . 'list_full',
list_pub => $root_path . 'list', list_pub => $root_path . 'list',
log => $root_path . 'log.log', log => $root_path . 'log.log',
time_purge => 86400 * 30, time_purge => 86400 * 1,
time_alive => 650, time_alive => 650,
source_check => 1, source_check => 1,
ping_timeout => 3, ping_timeout => 3,
@ -190,11 +191,13 @@ sub request (;$) {
$param->{$_} = $j->{$_} for keys %$j; $param->{$_} = $j->{$_} for keys %$j;
delete $param->{json}; delete $param->{json};
} }
#printlog 'recv', Dumper $param;
if (%$param) { if (%$param) {
s/^false$// for values %$param; s/^false$// for values %$param;
$param->{ip} = $r->{REMOTE_ADDR}; $param->{ip} = $r->{REMOTE_ADDR};
$param->{ip} =~ s/^::ffff://; $param->{ip} =~ s/^::ffff://;
for (@{$config{blacklist}}) { for (@{$config{blacklist}}) {
#printlog("blacklist", $param->{ip} ~~ $_) if $config{debug};
return if $param->{ip} ~~ $_; return if $param->{ip} ~~ $_;
} }
$param->{address} ||= $param->{ip}; $param->{address} ||= $param->{ip};
@ -223,6 +226,7 @@ sub request (;$) {
$param->{ping} = $duration if $pingret; $param->{ping} = $duration if $pingret;
printlog " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug}; printlog " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug};
} }
return if !$param->{ping};
} }
my $list = read_json($config{list_full}) || {}; my $list = read_json($config{list_full}) || {};
printlog "readed[$config{list_full}] list size=", scalar @{$list->{list}}; printlog "readed[$config{list_full}] list size=", scalar @{$list->{list}};
@ -232,6 +236,7 @@ sub request (;$) {
$param->{time} ||= int time; $param->{time} ||= int time;
$param->{start} = $param->{action} ~~ 'start' ? $param->{time} : $old->{start} || $param->{time}; $param->{start} = $param->{action} ~~ 'start' ? $param->{time} : $old->{start} || $param->{time};
delete $param->{start} if $param->{off}; delete $param->{start} if $param->{off};
$param->{clients} ||= scalar @{$param->{clients_list}} if ref $param->{clients_list} eq 'ARRAY';
$param->{first} ||= $old->{first} || $old->{time} || $param->{time}; $param->{first} ||= $old->{first} || $old->{time} || $param->{time};
$param->{clients_top} = $old->{clients_top} if $old->{clients_top} > $param->{clients}; $param->{clients_top} = $old->{clients_top} if $old->{clients_top} > $param->{clients};
$param->{clients_top} ||= $param->{clients} || 0; $param->{clients_top} ||= $param->{clients} || 0;
@ -239,9 +244,12 @@ sub request (;$) {
for (qw(dedicated rollback liquid_finite mapgen mods)) { for (qw(dedicated rollback liquid_finite mapgen mods)) {
$param->{$_} ||= $old->{$_} if $old->{$_} and !($param->{action} ~~ 'start'); $param->{$_} ||= $old->{$_} if $old->{$_} and !($param->{action} ~~ 'start');
} }
$param->{pop_n} = $old->{pop_n} + 1;
$param->{pop_c} = $old->{pop_c} + $param->{clients};
$param->{pop_v} = $param->{pop_c} / $param->{pop_n};
delete $param->{action}; delete $param->{action};
$listk->{$param->{key}} = $param; $listk->{$param->{key}} = $param;
#printlog Dumper $param; #printlog 'write', Dumper $param if $config{debug};
$list->{list} = [grep { $_->{time} > time - $config{time_purge} } values %$listk]; $list->{list} = [grep { $_->{time} > time - $config{time_purge} } values %$listk];
file_rewrite($config{list_full}, JSON->new->encode($list)); file_rewrite($config{list_full}, JSON->new->encode($list));
printlog "writed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug}; printlog "writed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug};
@ -250,6 +258,13 @@ sub request (;$) {
grep { $_->{time} > time - $config{time_alive} and !$_->{off} and (!$config{ping} or !$config{pingable} or $_->{ping}) } grep { $_->{time} > time - $config{time_alive} and !$_->{off} and (!$config{ping} or !$config{pingable} or $_->{ping}) }
@{$list->{list}} @{$list->{list}}
]; ];
$list->{total} = {clients => 0, servers => 0};
for (@{$list->{list}}) {
$list->{total}{clients} += $_->{clients};
++$list->{total}{servers};
}
$list->{total_max}{clients} = $list->{total}{clients} if $list->{total_max}{clients} < $list->{total}{clients};
$list->{total_max}{servers} = $list->{total}{servers} if $list->{total_max}{servers} < $list->{total}{servers};
file_rewrite($config{list_pub}, JSON->new->encode($list)); file_rewrite($config{list_pub}, JSON->new->encode($list));
printlog "writed[$config{list_pub}] list size=", scalar @{$list->{list}} if $config{debug}; printlog "writed[$config{list_pub}] list size=", scalar @{$list->{list}} if $config{debug};
} }

View File

@ -13,7 +13,7 @@ div#table table {
width: 100%; width: 100%;
} }
.mts_mods { .mts_mods, .mts_clients_list {
visibility: hidden; visibility: hidden;
border:gray solid 1px; border:gray solid 1px;
position:absolute; position:absolute;
@ -22,11 +22,11 @@ div#table table {
padding:.5em; padding:.5em;
} }
.mts_version:hover .mts_mods { .mts_version:hover .mts_mods, .mts_clients:hover .mts_clients_list {
visibility: visible; visibility: visible;
} }
.mts_version.mts_ismods { .mts_version.mts_is_mods, .mts_clients.mts_is_clients {
text-decoration:underline; text-decoration:underline;
text-decoration-style:dashed; text-decoration-style:dashed;
} }