Location: PHPKode > scripts > TeeWorlds Server Stats > teeworlds-server-stats/TeeWorldsServer.php
<?php

require('TeeWorldsPlayer.php');

/**
 * TeeWorldsServer class
 *
 * Stores Tee Worlds server information.
 *
 * @copyright  2009 Tomaž Muraus
 * @license    http://www.gnu.org/copyleft/gpl.html   GPL License
 * @version    Release: 1.0
 * @link       http://www.tomaz-muraus.info
 */
class TeeWorldsServer
{
	protected $_ip;
	protected $_port;
	protected $_timeout;
	protected $_socket = null;
	protected $_command = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffgief";

	protected $_ping;
    protected $_version;
    protected $_name;
    protected $_map;
    protected $_gametype;
    protected $_flags;
    protected $_progression;
    protected $_playerCount;
    protected $_maxPlayers;
    protected $_players = array();

	/**
	 * Constructor.
	 *
	 * @param string $ip Server IP address.
	 * @param int $port Server port.
	 * @param int $timeout Connection and socket read/write timeout in seconds.
	 */
	public function __construct($ip, $port = 8303, $timeout = 2)
	{
		$this->_ip = $ip;
		$this->_port = $port;
		$this->_timeout = $timeout;
	}

	public function getIp()
	{
	    return $this->_ip;
	}

	public function getPort()
	{
	    return $this->_port;
	}

	public function getPing()
	{
	    return $this->_ping;
	}

	public function getVersion()
	{
	    return $this->_version;
	}

	public function getName()
	{
	    return $this->_name;
	}

	public function getMap()
	{
	    return $this->_map;
	}

	public function getGametype()
	{
	    return $this->_gametype;
	}

	public function getFlags()
	{
	    return $this->_flags;
	}

	public function getProgression()
	{
	    return $this->_progression;
	}

	public function getPlayerCount()
	{
	    return $this->_playerCount;
	}

	public function getMaxPlayers()
	{
	    return $this->_maxPlayers;
	}

	public function getPlayers()
	{
	    return $this->_players;
	}

	/**
	 * Opens a socket and requests server information.
	 *
	 * @return boolean TRUE on success, FALSE otherwise.
	 */
	public function queryServer()
	{
	    // Creates a socket and connects to it
		$this->_socket = stream_socket_client('udp://' . $this->_ip . ':' . $this->_port, $errno, $errstr, $this->_timeout);

		// Stream (read/write) timeout
		stream_set_timeout($this->_socket, $this->_timeout);

		if ($this->_socket !== FALSE)
		{
		    $start = microtime(TRUE);
            // Sends the status command and reads the response
            fwrite($this->_socket, $this->_command, strlen($this->_command));
            $response = fread($this->_socket, 2048);
            $end = microtime(TRUE);

            if ($response)
            {
                // Ping in ms (response time - request time) * 1000 (1 ms = 1000 us)
                $this->_ping = round((($end - $start) * 1000));

                // Parses the server response
                $this->_parseServerResponse($response);

                // Closes a socket resource
		        fclose($this->_socket);

                return TRUE;
            }
            else
            {
                 // Closes a socket resource
		        fclose($this->_socket);

                return FALSE;
            }
		}
		else
		{
		    return FALSE;
		}
	}

	/**
	 * Parses the server response and saves the result into the class variables.
	 *
	 * @param string $response Server response delimited with NULL character.
	 *
	 * @return void
	 */
	protected function _parseServerResponse($response)
	{
	    $matches = array();
        preg_match('/
        info(\d\.\d\.\d)	# server version
        \0(.+?)				# server name
        \0(\w+)   			# map name
        \0(ctf|tdm|dm|mod)	# game type
        \0(\d+)				# flags
        \0(\d+)				# progression
        \0(\d+)				# player count
        \0(\d+)				# max players
        \0(.*)				# players
        /ix', $response, $matches);

        // Server information
        $this->_version = $matches[1];
        $this->_name = $matches[2];
        $this->_map = $matches[3];
        $this->_gametype = $matches[4];
        $this->_flags = $matches[5];
        $this->_progression = $matches[6];
        $this->_playerCount = $matches[7];
        $this->_maxPlayers = $matches[8];

        // Parses the player data
        if ($matches[9] != '')
        {
            $this->_parsePlayerData($matches[9]);
        }
	}

	/**
	 * Parses the player data and saves the response into the class variable $_players.
	 *
	 * @param string $data Player list delimited with NULL character.
	 *
	 * @return void
	 */
	protected function _parsePlayerData($data)
	{
	    $matches = array();
	    preg_match_all('/
	    (.+?)				# player name
	    \0(-{0,1}\d+)		# player score
	    /ix', $data, $matches);

        for ($i = 0; $i < count($matches[0]); $i++)
        {
            $this->_players[] = new TeeWorldsPlayer(ltrim($matches[1][$i]), $matches[2][$i]);
        }
	}
}

/* End of file TeeWorldsServer.php */
/* Location: ./TeeWorldsServer.php */
Return current item: TeeWorlds Server Stats