Jump to content

Figuring out how to use this code...


hlstriker

Recommended Posts

Hi, I found this code online somewhere and am trying to figure out how I can use it. What it does is queries a half-life game server and gets the information from it. Currently when I run the code it displays all of the servers information on the website with only print_r. I need to know how to format it so I can use all of the array in an echo or something that way I can use different parts of the array in my html. Really you all might just need to look at the last 4 lines of the code I think to know what to do. I can't figure this out though... any help would be great!

 

Here is the code...

<?php
class serverinfo
{
var $ip;
var $port;

var $fp;
var $timeout;

var $serverData;
var $playerData;


function serverinfo($ip, $port, $timeout = 3)
{
$this->ip = $ip;
$this->port = $port;

$this->fp = fsockopen("udp://" . $ip, $port);
$this->timeout = $timeout;
}


function getServerData()
{
$this->writeData($this->getQuery("A2S_INFO"));

$this->getData("byte");
$this->getString();
$this->serverData['hostname'] = $this->getString();
$this->serverData['map'] = $this->getString();
$this->getString();
$this->serverData['mod'] = $this->getString();
$this->serverData['players'] = $this->getData("byte");
$this->serverData['maxplayers'] = $this->getData("byte");
$this->getData("byte");
$this->serverData['servertype'] = (chr($this->getData("byte")) == "d") ? "Dedicated" : "Listen";
$this->serverData['server_os'] = (chr($this->getData("byte")) == "w") ? "Windows" : "Linux";
$this->serverData['password'] = $this->getData("byte");
$this->getData("byte");
$this->getString();
$this->getString();
$this->getString();
$this->getData("long");
$this->getData("long");
$this->getData("byte");
$this->getData("byte");
$this->serverData['vac'] = $this->getData("byte");
$this->getData("byte");
}


function getPlayerData()
{
$this->writeData($this->getQuery("A2S_PLAYER"));

fread($this->fp, 4);
$this->getData("byte");

$count = $this->getData("byte");

for($i = 0; $i < $count; $i++)
{
$this->playerData[$i]['id'] = $this->getData("byte");
$this->playerData[$i]['name'] = $this->getString();
$this->playerData[$i]['frags'] = $this->getData("long");
$this->playerData[$i]['time'] = round($this->getData("float"));
}
}


function getQuery($queryType)
{
switch($queryType)
{
case "A2S_SERVERQUERY_GETCHALLENGE":
return "\xFF\xFF\xFF\xFF\x57";
break;

case "A2S_INFO":
return "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
break;

case "A2S_PLAYER":
return sprintf("\xFF\xFF\xFF\xFFU%s", $this->getChallenge());
break;
}
}


function getChallenge()
{
$this->writeData($this->getQuery("A2S_SERVERQUERY_GETCHALLENGE"));

return substr(fread($this->fp, 16), 5, 9);
}


function getData($type)
{
switch($type)
{
case "long":
$data = unpack("L", fread($this->fp, 4));
return $data[1];
break;

case "byte":
return ord(fread($this->fp, 1));
break;

case "char":
return fread($this->fp, 1);
break;

case "float":
$data = unpack("f", fread($this->fp, 4));
return $data[1];
break;
}
}


function getString()
{
$string = '';
$loop = TRUE;

while($loop)
{
$_fp = $this->getData("char");

if( ord($_fp) != 0 )
{
$string .= $_fp;
}
else { $loop = FALSE; }
}

return $string;
}


function writeData($input)
{
if( !$this->fp )
{
exit("Error: Couldn't connect to server.");
}
else {
fwrite($this->fp, $input);
socket_set_timeout($this->fp, $this->timeout);
}
}


function setTimeFormat($format, $input)
{
$hours = floor($input / 3600);
$input = $input % 3600;

$minutes = floor($input / 60);
$input = $input % 60;

$seconds = round($input);

return sprintf($format, $hours, $minutes, $seconds);
}


function sortPlayers($sort = "time", $type = "desc")
{
if(isset($this->playerData[0][$sort]))
{
for($i = 0; $i < count($this->playerData); $i++)
{
$temp[] = $this->playerData[$i][$sort];
}

switch($sort)
{
case "name":
uasort($temp, "strcasecmp");
break;

default:
if($type == "desc") {
arsort($temp);
}
elseif($type == "asc") {
asort($temp);
}
break;
}

foreach($temp as $key => $value)
{
$keys[] = $key;
}

foreach($keys AS $k => $v)
{
$tempvar[$k]['id'] = $this->playerData[$v]['id'];
$tempvar[$k]['name'] = $this->playerData[$v]['name'];
$tempvar[$k]['frags'] = $this->playerData[$v]['frags'];
$tempvar[$k]['time'] = $this->playerData[$v]['time'];
}

$this->playerData = $tempvar;
}
}
}

$s = &new serverinfo("xx.xxx.xx.xx", "27015");
$s->getServerData();
$s->getPlayerData();

print "<pre>";
print_r($s->serverData);
print_r($s->playerData);
print "</pre>";
?>

Link to comment
https://forums.phpfreaks.com/topic/45692-figuring-out-how-to-use-this-code/
Share on other sites

Looks pretty simple, just copy all the code into your page. Then like you said, the last 4 lines do stuff:

 

inlcude_once( 'half-life.inc');

// this creates the object:
$s = &new serverinfo("xx.xxx.xx.xx", "27015");

// these query the server and get the data (may be slow)
$s->getServerData();
$s->getPlayerData();

// these print the data out
print "<pre>";
print_r($s->serverData);
print_r($s->playerData);
print "</pre>";

 

What problems are you having?

 

If I were you I'd put the class in a separate file, and use include_once at the top of the PHP page. I put this as an example into the above code. Then call the two query server data methods. Then somewhere in your page print this data out.

 

monk.e.boy

I'm a noob and I need to extract the info from print_r($s->serverData) and print_r($s->playerData) and put them into a variable by itself. I don't know how to use those arrays in an echo. I don't understand what the -> in $s->playerData means either.

 

Example: I need to echo array[0], echo array[1], echo array[2], etc... but also find out if data exists in those arrays.

I'm a noob and I need to extract the info from print_r($s->serverData) and print_r($s->playerData) and put them into a variable by itself. I don't know how to use those arrays in an echo. I don't understand what the -> in $s->playerData means either.

 

Example: I need to echo array[0], echo array[1], echo array[2], etc... but also find out if data exists in those arrays.

 

The arrow -> is the way to access and internal bit of the object. So to call a function in that object you $s->function_name() to access data in the object you $s->data.

 

I guess that serverData is an array, so to access it:

 

$a = $s->serverData;

echo $a[0];

 

or

 

echo $s->serverData[0];

 

and so on.

 

monk.e.boy

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.