SRCDS Steam group


Query a server with PHP
#31
Afaik Srcds does not have a rcon-command showing the player's team.
However you could create a UDP-listener and use the remote-logging feature to receive all the logs to a daemonized script
which then keeps track of the player's team changes, etc.
Q: What would an omnipotent computer to?
A: Get rid of humanity!
Reply
#32
Hm,
i thought about a way to send a query to the steam masterserver, it should be possible to get these information in theory.
The query itself is easy to manage, the protocol is listed here: http://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol
but there must be a second query to get the playerlist (the list you see when you open the server info window)

i know how to do that with php, but i'm not very experienced with the source engine itself...

Also: http://developer.valvesoftware.com/wiki/Server_Queries
Reply
#33
hmmm for some reason I cant get this to work. Do you have to name the first code strip a certain name?
Reply
#34
Is there a way to reduce the query time? Like if a server is currently down or offline, it would show up as offline. Currently it takes like over 30 seconds to load the page because the servers are offline.
Reply
#35
(08-22-2010, 10:14 PM)SaintGTR Wrote:  Is there a way to reduce the query time? Like if a server is currently down or offline, it would show up as offline. Currently it takes like over 30 seconds to load the page because the servers are offline.

You should be able to set a timeout in the actual query. You can simply adjust it to your needs Smile
Reply
#36
(08-22-2010, 10:14 PM)SaintGTR Wrote:  Is there a way to reduce the query time? Like if a server is currently down or offline, it would show up as offline. Currently it takes like over 30 seconds to load the page because the servers are offline.

http://php.net/manual/en/function.set-time-limit.php
~ trewq
Reply
#37
(08-22-2010, 10:14 PM)SaintGTR Wrote:  Is there a way to reduce the query time? Like if a server is currently down or offline, it would show up as offline. Currently it takes like over 30 seconds to load the page because the servers are offline.

Send the server with a "ping" packet before actually querying it to check if its online/offline, than continue querying as usual (just set it up in a simple if/else block), it's faster than a timeout.

http://developer.valvesoftware.com/wiki/Source_Server_Queries
Look for "A2A_PING"
[Image: 468.gif]

IRC's idled on: SA-MP IRC (#php #sa-mp #samp.scripting), GTANet (#volt #vx-rp #epic-missions #mta #vice-players), GameSurge (#Everystuff #srcds)
Steam: ilinxx
Contact email: linx@volt-host.com
Reply
#38
Thats wicked mate! I've made use of that script (slightly modified) on my servers website, works wondersSmile
BS Gaming Community gamesadmin@brainstew.co.nz
Reply
#39
I've updated this script to remove those hacky things and added some more functionality. I had to invent php byte buffer as I couldn't find anything similar lol.

Anyway here it is:

Code:
class ByteBuffer
{
    public $data = array(), $pointer;
    
    function __construct($data)
    {
        $this->data = str_split($data);
        $this->pointer = 0;
    }
    
    public function ReadByte()
    {
        return ord($this->data[$this->pointer++]);
    }
    
    public function ReadShort()
    {
        return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8));
    }
    
    public function ReadInt()
    {
        return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8) | (ord($this->data[$this->pointer++])<<16) | (ord($this->data[$this->pointer++])<<24));
    }
    
    public function ReadChar()
    {
        return $this->data[$this->pointer++];
    }
    
    public function ReadString()
    {
        $string = '';
        while ($this->data[$this->pointer] != "\0")
        {
            $string .= $this->data[$this->pointer];
            $this->pointer++;
        }
        $this->pointer++;
        return $string;
    }
}

    function source_query($ip)
    {
        $cut = explode(":", $ip);
        $HL2_address = gethostbyname($cut[0]);
        $HL2_port = $cut[1];

        $HL2_command = "\377\377\377\377TSource Engine Query\0";
        $HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr,3);
        fwrite($HL2_socket, $HL2_command);
        $JunkHead = fread($HL2_socket,4);
        $CheckStatus = socket_get_status($HL2_socket);

        if ($CheckStatus["unread_bytes"] == 0)
        {
            return 0;
        }

        $HL2_stats = '';
        $do = 1;
        while($do)
        {
            $str = fread($HL2_socket,1);
            $HL2_stats .= $str;
            $status = socket_get_status($HL2_socket);
            if($status["unread_bytes"] == 0)
            {
                $do = 0;
            }
        }
        fclose($HL2_socket);

        $result = '';
        $x = 0;
        while ($x <= strlen($HL2_stats))
        {
            $x++;
            $result.= substr($HL2_stats, $x, 1);    
        }

        return $result;
    }

    function format_source_query($string)
    {
        $buffer = new ByteBuffer($string);
        $info['version']         = $buffer->ReadByte();
        $info['hostname']         = $buffer->ReadString();
        $info['map']             = $buffer->ReadString();
        $info['game']             = $buffer->ReadString();
        $info['gamedesc']         = $buffer->ReadString();
        $info['appid']             = $buffer->ReadShort();
        $info['players']         = $buffer->ReadByte();
        $info['maxplayers']     = $buffer->ReadByte();
        $info['bots']             = $buffer->ReadByte();
        $info['dedicated']         = $buffer->ReadChar(); // l - listen, d - dedicated, p - sourcetv
        $info['os']             = $buffer->ReadChar(); // l - linux, w - windows
        $info['password']         = $buffer->ReadByte(); // true/false
        $info['vac']             = $buffer->ReadByte(); // true/false
        $info['gameversion']     = $buffer->ReadString();

        return $info;
    }

    $query = source_query($address);
    $response = format_source_query($query);
