Posts: 2
Threads: 1
Joined: Jan 2008
Reputation:
0
I have an init script that runs my server automatically at server startup on a CentOS 5 machine. In that script, there's a status option that tells me if the server is currently running or not. Is there any way to tell what the current server information like map, number of players, etc. is without having to check through steam? I'd love for my script (or any other script I might write for that matter) to tell me that information, but I'm not sure if/where it's stored to check. Thanks!
Posts: 3,906
Threads: 404
Joined: Oct 2007
Reputation:
21
01-22-2008, 07:27 AM
(This post was last modified: 01-22-2008, 07:27 AM by Mooga.)
Besides using HLSW, you can check game-monitor.com
Just go to:
Code:
http://www.game-monitor.com/GameServer/IP_ADDRESS:PORT
~ Mooga ...w00t? - SRCDS.com on Twitter
Please do not PM me for server related help
fqdn Wrote:if you've seen the any of the matrix movies, a game server is not all that different. it runs a version of the game that handles the entire world for each client connected. that's the 2 sentence explanation.
Posts: 12
Threads: 3
Joined: Jan 2008
Reputation:
0
01-29-2008, 11:51 PM
(This post was last modified: 01-29-2008, 11:53 PM by ColdFire.)
I've made a php script which does what you want. It generates a png image as my main forum does not accept dynamically generated images.
With a few modifications, the php script could display the generated image without saving it into a file.
It's a quick done script, so there's place for improvement.
Code:
<?php
/**
* Return a byte and split it out of the string
* - unsigned char
*
* @param string $string String
*/
function getByte(&$string)
{
$data = substr($string, 0, 1);
$string = substr($string, 1);
$data = unpack('Cvalue', $data);
return $data['value'];
}
/**
* Return an unsigned short and split it out of the string
* - unsigned short (16 bit, big endian byte order)
*
* @param string $string String
*/
function getShortUnsigned(&$string)
{
$data = substr($string, 0, 2);
$string = substr($string, 2);
$data = unpack('nvalue', $data);
return $data['value'];
}
/**
* Return a signed short and split it out of the string
* - signed short (16 bit, machine byte order)
*
* @param string $string String
*/
function getShortSigned(&$string)
{
$data = substr($string, 0, 2);
$string = substr($string, 2);
$data = unpack('svalue', $data);
return $data['value'];
}
/**
* Return a long and split it out of the string
* - unsigned long (32 bit, little endian byte order)
*
* @param string $string String
*/
function getLong(&$string)
{
$data = substr($string, 0, 4);
$string = substr($string, 4);
$data = unpack('Vvalue', $data);
return $data['value'];
}
/**
* Return a float and split it out of the string
*
* @param string $string String
*/
function getFloat(&$string)
{
$data = substr($string, 0, 4);
$string = substr($string, 4);
$array = unpack("fvalue", $data);
return $array['value'];
}
/**
* Return a string and split it out of the string
*
* @param string $string String
*/
function getString(&$string)
{
$data = "";
$byte = substr($string, 0, 1);
$string = substr($string, 1);
while (ord($byte) != "0")
{
$data .= $byte;
$byte = substr($string, 0, 1);
$string = substr($string, 1);
}
return $data;
}
// Constant
define('PACKET_SIZE', '1400');
define('SERVERQUERY_INFO', "\xFF\xFF\xFF\xFFTSource Engine Query");
define ('REPLY_INFO', "\x49");
define('SERVERQUERY_GETCHALLENGE', "\xFF\xFF\xFF\xFF\x57");
define ('REPLY_GETCHALLENGE', "\x41");
// Ip address and port
$_ip = 'xxx.xxx.xxx.xxx' ; //set your server ip here
$_port = '27015'; //set your server port here
$socket = fsockopen('udp://'.$_ip, $_port, $errno, $errstr, 1);
// Send command to server
$cmd = SERVERQUERY_GETCHALLENGE;
$length = strlen($cmd);
fwrite($socket, $cmd, $length);
// Get response from server
$response = fread($socket, PACKET_SIZE);
if (empty($response))
{
// header("Content-type: image/png"); // uncomment to allow php to send the png to client and not to store it to a file
$string = "Serveur TF2 Offline";
$im = imagecreatefrompng("images/offline.png"); // check the path here
$white = imagecolorallocate($im, 255, 255, 255);
$px = (imagesx($im) - 7 * strlen($string)) / 2;
imagestring($im, 3, $px, 5, $string, $white);
imagepng($im, '/path/to/store/your/tf2status.png'); // comment here for the image not to be stored in tf2status.png
//imagepng ($im) ; //uncomment to send the image to standard output (the browser)
imagedestroy($im);
}
else
{
// Open connection with server
$socket = fsockopen ('udp://'.$_ip, $_port, $errno, $errstr, 30);
// Send command to server
$cmd = SERVERQUERY_INFO;
$length = strlen($cmd);
fwrite($socket, $cmd, $length);
// Get response from server
$response = fread($socket, PACKET_SIZE);
// Clean response
$pattern = "#\xFF\xFF\xFF\xFF".REPLY_INFO."#";
$response = preg_replace($pattern, '', $response);
//Version - byte (Network version. 0x07 is the current Steam version.)
$server['version'] = getByte($response);
// Ip and port
$server['ip'] = $_ip;
$server['port'] = $_port;
//Server Name - string (The Source server's name, eg: "My über TF2 Server")
$server['name'] = trim(getString($response));
//Map - string (The current map being played, eg: "cp_well")
$server['map'] = getString($response);
//Game Directory - string (The name of the folder containing the game files, eg: "tf")
$server['gamedir'] = getString($response);
//Game Description- string (A friendly string name for the game type, eg: "Team Fortress")
$server['gamedesc'] = getString($response);
//AppID - short (Steam Application ID)
$server['appid'] = getShortSigned($response);
//Number of players - byte (The number of players currently on the server)
$server['numplayers'] = getByte($response);
//Maximum players - byte (Maximum allowed players for the server)
$server['maxplayers'] = getByte($response);
//Number of bots - byte (Number of bot players currently on the server)
$server['bot'] = getByte($response);
//Dedicated - byte ('l' for listen, 'd' for dedicated, 'p' for SourceTV)
$data = chr(getByte($response));
$server['dedicated'] = 0;
$server['sourcetv'] = 0;
$server['listen'] = 0;
if ($data == 'd') $server['dedicated'] = 1;
if ($data == 'p') $server['sourcetv'] = 1;
if ($data == 'l') $server['listen'] = 1;
//OS - byte (Host operating system. 'l' for Linux, 'w' for Windows)
$data = chr(getByte($response));
$server['os'] = 'undefined';
if ($data == 'l')
{
$server['os'] = 'linux';
}
elseif ($data == 'w')
{
$server['os'] = 'windows';
}
//Password - byte (If set to 0x01, a password is required to join this server)
$data = getByte($response);
$server['password'] = 0;
if ($data == 1)
{
$server['password'] = 1;
}
//Secure - byte (if set to 0x01, this server is VAC secured)
$data = getByte($response);
$server['secure'] = 0;
if ($data == 1)
{
$server['secure'] = 1;
}
//Game Version - string (The version of the game, eg: "1.0.0.22")
$server['gameversion'] = getString($response);
//header("Content-type: image/png"); //uncomment this line if you want the image to be directly sent to the browser
$string1 = $server['name'];
$string2 = $_ip.":".$_port ;
$string3 = "Map: ".$server['map'];
$string4 = "Players: ".$server['numplayers']."/".$server['maxplayers'] ;
$im = imagecreatefrompng("images/online.png"); // check that the source image is available
$white = imagecolorallocate($im, 255, 255, 255);
$px1 = (imagesx($im) - 7 * strlen($string1)) / 2;
$px2 = (imagesx($im) - 7 * strlen($string2)) / 2;
$px3 = (imagesx($im) - 7 * strlen($string3)) / 2;
$px4 = (imagesx($im) - 7 * strlen($string4)) / 2;
imagestring($im, 3, $px1, 3, $string1, $white) ;
imagestring($im, 3, $px2, 15, $string2, $white) ;
imagestring($im, 3, $px3, 27, $string3, $white) ;
imagestring($im, 3, $px4, 39, $string4, $white) ;
imagepng($im,'/path/to/store/the/tf2status.png'); // comment this line if you don't want the image to be stored.
//imagepng($im); // uncomment if you want the image to be sent to standard output, i.e. the web browser
imagedestroy($im);
}
?>
You can find source images here. By default, put them in a "images" subfolder.
Online
Offline
Note: if you want to directly output the image to the browser (imagine <img src="http://blah.com/tf2info.php">), there are some lines to uncomment, and some to comment. Check the code to find them.
Posts: 12
Threads: 3
Joined: Jan 2008
Reputation:
0
It's possible to send RCON commands via PHP to a Source server, so I think I'll try that in the next days and I'll keep you updated with script samples.
|