class cstrike_statistics
{
protected $hostname;
protected $port;
protected $playerinfo;
protected $servervars;
protected $serverrules;
protected $errmsg;
protected $timeout;
protected $waittime;
protected $serverCommands;
public function cstrike_statistics()
{
$this->port = 27015;
$this->timeout = 15;
$this->waittime = 500;
$this->serverCommands["players"] = "\xFF\xFF\xFF\xFF\x55";
$this->serverCommands["rules"] = "\xFF\xFF\xFF\xFF\x56";
$this->serverCommands["info"] = "\xFF\xFF\xFF\xFF\x54";
}
public function setHostname($hostname)
{
$this->hostname = $hostname;
}
public function setPort($port)
{
$this->port = $port;
}
private function timenow()
{
return doubleval(ereg_replace('^0.([0-9]*) ([0-9]*)$', '2.1', microtime()));
}
private function getServerData($command)
{
$serverdata = "";
$serverdatalen = 0;
if (!$cssocket=@fsockopen("udp://".$this->hostname, $this->port, $errnr))
{
$this->errmsg="Unable to connect to target host";
return "";
}
else
{
socket_set_blocking($cssocket, true);
@socket_set_timeout($cssocket, 0, 500000);
fwrite($cssocket, $command, strlen($command));
$starttime = $this->timenow();
do
{
$serverdata .= fgetc($cssocket);
$serverdatalen++;
$socketstatus = socket_get_status($cssocket);
if ($this->timenow() > ($starttime + $this->waittime))
{
$this->errmsg = "Connection timed out";
fclose($cssocket);
return "";
}
} while ($socketstatus["unread_bytes"]);
fclose($cssocket);
return $serverdata;
}
}
private function getnextstring(&$data)
{
$tmp = "";
$counter = 0;
while (ord($data[$counter++])!=0) $tmp .= $data[$counter-1];
$data = substr($data, strlen($tmp)+1);
return $tmp;
}
private function getnextbytevalue(&$data)
{
$tmp = ord($data[0]);
$data = substr($data, 1);
return $tmp;
}
private function getnextfragvalue(&$data)
{
$frags = ord($data[0])+(ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
if ($frags>=4294967294) $frags-=4294967296;
$data = substr($data, 4);
return $frags;
}
private function getnextplaytime(&$data)
{
$decnumber = ord($data[0])+(ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
$binnumber = base_convert($decnumber, 10, 2);
while (strlen($binnumber) < 32) $binnumber = "0".$binnumber;
$exp = abs(base_convert(substr($binnumber, 1, 8), 2, 10)) - 127;
if (substr($binnumber, 0, 1) == "1") $exp = 0 - $exp;
$man=1;
$manadd=0.5;
for ($counter=9; $counter<32; $counter++)
{
if (substr($binnumber, $counter, 1) == "1") $man += $manadd;
$manadd = $manadd/2;
}
$time = round(pow(2, $exp) * $man);
$playtime = "";
if ($time>3600) $playtime = sprintf("%2dh", $time/3600);
$time %= 3600;
$playtime = $playtime.sprintf("%2dm", $time/60);
$time %= 60;
$playtime = $playtime.sprintf("%2ds", $time);
$data = substr($data, 5);
return $playtime;
}
public function getServerRules()
{
$serverdata = $this->getServerData($this->serverCommands["rules"]);
if (strlen($serverdata)<5) return false;
$rules = (ord($serverdata[5]))+(ord($serverdata[6])*256);
if ($rules!=0)
{
$serverdata = substr($serverdata, 7);
for ($i=1; $i<=$rules; $i++)
{
$this->serverrules[$this->getnextstring($serverdata)] = $this->getnextstring($serverdata);
}
return true;
}
else
{
return false;
}
}
public function getServerInfo()
{
$serverdata=$this->getServerData($this->serverCommands["info"]);
if (strlen($serverdata)<5) return false;
$serverdata=substr($serverdata,5);
$this->servervars["serveraddress"] = $this->getnextstring($serverdata);
$this->servervars["servername"] = $this->getnextstring($serverdata);
$this->servervars["mapname"] = $this->getnextstring($serverdata);
$this->servervars["game"] = $this->getnextstring($serverdata);
$this->servervars["gamename"] = $this->getnextstring($serverdata);
$this->servervars["currentplayers"] = $this->getnextbytevalue($serverdata);
$this->servervars["maxplayers"] = $this->getnextbytevalue($serverdata);
return true;
}
public function getServerPlayers()
{
$serverdata=$this->getServerData($this->serverCommands["players"]);
if (strlen($serverdata)<5) return false;
$players=ord($serverdata[5]);
$serverdata=substr($serverdata,7);
for ($i=1;$i<=$players;$i++)
{
$this->playerinfo[$i]["name"] = $this->getnextstring($serverdata);
$this->playerinfo[$i]["frags"] = $this->getnextfragvalue($serverdata);
$this->playerinfo[$i]["time"] = $this->getnextplaytime($serverdata);
}
return true;
}
public function getData()
{
$data = Array();
$this->getServerInfo();
$this->getServerPlayers();
$this->getServerRules();
$data['cstrike']['ServerInfo'] = $this->playerinfo;
$data['cstrike']['ServerVars'] = $this->servervars;
$data['cstrike']['ServerRules'] = $this->serverrules;
return $data;
}
}
$cstrike = new cstrike_statistics();
$cstrike->setHostname("206.212.247.10");
$cstrike->setPort(27015);
print_r($cstrike->getData());
?>
这一切的输出是:
Array
(
[cstrike] => Array
(
[ServerInfo] => Array
(
[0] => Array
(
[name] => I told Ya, i'll get Ya :)
[frags] => 103
[time] => 1h42m 0s
)
[1] => Array
(
[name] => Black is Back {UA}
[frags] => 59
[time] => 1h20m58s
)
[2] => Array
(
[name] => Chelsea FC
[frags] => 35
[time] => 1h 1m15s
)
[3] => Array
(
[name] => shadowd-m3
[frags] => 25
[time] => 14m46s
)
[4] => Array
(
[name] => .Clem
[frags] => 18
[time] => 19m27s
)
[5] => Array
(
[name] => minDsodom {dope herbs}
[frags] => 15
[time] => 12m50s
)
[6] => Array
(
[name] => Lasa
[frags] => 10
[time] => 24m16s
)
[7] => Array
(
[name] => memo
[frags] => 9
[time] => 40m49s
)
[8] => Array
(
[name] => :o
[frags] => 8
[time] => 9m19s
)
)
[ServerVars] => Array
(
[serveraddress] => 127.0.0.1:27015
[servername] => ]Si[ clan [nosteam|boosted] www.clan-si.com [chicago]
[mapname] => de_dust2
[game] => cstrike
[gamename] => NoSteam CS 1.6
[currentplayers] => 24
[maxplayers] => 32
)
[ServerRules] => Array
(
[] => ????Em
[_tutor_bomb_viewable_check_interval] => 0.5
[_tutor_debug_level] => 0
[_tutor_examine_time] => 0.5
[_tutor_hint_interval_time] => 10.0
[_tutor_look_angle] => 10
[_tutor_look_distance] => 200
[_tutor_message_character_display_time_coefficient] => 0.07
[_tutor_message_minimum_display_time] => 1
[_tutor_message_repeats] => 5
[_tutor_view_distance] => 1000
[AdThemes] => v1.8 by Vet(3TT3V)
[aesp_version] => 1.4_beta
[afkslay_version] => 2.5.2
[allow_spectators] => 1.0
[amx_client_languages] => 0
[amx_nextmap] => de_cpl_mill
[amx_timeleft] => 00:00
[amxbans_version] => 5.0
[amxmodx_version] => 1.8.0.3660
[booster_version] => 1.36
[coop] => 0
[deathmatch] => 1
[decalfrequency] => 30
[DoNotShootTheHostages] => 0.2
[dp_version] => 0.4.1
[edgefriction] => 2
[hostage_debug] => 0
[hostage_stop] => 0
[humans_join_team] => any
[map_spawns] => T(20) CT(20)
[max_queries_sec] => 1
[max_queries_sec_global] => 1
[max_queries_window] => 1
[metamod_version] => 1.19
[mp_allowmonsters] => 0
[mp_autokick] => 0
[mp_autoteambalance] => 1
[mp_buytime] => 0.5
[mp_c4timer] => 45
[mp_chattime] => 10
[mp_consistency] => 1
[mp_fadetoblack] => 0
[mp_flashlight] => 1
[mp_footsteps] => 1
[mp_forcecamera] => 0
[mp_forcechasecam] => 0
[mp_fragsleft] => 0
[mp_freezetime] => 3
[mp_friendlyfire] => 0
[mp_ghostfrequency] => 0.1
[mp_hostagepenalty] => 13
[mp_kickpercent] => 0.66
[mp_limitteams] => 2
[mp_logdetail] => 0
[mp_logfile] => 1
[mp_logmessages] => 1
[mp_mapvoteratio] => 1
[mp_maxrounds] => 0
[mp_mirrordamage] => 0
[mp_playerid] => 0
[mp_roundtime] => 2.3
[mp_startmoney] => 1000
[mp_timeleft] => 0
[mp_timelimit] => 0
[mp_tkpunish] => 0
)
)
)
我无法正确理解代码从数组中获取数据。
答案 0 :(得分:2)
在代码的最后(在课程结束之后),用以下代码替换代码:
$cstrike = new cstrike_statistics();
$cstrike->setHostname("206.212.247.10");
$cstrike->setPort(27015);
$result = $cstrike->getData();
//For maxplayers, do this:
echo "<h3>Maximum Players: ".$result['cstrike']['ServerVars']['maxplayers']."</h3>";
//For players do this:
foreach($result['cstrike']['ServerInfo'] as $players)
echo "<h4>Player Name: ".$players['name']."</h4>";
答案 1 :(得分:0)
可能最好阅读数组上的手册页:http://php.net/manual/en/language.types.array.php
答案 2 :(得分:0)
尝试以下方法:
$obj = $cstrike->getData();
echo "players count : " . count($obj['cstrike']['ServerInfo']) . "<br/>";
echo "Player1 Name: " . $obj['cstrike']['ServerInfo'][0]['name'] . "<br/>";