Reply
#40
Can someone help me make a little php script for Counter-Strike: Global Offensive?

If the Counter-Strike: Global Offensive server is online the php script should show this ->
Server Status: Online (green text color)
Server IP: x.x.x.x:x
Server Name: xxx
Server Version: xxx
Game Mode: xxx
Corrent Map: xxx
Current Players: x / x

If the Counter-Strike: Global Offensive server is offline the php script should show this ->
Server Status: Offline (red text color)
Server IP: x.x.x.x:x
Reply
#41
Yea, I can't get the Current Players and Max Players to show up properly. This is what I got to work, [Image: 1G4Cn]. But I don't have 37 players on or a max of 49 players. Its still in bytes.


Functions
PHP Code:
<?php
// error_reporting(0);
/* SOURCE ENGINE QUERY FUNCTION, requires the server ip:port */


class ByteBuffer
{
    public 
$data = array(), $pointer;
    
    function 
__construct($data)
    {
        
$this->data str_split($data);
        
$this->pointer 0;
    }
    
    public function 
ReadByte()
    {
        return 
ord($this->data[$this->pointer++]);
    }
    
    public function 
ReadShort()
    {
        return (
ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8));
    }
    
    public function 
ReadInt()
    {
        return (
ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8) | (ord($this->data[$this->pointer++])<<16) | (ord($this->data[$this->pointer++])<<24));
    }
    
    public function 
ReadChar()
    {
        return 
$this->data[$this->pointer++];
    }
    
    public function 
ReadString()
    {
        
$string '';
        while (
$this->data[$this->pointer] != "\0")
        {
            
$string .= $this->data[$this->pointer];
            
$this->pointer++;
        }
        
$this->pointer++;
        return 
$string;
    }
}



function 
source_query($ip)
{
$cut explode(":"$ip);
$HL2_address $cut[0];
$HL2_port $cut[1];

$HL2_command "\377\377\377\377TSource Engine Query\0";
$HL2_socket fsockopen("udp://".$HL2_address$HL2_port$errno$errstr,3);
fwrite($HL2_socket$HL2_command); $JunkHead fread($HL2_socket,4);
$CheckStatus socket_get_status($HL2_socket);

if(
$CheckStatus["unread_bytes"] == 0)
{
    return 
0;
}

$do 1;
while(
$do)
{
    
$str fread($HL2_socket,1);
    
$HL2_stats.= $str;
    
$status socket_get_status($HL2_socket);
    if(
$status["unread_bytes"] == 0)
    {
        
$do 0;
    }
}
fclose($HL2_socket);

$x 0;
while (
$x <= strlen($HL2_stats))
{
    
$x++;
    
$result.= substr($HL2_stats$x1);    
}
$result urlencode($result); // the output
return $result;



/* FORMAT SOURCE ENGINE QUERY (assumes the query's results were urlencode()'ed!) */
function format_source_query($string)
{
$string str_replace('%07','',$string);
$string str_replace("%00","|||",$string);
$sinfo urldecode($string);
$sinfo explode('|||',$sinfo);
$info['hostname'] = $sinfo[0];
$info['map'] = $sinfo[1];
$info['game'] = $sinfo[2];
if (
$info['game'] == 'garrysmod') { $info['game'] = "Garry's Mod"; }
elseif (
$info['game'] == 'cstrike') { $info['game'] = "Counter-Strike: Source"; }
elseif (
$info['game'] == 'dod') { $info['game'] = "Day of Defeat: Source"; }
$info['gamemode'] = $sinfo[3];
$buffer = new ByteBuffer($string);
$info['players'] = $buffer->ReadByte();
$info['maxplayers'] = $buffer->ReadByte();

return 
$info;


?>


Display coding:
PHP Code:
<html>
<
body>

<?
php
include 'queryfunction.php';

$query source_query('XXXXX'); // $ip MUST contain IP:PORT
$q format_source_query($query);


echo 
"Hostname: ".$q['hostname'];
echo 
"<br>";
echo 
"Connection Info: XXXX";
echo 
"<br>";
echo 
"Map: ".$q['map'];
echo 
"<br>";
echo 
"Game: ".$q['game'];
echo 
"<br>";
echo 
"Gamemode: ".$q['gamemode']; 
echo 
"<br>";
echo 
"Players: " .$q['players'];
echo 
"/" .$q['maxplayers'];

?>

</body>
</html> 
Reply
#42
(11-30-2008, 04:26 PM)Psychosis Wrote:  How do I get playercount in the form "xx/maxplayers" ?

You can use this
Code:
echo $q['players']."/".$q['max']."<br/>";

To replace the
Code:
echo "players: ".$q['players']."<br/>";
echo "max: ".$q['max']."<br/>";

part of the code
Reply


Forum Jump:


Users browsing this thread: 11 Guest(s)