/
c_serverquery.php
107 lines (69 loc) · 1.94 KB
/
c_serverquery.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
<?
class ServerQuery {
function ServerQuery($ip, $qstatoutput) {
/*
$cmd = "qstat -nh -ts -P -hl2s $ip";
$text = `$cmd`;
$this->parse($text);
*/
$this->parse(& $qstatoutput[$ip]);
}
/*
function parse($text) {
$lines = explode("\n", $text);
*/
function parse($lines) {
// header looks like this:
// 218.214.224.105:27015 22/22 cs_italy 28 / 0 SwiftGames CS:Source Sydney
$this->curplayers = 0;
$this->maxplayers = 0;
$header = $lines[0];
if (strlen($header) == 0) {
$this->map = "";
$this->name = "DOWN";
return;
}
// this fixes the 22/22 when the denominatior is one digit: "5/ 8" to "5/8"
$header = str_replace("0/ ", "0/", $header);
// rem all spaces
while (strstr($header, " "))
$header = str_replace(" ", " ", $header);
$headbits = explode(" ", $header);
// server is up!
if ($headbits[1] == "DOWN" || $headbits[2] == "response") {
$this->map = "";
$this->name = "DOWN";
} else {
$this->map = strtolower($headbits[2]);
// get player stats
$t = explode('/', $headbits[1]);
$this->curplayers = $t[0];
$this->maxplayers = $t[1];
$this->name = "";
for ($i = 7; $i < sizeof($headbits); $i++)
$this->name .= $headbits[$i] . " ";
$this->name = trim($this->name);
unset($this->players);
for ($i = 1; $i < sizeof($lines); $i++) {
$line = $lines[$i];
$s = explode("\t", $line);
unset($player);
$player->frags = $s[1]+0;
$player->time = $s[2]+0;
$player->ename = $s[3];
$player->name = $s[4];
// convert hex player name to acsii
$o = '';
for ($j = 0; $j < strlen($player->ename); $j+=2) {
$o .= chr(hexdec($player->ename[$j]) * 16 + hexdec($player->ename[$j+1]));
}
$player->ename = $o;
$player->name = $o;
//echo $player->ename . " ----- ";
//echo $player->name . "\n";
$this->players[] = $player;
}
}
}
}
?>