Main Page | Class Hierarchy | Class List | Directories | Class Members | Related Pages | Examples

example_usage.php

This is a detailed example of how to use gsQuery

<?php header('Content-type: text/html', TRUE); // application/xhtml+xml normally, but M$ does not know what web standards are... 
?>
<?php
/*
 *  gsQuery - Querys game servers
 *  Copyright (c) 2002-2004 Jeremias Reith <jr@terragate.net>
 *  http://gsquery.terragate.net
 *
 *  This file is part of the gsQuery library.
 *
 *  The gsQuery library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  The gsQuery library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with the gsQuery library; if not, write to the
 *  Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 */
?>
<?php 
  // stylesheet declaration has to be quoted because most php web servers has
  // have the short open tag option activated.
  echo "<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n";
  echo "<?xml-stylesheet href=\"style.css\" type=\"text/css\"?>\n";
?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>gsQuery Example</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
  </head>
  <body>
    <!--
      To use this example simply add the following params to the URI:
      host: The game server's hostname or IP
      queryport: The game server's queryport
      protocol: The protocol to use (currently gameSpy, hlife, q3a, vietkong, rvnshld, armyGame)

      Example:
      http://mydomain.com/path/to/gsQuery/examples/example_usage.php?host=terragate.net&amp;queryport=7787&amp;protocol=gameSpy

      Note: You have to adjust the path in the include statment if you want to use the example outside of the gsQuery example directory
    -->

    <div id="basicInfo" class="block">
      <?php 
        if(isset($_GET['debug'])) {
          error_reporting(E_ALL);
        }

        if(isset($_GET['url'])) {

          require_once dirname(__FILE__) . DIRECTORY_SEPARATOR .'..'. 
                                           DIRECTORY_SEPARATOR .'src'. 
                                           DIRECTORY_SEPARATOR .'gsQuery.php'; 
          
          $gameserver=gsQuery::unserializeFromURL($_GET['url']);
          if(!$gameserver) {
            echo 'Could not fetch the serialized object.';
          }
        } else {
          $gameserver=queryServer($_GET['host'], $_GET['queryport'], $_GET['protocol']);
        }
      if(!$gameserver) { echo '</div></body></html>'; exit(0); }
      $version = $gameserver->version;
      basicInfo($gameserver); 
      ?>
    </div>
    <div id="players" class="block">
      <?php players($gameserver); ?>          
    </div>
    <div id="rules" class="block">
     <?php rules($gameserver); ?>          
    </div>
    <div id="debug" class="block">
      <?php if(isset($_GET['debug'])) { debugInfo($gameserver); } ?>          
    </div>
    <div id="footer">
      <a href="http://validator.w3.org/check/referer"><img
         src="http://www.w3.org/Icons/valid-xhtml10"
         alt="Valid XHTML 1.0!" height="31" width="88"/></a>
      <a href="http://jigsaw.w3.org/css-validator/">
        <img style="border:0;width:88px;height:31px"
             src="http://jigsaw.w3.org/css-validator/images/vcss" 
             alt="Valid CSS!"/></a>
      <p>
        Powered by <a href="http://www.gsquery.org">gsQuery</a>
        <?php echo $version; ?>
      </p>
    </div>
  </body>
</html>


<?php

function queryServer($address, $port, $protocol)
{
  require_once dirname(__FILE__) . DIRECTORY_SEPARATOR .'..'.
                                   DIRECTORY_SEPARATOR .'src'. 
                                   DIRECTORY_SEPARATOR .'gsQuery.php'; 

  if(!$address && !$port && !$protocol) {
    echo "No parameters given\n";
    return FALSE;
  }

  $gameserver=gsQuery::createInstance($protocol, $address, $port);
  if(!$gameserver) {
    echo "<h1>Could not instantiate gsQuery class. Does the protocol you've specified exist?</h1>\n";
    return FALSE;
  }
  
  if(!$gameserver->query_server(TRUE, TRUE)) { // fetch everything
    // query was not succesful, dumping some debug info
    echo '<h1>Error: '. ($gameserver->errstr ? $gameserver->errstr : 'Could not query the server / No response received') ."</h1>\n";
    if(array_key_exists('debug', $_GET) && $_GET['debug']) {
      debugInfo($gameserver); 
    }
    return FALSE;
  }

  return $gameserver;
}


function basicInfo($gameserver) 
{
  echo '<table><caption>Generic</caption>';

  // server query was succesful
  // show servertitle
  echo "<tr><td>Servername:</td><td>".$gameserver->htmlize($gameserver->servertitle)."</td></tr>\n";
  
  // show gamever if available
  if($gameserver->gameversion!="") {
    echo "<tr><td>Version:</td><td>".htmlentities($gameserver->gameversion)."</td></tr>\n";
  }
  
  // show current/max players
  echo "<tr><td>Players/Max Players:</td><td>".htmlentities($gameserver->numplayers)."/".htmlentities($gameserver->maxplayers)."</td></tr>\n";
  
  // show mapname/maptitle
  echo "<tr><td>Mapname/Maptitle:</td><td>".htmlentities($gameserver->mapname);
  if($gameserver->maptitle) {
    echo " / ".htmlentities($gameserver->maptitle)."</td></tr>\n";
  } else {
    echo "</td></tr>";
  }

  // show gametype
  echo "<tr><td>Gametype:</td><td>".htmlentities($gameserver->gametype)."</td></tr>\n";

  echo "</table>";
}


function players($gameserver) 
{ 
  // check if there are players
  if(!count($gameserver->players)) {
    echo "No Players";
    return FALSE;
  }
 
  $result="<table><caption>Players</caption>\n<tr>";
  
  // create table header
  foreach($gameserver->playerkeys as $key => $value) {
    if($value) {
      $result.="<th>".$key."</th>\n";
    }
  }
  
  $result.="</tr>";

  // foreach player
  foreach($gameserver->players as $player) {
    $result.="<tr>";
    // dump all available infos
    foreach($gameserver->playerkeys as $key => $value) {
      if($value) {
        $result.="<td class=\"player".htmlentities($key)."\">".$gameserver->htmlize($player[$key])."</td>\n";
      }
    }
    $result.="</tr>\n";
  }
  $result.="</table>";
  echo $result;
}

function rules($gameserver) 
{
  if(!count($gameserver->rules)) {
    echo "No rules";
    return FALSE;
  }
   
  $result="<table><caption>Rules</caption><tr><th>Key</th><th>Value</th></tr>\n";
  
  // dump all rules
  foreach($gameserver->rules as $key => $value) {
    $result.="<tr><td class=\"rulekey\">".$key."</td><td>".$value."</td></tr>\n";
  }

  $result.="</table>";
  echo $result;
}

function debugInfo($gameserver) 
{  
  $dumps = $gameserver->getDebugDumps(TRUE);

  echo "<table><caption>Debug</caption><tr><th>Send</th><th>Received</th></tr>\n";
  foreach ($dumps as $curDebug) {
    echo "<tr><td>".$curDebug[0]."</td><td>".$curDebug[1]."</td></tr>\n";
  }
  echo "</table>";
}

?>

Generated on Sat Oct 15 16:51:55 2005 for gsQuery by  doxygen 1.4.4