PHP ping client

From ASSS Wiki
Revision as of 09:16, 26 December 2007 by Goldeye (talk | contribs) (Added category:utilities)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Version 1.0 of this PHP class was produced by Goldeye on 12/26/07. It provides functions to report on the number of players logged on -- as well as the number playing -- in an ASSS zone or specific arenas. It is currently only compatible with ASSS's new Ping_Protocol. It's intended function is to put this information on a zone's website.

The code at the top is demonstration using SSCE and it's two most active arenas. It can be removed without affecting the class. Basic PHP knowledge is required to adapt the script

Feel free to contact the author with any requests or bugs with the script.

  //ServerPopulation.php v1.0 
  // by Goldeye 12/26/07

  //Errors are silent and the recv times out default 100ms.
  //Updates occur no more than once in 60 sec by default anyway. 
$hz = new ServerPopulation("",7502);
$total = $hz->get_server_total();
$playing = $hz->get_server_playing();
$spec = $total-$playing;
//Note: name automatically lowercased before it gets here.
print "Total: $playing playing, $spec spectating, $total logged in.\n";
print "(Public 0): $pub0[playing] playing, $pub0[spec] spectating, $pub0[total] logged in.\n";
print "Football: $fb[playing] playing, $fb[spec] spectating, $fb[total] logged in.\n";

class ServerPopulation
  var $host;
  var $port;
  var $last_time;
  var $update_time;
  var $timeout;
  var $arenas = array();
  var $total;
  var $playing;

  function ServerPopulation($host,$port,$update_time=60,$timeout=100)
  { //For PHP4

  function __construct($host,$port,$update_time=60,$timeout=100)
  { //For PHP5
    $this->host = $host;
    $this->port = $port;

  function get_server_total()
    return $this->total;

  function get_server_playing()
    return $this->playing;
  function get_arena($name)
    return $this->arenas[$name];

  function get_arena_array()
    return $this->arenas;

  function update()
    if(time()-$this->last_time < $this->update_time)
    $socket = socket_create(AF_INET, SOCK_DGRAM, 0);
    $result = socket_connect($socket,$this->host,$this->port);
    if(!$socket || !$result) return;
    socket_write($socket, $out, 8);
    if(@socket_recv($socket,&$in,512,0) < 1)
    $res = unpack("Vtime/Voptions",$in);
    $opt = $res["options"];
    if($opt & 0x01) // Server info included.
	$in = substr($in,8);
	$res = unpack( "Vtotal/Vplaying",$in);
	$this->total = $res["total"];
	$this->playing = $res["playing"];
    if($opt & 0x02) // Arena info included.
	$in = substr($in,8);
	    $n = strpos($in,0x00);
	    $name = strtolower(substr($in,0,$n));
	    $in = substr($in,$n+1);
	    $res = unpack("vtotal/vplaying",$in);
	    $in = substr($in,4);