Server Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Server Query

Post by scts102 »

Hello,
I recently downloaded the gsQuery package. I have messed around with it, and figured out how it functioned.
Here is one of the full examples:

Code: Select all

<?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) 2003 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-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/exa ... ol=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"])) {
          include_once("../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); }
        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>
    </div>
  </body>
</html>


<?php

function queryServer($address, $port, $protocol)
{
  include_once("../gsQuery.php"); 

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

  $gameserver=gsQuery::createInstance($protocol, $address, $port);
  if(!$gameserver) {
    echo "Could not instantiate gsQuery class. Does the protocol you've specified exist?\n";
    return FALSE;
  }
  
  if(!$gameserver->query_server(TRUE, TRUE)) { // fetch everything
    // query was not succesful, dumping some debug info
    echo "<div>Error ".$gameserver->errstr."</div>\n";
    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>";
}


/**
 * @brief dumps all available player infos in a table 
 * @param gameserver the gameserver to use the data from
 */
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;
}

/**
 * @brief dumps all rules in a table
 * @param gameserver the gameserver to use the rules from
 */
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;
}

/** 
 * @brief dumps some debug infos
 *
 * @param queryString the raw query string
 * @return a hash with all found values
 */
function debugInfo($gameserver) 
{
  echo "<table><caption>Debug</caption><tr><th>That was done</th><th>That happened</th></tr>\n";
  foreach ($gameserver->debug as $key => $value) {
    if(!is_array($value)) {
      echo "<tr><td>".htmlentities($key)."</td><td>".htmlentities($value)."</td></tr>\n";
    }
  }
  echo "</table>";
}

?>
The only thing I need from this is the list of players, sans the extra player info (frags, etc) I was wondering if someone could strip this so it only queries for the player names.

My next question is, how would I sort this playerlist into a combo box (drop down menu)?


Thanks for any help,
Scts102
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

looks like you should just call players()
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Yes, you are correct. I should have seen that, I had a long day yesterday. Thanks.

My new issue: when the server is queried, the player list is generated, but it also includes all the extra information, like player stats and current round information. Is there a way to exclude this? I was looking around the different files in gsQuery, but I could not find a way to remove it.

Sorry for asking trivial questions, but I am new to php....and I gotta start somewhere.



Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add code to filter the $player while inside the foreach of $gameserver->players
Post Reply