Net_GameServerQuery
[ class tree: Net_GameServerQuery ] [ index: Net_GameServerQuery ] [ all elements ]

Source for file HalfLife.php

Documentation is available at HalfLife.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available through the world-wide-web at the following url:           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Aidan Lister <aidan@php.net>                                |
  16. // |          Tom Buskens <ortega@php.net>                                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: HalfLife.php,v 1.18 2004/09/18 14:26:02 aidan Exp $
  20.  
  21.  
  22. require_once 'Net\GameServerQuery\Protocol.php';
  23.  
  24.  
  25. /**
  26.  * HalfLife Protocol
  27.  *
  28.  * @category       Net
  29.  * @package        Net_GameServerQuery
  30.  * @author         Tom Buskens <ortega@php.net>
  31.  * @version        $Revision: 1.18 $
  32.  */
  33. {
  34.  
  35.     /**
  36.      * Details packet
  37.      *
  38.      * @access    private
  39.      * @return    array      Array containing formatted server response
  40.      */
  41.     private function _details()
  42.     {
  43.         // Header
  44.         if (!$this->_match("\xFF\xFF\xFF\xFF\x6d")) {
  45.             throw new Exception('Parsing error');
  46.         }
  47.  
  48.         // Body regular expression
  49.         $body "(([^\x00]+)\x00([^\x00]+)\x00([^\x00]+)\x00([^\x00]+)\x00"
  50.               . "([^\x00]+)\x00(.)(.)(.)(.)(.)(.)(.)";
  51.  
  52.         // Body variable names
  53.         $vars = array('serverip''servername''mapname''gamedir',
  54.                       'gamename''playercount''playermax',
  55.                       'protocolversion''servertype''serveros',
  56.                       'serverpassword''gamemod'
  57.         );
  58.  
  59.         // Match body
  60.         if ($this->_match($body)) {
  61.  
  62.             // Process and save variables
  63.             for ($i = 0$x count($vars)$i != $x$i++{
  64.                 switch ($i{
  65.                     case  5:
  66.                     case  6:
  67.                     case  7:
  68.                     case 10:
  69.                     case 11:
  70.                         $this->_result[$i+1$this->toInt($this->_result[$i+1]);
  71.                     default:
  72.                         $this->_add($vars[$i]$this->_result[$i+1]);
  73.                         break;
  74.                 }
  75.             }
  76.  
  77.         }
  78.         else {
  79.             throw new Exception('Parsing error');
  80.         }
  81.  
  82.         return $this->_output;
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Infostring packet
  88.      *
  89.      * @access     protected
  90.      * @return     array     Array containing formatted server response
  91.      */
  92.     protected function _infostring()
  93.     {
  94.         // Header
  95.         if (!$this->_match("\xFF\xFF\xFF\xFFinfostringresponse\x00")) {
  96.             throw new Exception('Parsing error');
  97.         }
  98.  
  99.         // Variable / value pairs
  100.         while ($this->_match("\\([^\\]+)\\([^\\\x00]*)")) {
  101.             $this->_add($this->_result[1]$this->_result[2]);
  102.         }
  103.  
  104.         // Terminating character
  105.         if (!$this->_match("\x00")) {
  106.             throw new Exception('Parsing error');
  107.         }
  108.  
  109.         return $this->_output;
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Ping packet
  115.      *
  116.      * @access     protected
  117.      * @return     array     Array containing formatted server response
  118.      */
  119.     protected function _ping()
  120.     {
  121.         if ($this->_match("\xFF\xFF\xFF\xFF\n")) {
  122.             return $this->_output;
  123.         else {
  124.             throw new Exception('Parsing error');
  125.         }
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Players packet
  131.      *
  132.      * @access     protected
  133.      * @return     array     Array containing formatted server response
  134.      */
  135.     protected function _players()
  136.     {
  137.         // Header
  138.         if ($this->_match("\xFF\xFF\xFF\xFF\x44(.)")) {
  139.             //$this->_add('playercount', $this->toInt($this->_result[1]));
  140.         else {
  141.             throw new Exception('Parsing error');
  142.         }
  143.  
  144.         // Players
  145.         while ($this->_match("(.)([^\x00]+)\x00(.{4})(.{4})")) {
  146.             $this->_addPlayer('id',    $this->toInt($this->_result[1]));
  147.             $this->_addPlayer('name',  $this->_result[2]);
  148.             $this->_addPlayer('score'$this->toInt($this->_result[3]32));
  149.             $this->_addPlayer('time',  $this->toFloat($this->_result[4]));
  150.         }
  151.  
  152.         return $this->_output;
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Rules packet
  158.      *
  159.      * @access     protected
  160.      * @return     array     Array containing formatted server response
  161.      */
  162.     protected function _rules()
  163.     {
  164.         // Remove the header of the possible second packet
  165.         $this->_response = preg_replace("/\xFE\xFF\xFF\xFF.{5}/"''$this->_response);
  166.  
  167.         // Get header, rulecount
  168.         if ($this->_match("\xFF\xFF\xFF\xFF\x45(.{2})")) {
  169.             $this->_add('rulecount'$this->toInt($this->_result[1]16));
  170.         else {
  171.             throw new Exception('Parsing error');
  172.         }
  173.  
  174.         // Variable / value pairs
  175.         while ($this->_match("([^\x00]+)\x00([^\x00]*)\x00")) {
  176.             $this->_add($this->_result[1]$this->_result[2]);
  177.         }
  178.  
  179.         return $this->_output;
  180.     }
  181.  
  182. }
  183.  
  184. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.