ut-serverstats
Code (php)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
//define aettings
//this is only if you want that the users can enter ip+host by their self...
//ip of the server
if(empty($_GET['host'])){
//default server
$host = '213.230.209.202';
}else{
$host = $_GET['host'];
}
if(empty($_GET['port'])){
//querypoort of the server. always gameserver port + 1
$port = 7778;
}else{
$port = $_GET['port'];
}
//backgroud color
$background = 'white';
//no edit afher
function getInfo($host,$port) {
if(is_int($port)){
$sock = @fsockopen("udp://" . $host, $port, $errno, $errstr,4);
//Check if we have a socket open, if not, display error message
if (!$sock) {
return '';
}else
{
fputs($sock,"\\status\\\player_property\Health\\\game_property\ElapsedTime\\\game_property\RemainingTime\\");
$gotfinal = False;
$data = "";
//Set starttime, for possible loop expiration, so the server doesn't get too much work.
$starttime = Time();
//Loop until final packet has been received.
while(!($gotfinal == True || feof($sock))) {
//Get data
if(($buf = fgetc($sock)) == FALSE) {
usleep(100); // wait for additional data? :S whatever
}
//Add to databuffer
$data .= $buf;
//Check if final item (queryid) has been received
if (strpos($data,"final\\") != False) {
$gotfinal = True;
}
//Protect webserver against massive loop.
if ((Time() - $starttime) > 5) {
echo "Data receiving took too long. Cancelled.";
$gotfinal = True;
}
}
//Close socket
fclose ($sock);
//Split chunks by \
}
return $data;
}else{
return '';
}
}
function parseInfo($info) {
$retval = array();
if ($info != '') {
$key = strtok($info, '\\');
while ($key) {
$val = strtok('\\');
if ($val !== false){
if(empty($val)){
$retval[$key] = "0";
}else{
$retval[$key] = $val;
}
}
else
{
break;
}
$key = strtok('\\');
}
}
return $retval;
}
function get_minutes($seconds){
//ut return remaining/espalated time in seconds (eg 1159) we want to read it as 19:59
$timemins = intval($seconds / 60);
//% means modulo
$timesecs = ($seconds % 60);
$Reqlength = 2; //Amount of digits we need
//make the text 2 digets
if ($Reqlength-strlen($timemins) > 0) $timemins = str_repeat("0",($Reqlength-strlen($timemins))) . $timemins;
if ($Reqlength-strlen($timesecs) > 0) $timesecs = str_repeat("0",($Reqlength-strlen($timesecs))) . $timesecs;
return $timemins . ":" . $timesecs;
}
//game tupe in ut wll often return as Teamgameplus. To create a better/understand format for us
function get_true_gamename($game){
switch ($game){
//dm
case 'DeathMatchPlus':
$out = 'Death Match';
break;
//tdm
case 'TeamGamePlus':
$out = 'Team Death Match';
break;
//ctf
case 'CTFGame':
$out = 'Capture The Flag';
break;
//dom
case 'Domination':
$out = 'Domination';
break;
//as
case 'Assault':
$out = 'Assault';
break;
//LMS
case 'LastManStanding':
$out = 'Last Man Standing';
break;
default:
$out = $game;
break;
}
return $out;
}
function team($teamid,$teamids){
//team red == 0
//tram blue == 1
//team green == 2
//team yellow == 3
//spectator/none color == 255 == background collor
$colors = array("red","blue","green","yellow");
if ($teamid < 4)
{
$teamids = $colors[$teamid];
}
return $teamids;
}
//parse info and recive the info
$stats = parseInfo(getInfo($host,$port));
//show image in the right corner
echo '<div style="float:right;">';
if(file_exists('images/'.$stats['mapname'].'.jpg')){
echo '<img src="images/'.$stats['mapname'].'.jpg" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';
}else{
echo '<img src="images/none.gif" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';
}
echo '</div>';
echo '<strong>Serverinfo</strong><br />';
echo 'Servername: '.$stats['hostname'].'<br />';
echo 'Gameserver version: '.$stats['gamever'].'<br />';
echo 'Required version: '.$stats['minnetver'].'<br />';
echo '<strong>Settings</strong><br />';
echo 'Gametype: '.get_true_gamename($stats['gametype']).'<br />';
echo 'Mapname: '.$stats['mapname'].'<br />';
echo 'Maptitle: '.$stats['maptitle'].'<br />';
echo 'Players: '.$stats['numplayers'].'/'.$stats['maxplayers'].'<br />';
echo 'Mutators: '.$stats['mutators'].'<br />';
echo 'Timelimit: '.$stats['timelimit'].' minutes<br />';
echo 'Fraglimit: '.$stats['goalteamscore'].'<br />';
echo 'Friendly Fire: '.$stats['friendlyfire'].'<br />';
echo 'Tournament: '.$stats['tournament'].'<br />';
echo 'Gamestyle: '.$stats['gamestyle'].'<br />';
echo 'Elapsed Time: '.get_minutes($stats['ElapsedTime']).'<br />';
echo 'Remaining Time: '.get_minutes($stats['RemainingTime']).'<br />';
echo '<strong>Playerlist:</strong>';
//echo table
echo '<table>
<tr><td>Player name:</td><td>Frags</td><td>Ping</td><tr>';
for($i = 0;!empty($stats['player_'.$i]);$i++){
echo '<tr style="background-color:'.team($stats['team_'.$i],$background).'"><td>'.$stats['player_'.$i].'</td><td>'.$stats['frags_'.$i].'</td><td>'.$stats['ping_'.$i].'</td></tr>';
}
echo '</table>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">
IP: <input type="text" name="host"/><br />
Queryport (Gameport + 1): <input type="text" name="port"/><br />
<input type="submit" value="query server"/>
</form>';
?>
//define aettings
//this is only if you want that the users can enter ip+host by their self...
//ip of the server
if(empty($_GET['host'])){
//default server
$host = '213.230.209.202';
}else{
$host = $_GET['host'];
}
if(empty($_GET['port'])){
//querypoort of the server. always gameserver port + 1
$port = 7778;
}else{
$port = $_GET['port'];
}
//backgroud color
$background = 'white';
//no edit afher
function getInfo($host,$port) {
if(is_int($port)){
$sock = @fsockopen("udp://" . $host, $port, $errno, $errstr,4);
//Check if we have a socket open, if not, display error message
if (!$sock) {
return '';
}else
{
fputs($sock,"\\status\\\player_property\Health\\\game_property\ElapsedTime\\\game_property\RemainingTime\\");
$gotfinal = False;
$data = "";
//Set starttime, for possible loop expiration, so the server doesn't get too much work.
$starttime = Time();
//Loop until final packet has been received.
while(!($gotfinal == True || feof($sock))) {
//Get data
if(($buf = fgetc($sock)) == FALSE) {
usleep(100); // wait for additional data? :S whatever
}
//Add to databuffer
$data .= $buf;
//Check if final item (queryid) has been received
if (strpos($data,"final\\") != False) {
$gotfinal = True;
}
//Protect webserver against massive loop.
if ((Time() - $starttime) > 5) {
echo "Data receiving took too long. Cancelled.";
$gotfinal = True;
}
}
//Close socket
fclose ($sock);
//Split chunks by \
}
return $data;
}else{
return '';
}
}
function parseInfo($info) {
$retval = array();
if ($info != '') {
$key = strtok($info, '\\');
while ($key) {
$val = strtok('\\');
if ($val !== false){
if(empty($val)){
$retval[$key] = "0";
}else{
$retval[$key] = $val;
}
}
else
{
break;
}
$key = strtok('\\');
}
}
return $retval;
}
function get_minutes($seconds){
//ut return remaining/espalated time in seconds (eg 1159) we want to read it as 19:59
$timemins = intval($seconds / 60);
//% means modulo
$timesecs = ($seconds % 60);
$Reqlength = 2; //Amount of digits we need
//make the text 2 digets
if ($Reqlength-strlen($timemins) > 0) $timemins = str_repeat("0",($Reqlength-strlen($timemins))) . $timemins;
if ($Reqlength-strlen($timesecs) > 0) $timesecs = str_repeat("0",($Reqlength-strlen($timesecs))) . $timesecs;
return $timemins . ":" . $timesecs;
}
//game tupe in ut wll often return as Teamgameplus. To create a better/understand format for us
function get_true_gamename($game){
switch ($game){
//dm
case 'DeathMatchPlus':
$out = 'Death Match';
break;
//tdm
case 'TeamGamePlus':
$out = 'Team Death Match';
break;
//ctf
case 'CTFGame':
$out = 'Capture The Flag';
break;
//dom
case 'Domination':
$out = 'Domination';
break;
//as
case 'Assault':
$out = 'Assault';
break;
//LMS
case 'LastManStanding':
$out = 'Last Man Standing';
break;
default:
$out = $game;
break;
}
return $out;
}
function team($teamid,$teamids){
//team red == 0
//tram blue == 1
//team green == 2
//team yellow == 3
//spectator/none color == 255 == background collor
$colors = array("red","blue","green","yellow");
if ($teamid < 4)
{
$teamids = $colors[$teamid];
}
return $teamids;
}
//parse info and recive the info
$stats = parseInfo(getInfo($host,$port));
//show image in the right corner
echo '<div style="float:right;">';
if(file_exists('images/'.$stats['mapname'].'.jpg')){
echo '<img src="images/'.$stats['mapname'].'.jpg" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';
}else{
echo '<img src="images/none.gif" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';
}
echo '</div>';
echo '<strong>Serverinfo</strong><br />';
echo 'Servername: '.$stats['hostname'].'<br />';
echo 'Gameserver version: '.$stats['gamever'].'<br />';
echo 'Required version: '.$stats['minnetver'].'<br />';
echo '<strong>Settings</strong><br />';
echo 'Gametype: '.get_true_gamename($stats['gametype']).'<br />';
echo 'Mapname: '.$stats['mapname'].'<br />';
echo 'Maptitle: '.$stats['maptitle'].'<br />';
echo 'Players: '.$stats['numplayers'].'/'.$stats['maxplayers'].'<br />';
echo 'Mutators: '.$stats['mutators'].'<br />';
echo 'Timelimit: '.$stats['timelimit'].' minutes<br />';
echo 'Fraglimit: '.$stats['goalteamscore'].'<br />';
echo 'Friendly Fire: '.$stats['friendlyfire'].'<br />';
echo 'Tournament: '.$stats['tournament'].'<br />';
echo 'Gamestyle: '.$stats['gamestyle'].'<br />';
echo 'Elapsed Time: '.get_minutes($stats['ElapsedTime']).'<br />';
echo 'Remaining Time: '.get_minutes($stats['RemainingTime']).'<br />';
echo '<strong>Playerlist:</strong>';
//echo table
echo '<table>
<tr><td>Player name:</td><td>Frags</td><td>Ping</td><tr>';
for($i = 0;!empty($stats['player_'.$i]);$i++){
echo '<tr style="background-color:'.team($stats['team_'.$i],$background).'"><td>'.$stats['player_'.$i].'</td><td>'.$stats['frags_'.$i].'</td><td>'.$stats['ping_'.$i].'</td></tr>';
}
echo '</table>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">
IP: <input type="text" name="host"/><br />
Queryport (Gameport + 1): <input type="text" name="port"/><br />
<input type="submit" value="query server"/>
</form>';
?>