]> git.lizzy.rs Git - minetest.git/blob - util/master/list.js
4e2104bc88a8f2567e12e030d99aa910a1a993cb
[minetest.git] / util / master / list.js
1 var master_root, output_to;
2 var master;
3 if (!master) master = {
4     root: master_root,
5     output: output_to
6 };
7
8 function e(s) {
9     if (typeof s === "undefined") s = '';
10     if (typeof s === "number") return s;
11     return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); //mc"
12 }
13
14 function human_time(t, abs) {
15     var n = 's';
16     if (!t || t < 0) t = 0;
17     var f = 0;
18     var s = parseInt(abs ? (t || 0) : (new Date().getTime() / 1000 - (t || 0)));
19     if (!s || s <= 0) s = 0;
20     if (s == 0) return 'now';
21     if (s >= 60) {
22         s /= 60;
23         n = 'm';
24         if (s >= 60) {
25             s /= 60;
26             n = 'h';
27             f = 1;
28             if (s >= 24) {
29                 s /= 24;
30                 n = 'd';
31                 f = 1;
32                 if (s >= 30) {
33                     s /= 30;
34                     n = 'M';
35                     f = 1;
36                     if (s >= 12) {
37                         s /= 12;
38                         n = 'y';
39                         f = 1;
40                     }
41                 }
42             }
43         }
44     }
45     return ((f ? parseFloat(s).toFixed(1) : parseInt(s)) + n);
46 }
47
48 function success(r) {
49     if (!r || !r.list) return;
50     var h = '';
51     if (!master.no_total && r.total && r.total_max)
52         h += '<div class="mts_total">Players: ' + r.total.clients + ('/' + r.total_max.clients) + ' servers: ' + r.total.servers + ('/' + r.total_max.servers) + '</div>';
53     h += '<table class="mts_table">';
54     if (r.list.length) {
55         h += '<tr class="mts_head">';
56         if (!master.no_address) h += '<th>ip[:port]</th>';
57         if (!master.no_clients) h += '<th>players/max</th>';
58         if (!master.no_version) h += '<th>version gameid mapgen</th>';
59         if (!master.no_name) h += '<th>name</th>';
60         if (!master.no_description) h += '<th>description</th>';
61         if (!master.no_flags) h += '<th>flags</th>';
62         if (!master.no_uptime) h += '<th>uptime age</th>';
63         if (!master.no_ping) h += '<th>ping</th>';
64         h += '</tr>';
65     }
66     var count = 0;
67     for (var i = 0; i < r.list.length; ++i) {
68         if (++count > master.limit && master.limit) break;
69         var s = r.list[i];
70         if (!s) continue;
71         if (master.clients_min && s.clients < master.clients_min) continue;
72         if (/:/.test(s.address)) s.address = '[' + s.address + ']';
73         h += '<tr class="mts_row">';
74         if (!master.no_address) h += '<td class="mts_address">' + e(s.address) + (s.port != 30000 ? (':' + e(s.port)) : '') + '</td>';
75         if (!master.no_clients) {
76             h += '<td class="mts_clients' + (s.clients && s.clients_list ? ' mts_is_clients' : '') + '">';
77             if (!master.no_clients_list && s.clients && s.clients_list) {
78                 h += '<div class="mts_clients_list">Players (' + e(s.clients) + '):<br/>';
79                 for (var ii in s.clients_list)
80                     h += e(s.clients_list[ii]) + '<br/>';
81                 h += '</div>';
82             }
83             h += e(s.clients) + (s.clients_max ? '/' + e(s.clients_max) : '') + (s.clients_top ? ', ' + e(s.clients_top) : '') + '</td>';
84         }
85         var mods = 0;
86         if (s.mods && jQuery.isArray(s.mods))
87             mods = s.mods.length;
88         if (!master.no_version) {
89             h += '<td class="mts_version' + (mods ? ' mts_is_mods' : '') + '">' + e(s.version) + ' ' + e(s.gameid) + ' ' + e(s.mapgen);
90             if (!master.no_mods && mods) {
91                 h += '<div class="mts_mods">Mods (' + mods + '):<br/>';
92                 for (var ii in s.mods)
93                     h += e(s.mods[ii]) + '<br/>';
94                 h += '</div>';
95             }
96             h += '</td>';
97         }
98         if (!master.no_name) {
99             h += '<td class="mts_url">';
100             if (s.url) h += '<a href="' + e(s.url) + '">';
101             h += e(s.name || s.url);
102             if (s.url) h += '</a>';
103             h += '</td>';
104         }
105         if (!master.no_description) h += '<td class="mts_description">' + e(s.description) + '</td>';
106         if (!master.no_flags) {
107             h += '<td class="mts_flags">' +
108                 (s.password ? 'Pwd ' : '') +
109                 (s.creative ? 'Cre ' : '') +
110                 (s.damage ? 'Dmg ' : '') +
111                 (s.pvp ? 'Pvp ' : '') +
112                 (s.dedicated ? 'Ded ' : '') +
113                 (s.rollback ? 'Rol ' : '') +
114                 (s.liquid_finite ? 'Liq ' : '') +
115                 '</td>';
116         }
117         if (!s.start || s.start < 0) s.start = 0;
118         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>';
119         if (!master.no_ping) h += '<td class="mts_ping">' + (s.ping ? parseFloat(s.ping).toFixed(3) * 1000 : '') + '</td>';
120         h += '</tr>';
121     }
122     h += '</table>';
123     if (master.clients_min || master.limit)
124         h += '<a href="#" onclick="delete master.limit;delete master.clients_min; get(1);">more...</a>';
125     jQuery(master.output || '#servers_table').html(h);
126 }
127
128 function get(refresh) {
129     jQuery.getJSON((master.root || '') + 'list', success);
130     if (!refresh && !master.no_refresh) setTimeout(get, 60000);
131 }
132 get();