01-13-2009, 07:41 PM
lib/class_ValveSourceServer.php
servertable.php
It works as intended, but why is it so slow??
PHP Code:
<?PHP
class ValveSourceServer
{
var $srv;
var $port;
function ValveSourceServer()
{
define("A2S_SERVERQUERY_GETCHALLENGE", "\xFF\xFF\xFF\xFF\x57");
// get the challenge !
define("A2S_INFO", "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00");
define("A2S_PLAYER", "\xFF\xFF\xFF\xFF\x55"); // requires Challenge suffix to work
define("A2S_RULES", "\xFF\xFF\xFF\xFF\x56"); // requires Challenge suffix to work
}
function ConnectToServer($server,$portno)
{
$this->srv=$server;
$this->port=$portno;
$this->A2S_connect();
}
function Disconnect()
{
$this->A2S_close();
}
function _getchallenge()
{
$data = $this->A2S_query(A2S_SERVERQUERY_GETCHALLENGE);
return substr($data,5);
}
function readString($string, &$i) {
$begin = $i;
$strlen = strlen($string);
for ($i; ($i < $strlen) && ($string{$i} != chr(0)); $i++);
$result = substr($string, $begin, $i-$begin);
$i++;
return $result;
}
function A2S_connect()
{
$this->connection = fsockopen("udp://".$this->srv, $this->port, $errorno, $errorstr, 1);
if(!$this->connection){ die("ERROR: $errorno $errorstr"); }
}
function A2S_query($query)
{
fwrite($this->connection, $query, strlen($query));
return fread($this->connection, 4096);
}
function A2S_players()
{
$challenge = $this->_getchallenge();
$buffer = $this->A2S_query(A2S_PLAYER.$challenge);
$i = 5;
$num_players = ord($buffer{$i++});
if($num_players > 0) {
for($p = 0; $p < $num_players; $p++) {
if(!empty($buffer{$i+1})) {
$data[$p]['index'] = ord($buffer{$i++});
$data[$p]['name'] = $this->readString($buffer, $i);
$frags = unpack("L", substr($buffer, $i, $i+4)); $i+=4;
$data[$p]['frags'] = $frags[1];
$time = unpack("f", substr($buffer, $i, $i+4)); $i+=4;
$time = mktime(0, 0, $time[1]);
$time = date("H:i:s", $time);
$data[$p]['time'] = $time;
}
}
}
return $data;
}
function A2S_info()
{
$buffer = $this->A2S_query(A2S_INFO);
$i = 5;
$data = array();
$data['netver'] = ord($buffer{$i++});
$data['hostname'] = $this->readString($buffer, $i);
$data['map'] = $this->readString($buffer, $i);
$data['game'] = $this->readString($buffer, $i);
$data['desc'] = $this->readString($buffer, $i);
$temp = unpack("S",$buffer{$i++}.$buffer{$i++});
$data['appid'] = $temp[1];
$data['players'] = ord($buffer{$i++});
$data['maxplayers'] = ord($buffer{$i++});
$data['bots'] = ord($buffer{$i++});
$data['dedicated'] = ord($buffer{$i++});
$data['os'] = $buffer{$i++};
$data['pwd'] = ord($buffer{$i++});
$data['vac'] = ord($buffer{$i++});
$data['appver'] = $this->readString($buffer, $i);
return $data;
}
function A2S_close()
{
fclose($this->connection);
}
}
?>
servertable.php
PHP Code:
<?PHP
include 'lib/open_database.php';
include 'lib/class_ValveSourceServer.php';
function PrintServerInfo($server,$port){
$ValveSourceServer = new ValveSourceServer();
$ValveSourceServer->ConnectToServer($server,$port);
$_player_array = $ValveSourceServer->A2S_players();
$_data = $ValveSourceServer->A2S_Info();
$ValveSourceServer->Disconnect();
$server = gethostbyaddr(gethostbyname($server));
$_hostname=$_data['hostname'];
$_playercount=count($_player_array);
$_map=$_data['map'];
$_gamemode=$_data['desc'];
/*foreach($_player_array as $_player){
$_playername = $_player['name'];
$_playerscore = $_player['frags'];
$_playertime = $_player['time'];
}*/
//print_r($_player_array);
echo "<tr><td bgcolor=\"#eeeeee\">".$_hostname."</td>";
echo "<td>".$server.":".$port."</td>";
echo "<td>".$_playercount."</td>";
echo "<td>".$_map."</td>";
echo "<td>".$_gamemode."</td></tr>";
}
function PrintAllServerInfo(){
$query = "SELECT * from `bluorb`.`servers` ORDER BY `Game`;";
$SQL_result=mysql_query($query);
$_lastgame="";
while($row = mysql_fetch_assoc($SQL_result)){
if($row['GameEngine']=="sourceengine"){
echo "Printing Server info for ".$row['IP'].":".$row['Port']."\n";
if (($row['Game'] !== $_lastgame) || ($_lastgame="")){
if($_lastgame !== ""){
printf("</table></div>");
}
$_game=$row['Game'];
echo '<div class="file_table"><table border="0" width="100%" cellspacing="0" cellpadding="0" class="files"><tr><td bgcolor="#eeeeee"><a href=/games/'.str_replace(' ','_',$_game).'.php><strong>'.$_game.'</strong></a></td> <td bgcolor="#eeeeee"><strong>Server Address</strong></td><td bgcolor="#eeeeee"><strong>Players</strong></td><td bgcolor="#eeeeee"><strong>Map</strong></td><td bgcolor="#eeeeee"><strong>Game Mode</strong></td></tr>';
}
PrintServerInfo($row['IP'],$row['Port']);
}
$_lastgame=$row['Game'];
}//WHILE
printf("</table></div>");
}
PrintAllServerInfo();
It works as intended, but why is it so slow??