Author Topic: PHP - NWServer Query  (Read 874 times)

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
PHP - NWServer Query
« on: January 04, 2012, 03:09:25 am »


                I found some PHP on pastebin that is used to query the NWServer and display the list of players in your module. Perhaps this is already available on the boards, but I haven't seen it. So here is one more post.

http://pastebin.com/QK2cmbvX 


<?php

/* Script: Query NWN Servers                                              *
 * Author: Ingmar Stieger (papillon@blackdagger.com) or (istieger@gmx.de) *
 * License: this script is released under the GPL (see gpl.txt)           *
 * Last modified: September 25, 2002                                      *
 *                                                                        *
 * This PHP script queries a Neverwinter Nights Server just like the      *
 * graphical client does.                                                 *
 *                                                                        *
 * Available information: all server properties, players, module desc     *
 *                                                                        *
 * Q: How do I use this on my website ?                                   *
 *                                                                        *
 * A: Nothing could be simpler than that:                                 *
 *    1. Specify the ip address and the port of the server below          *
 *    2. (optional) Change the default timeout if you like.               *
 *    3. (optional) Scroll to the section that says 'Create HTML output'  *
 *       and configure the display to your liking.                        *
 *                                                                        *
 * Todo                                                                   *
 *   - module description needs some formatting                           *
 *                                                                        *
 * Changelog                                                              *
 *                                                                        *
 * V1.0 / September 11, 2002                                              *
 *   - initial version                                                    *
 * V1.1 / September 12, 2002                                              *
 *   - improved handling of servers not responding                        *
 *   - improved parsing of playerlist                                     *
 * V1.2 / September 25, 2002                                              *
 *   - improved handling of servers not responding, again                 *
 *                                                                        */

// Specify ip address or name of NWN server
$ipaddr = '66.188.135.106'; // Example: The World Of Avlis, main server

// Specify port of server (5121 is default)
$port = '5121';

// Specify timeout (maximum time to wait for connection and data)
$timeout = 5; // five seconds;

function Connect()
{
        global $ipaddr, $port, $timeout;

        // open connection to server
        $fp = fsockopen("udp://" . $ipaddr, $port, $errno, $errstr, $timeout);

        // return error if connection fsockopen failed
        if (!$fp)
                echo "ERROR: $errno - $errstr<br>\\n";

        // Set timeout on socket
        socket_set_timeout($fp, $timeout);

        // return connection
        return $fp;
}

function Disconnect($fp)
{
        // Done reading, close connection
        fclose($fp);
}

function QueryServer($fp)
{
        // Send query request to server
        $query = '\\\\status\\\\';
        fwrite($fp,$query);

   // this counter acts as a safety net
   $passes = 0;

        // Read in everything the server sends us back
        while (!$status['eof'] && ($passes < 10))
        {
           $singlechar = fread($fp,1);
           $serverinfo = $serverinfo . $singlechar;

                $status = socket_get_status ($fp);
                $bytes = $status["unread_bytes"];
                $serverinfo = $serverinfo . @fread($fp, $bytes);
                if (strstr($serverinfo, '\\\\final\\\\\\\\queryid'))
                {
                        break;
                }
               
                $passes++;
        }              

        // The string 'queryid' is at the end of every packet
        // We need to delete it so parsing will be easier later on
        $del = "\\\\queryid\\\\";
        while (strstr($serverinfo, $del))
        {
                // preg_replace seems to be unable to handle backslashes, so...
       
                $start = strpos($serverinfo, $del);
                $left = substr($serverinfo, 0, $start);
                $right = substr($serverinfo, $start + strlen($del));
               
                $right = substr($right, strpos($right, '\\\\'));
                $serverinfo = $left . $right;
        }

        // return what the server sent us
        return $serverinfo;
}

//
// Parse server info and return data as a map / associative array
//
function ParseServerInfo($serverinfo)
{
        $lines = explode("\\n", $serverinfo);
        $data = explode("\\\\", $lines[0]);

        for ($i = 1; $i < count($data); $i+=2)
        {
                $name = $data[$i];
                $value = $data[$i+1];
                $map[$name] = $value;
                //print "$name = $value\\n";
        }
       
        return $map;
}

//
// Pare module description and return it as a string
// TODO: Needs some formatting
//
function ParseModuleInfo($serverinfo)
{
        // extract module description, part 1
        // (everything except last line)
        $lines = explode("\\n", $serverinfo);
       
        for ($i = 1; $i < count($lines) - 1; $i++)
                $moduledesc = $moduledesc . $lines[$i];

        // extract module description, part 2
        // (extract description part of last line)
        $lastDescLine = $lines[$i];
        $moduledesc = $moduledesc . substr($lastDescLine, 0, strpos($lastDescLine, "\\

\\"));
       
        return $moduledesc;
 }
 
 //
 // Parse player list and return it in a two dimensional, associative array
 //
 function ParsePlayerList($serverinfo, $numPlayers)
 {
        // parse player list
        $i=0;
        while ( ($i < 63) && (count($playerdata) < $numPlayers)) // max 63 players

online
        {
                $pos_start = strpos($serverinfo, "player_" . $i);
                if ($pos_start != 0) // player was found
                {
                        $playerinfo = substr($serverinfo, $pos_start, 144);  // umm.

yes. that's ugly.
                        //print "<p>player $i info:$playerinfo</p>";                  

     
                        $playerinfo = explode("\\\\", $playerinfo);
                       
                        $newEntry = count($playerdata);
                        $playerdata[$newEntry]['playername'] = $playerinfo[1];
                        $playerdata[$newEntry]['type'] = $playerinfo[3];
                        $playerdata[$newEntry]['charname'] = $playerinfo[5];
                        $playerdata[$newEntry]['charlevel'] = $playerinfo[7];
                }
                $i++;
        }
        return $playerdata;
}

?>

<html>
<head><title> Players online </title></head>

<body>
<h3> Server status </h3>

<?php

$fp=Connect();
$serverinfo = QueryServer($fp);
Disconnect($fp);

$server = ParseServerInfo($serverinfo);
$moduledesc = ParseModuleInfo($serverinfo);
$playerdata = ParsePlayerList($serverinfo, $server['numplayers']);

/*****************************************
 * Create HTML output                    *
 *****************************************/

// Output online / offline status

print "<p>";
print "Server is <b>";
       
if (strlen($server['gamename']) > 0)
{
        print "online</b></p>\\n";
       
        // Output server information
        ?>
       
        <table border=1>
        <caption>Full server information</caption>
        <tr>
          <th>Property</th>
          <th>Value</th>            
        </tr>

        <tr><td>Players</td> <td><?php echo $server['numplayers']?></td></tr>
        <tr><td>Max Players</td> <td><?php echo $server['maxplayers']?></td></tr>

        </table>


        <p><b>Module details</b><br><br>
        <?php echo $moduledesc ?></p>

        <?php

        // Output player information
        if ($server['numplayers'] > 0)
        {
                if ($server['numplayers'] == 1)    
                        print "<p>There is one player online.</p>\\n";  
                else
                        print "<p>There are " . $server['numplayers'] . " players

online.</p>\\n";  
                ?>
               
                <table border=1>
                <caption>Playerlist</caption>
                <tr>
                  <th>Player</th>
                  <th>Character</th>            
                  <th>Level</th>                
                </tr>

                <?php
                foreach ($playerdata as $player)
                {
                        print "<tr>\\n  <td>" . $player['playername'] . "</td>\\n";
                        print "  <td>" . $player['charname'] . "</td>\\n";
                        print "  <td>" . $player['charlevel'] . "</td>\\n</tr>\\n";      

 
                }
                print "</table>";
        }
        else
        {
                print "<p>There are no players online.</p>\\n";
        }        
       
}        
else  // server hostname is empty
{
        print "offline</b>. No additional info available, sorry.</p>\\n";
}      


?>

</body>
</html>

               
               

               


                     Modifié par henesua, 04 janvier 2012 - 03:10 .
                     
                  


            

Legacy_Knight_Shield

  • Hero Member
  • *****
  • Posts: 812
  • Karma: +0/-0
PHP - NWServer Query
« Reply #1 on: October 03, 2013, 09:39:07 pm »


               Here is one I use from vault.

<html>
<head>
<body background="#000000" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<title>Name of Server</title>
<style type="text/css">
<!--
.r {
   color: #00ff00;
}
-->
</style>
</head>
<body>
<center>
<?php
$ipaddr = "12.34.56.78";
$port = "5121";
$timeout = 5;
$connect = fsockopen( "udp://" . $ipaddr, $port, $errno, $errstr, $timeout );
if ( ! $connect )   {
   print( "<h1>Server is down</h1><br>\\n" );
   print( "<h3>ERROR: $errno - $errstr</h3><br>\\n" );
   exit;
} else {
   socket_set_timeout( $connect, $timeout );
   $send = "\\xFE\\xFD\\x00\\xE0\\xEB\\x2D\\x0E\\x14\\x01\\x0B\\x01\\x05\\x08\\x0A\\x33\\x34\\x35\\x13\\x04\\x36\\x37\\x38\\x39\\x14\\x3A\\x3B\\x3C\\x3D\\x00\\x00";
   fwrite( $connect, $send );
   $output = fread( $connect, 5000 );
   if ( ! $output ) {
      print ( "<h1>Server is down.<br></h1>\\n" );
   } else {
      $lines = explode( "\\x00", $output );
      print( "<u>Server IP:</u> <i class='r'>$ipaddr</i>:<i class='r'>$port</i><br>\\n" );
      print( "<u>Server Status:</u> <i class='r'>Online</i><br>\\n" );
      print( "<b>Game Category:</b> <i class='r'>" );
      if ( $lines[16] == "274" ) {
         print( "Action</i><br>\\n" );
      } elseif ( $lines[16] == "363" ) {
         print( "Story</i><br>\\n" );
      } elseif ( $lines[16] == "364" ) {
         print( "Story Lite</i><br>\\n" );
      } elseif ( $lines[16] == "275" ) {
         print( "Role Play</i><br>\\n" );
      } elseif ( $lines[16] == "276" ) {
         print( "Team</i><br>\\n" );
      } elseif ( $lines[16] == "365" ) {
         print( "Melee</i><br>\\n" );
      } elseif ( $lines[16] == "366" ) {
         print( "Arena</i><br>\\n" );
      } elseif ( $lines[16] == "277" ) {
         print( "Social</i><br>\\n" );
      } elseif ( $lines[16] == "279" ) {
         print( "Alternative</i><br>\\n" );
      } elseif ( $lines[16] == "278" ) {
         print( "PW Action</i><br>\\n" );
      } elseif ( $lines[16] == "367" ) {
         print( "PW Story</i><br>\\n" );
      } elseif ( $lines[16] == "368" ) {
         print( "Solo</i><br>\\n" );
      } elseif ( $lines[16] == "370" ) {
         print( "Tech Support</i><br>\\n" );
      }
      print( "<u>Play Type:</u> <i class='r'>$lines[2]</i><br>\\n" );
      print( "<u>Game Name:</u> <i class='r'>$lines[3]</i><br>\\n" );
      print( "<u>Module Name:</u> <i class='r'>$lines[4]</i><br>\\n" );
      print( "<u>Version Number:</u> <i class='r'>$lines[14]" );
      if ( $lines[20] == '1' ) {
         print( " / XP-1 " );
      } elseif ( $lines[20] == '2' ) {
         print( " / XP-2 " );
      } elseif ( $lines[20] == '3') {
         print( " / XP-1 / XP-2 " );
      }
      print( "</i><br><br>\\n" );
      
      print( "<u>Level Range:</u> <i class='r'>$lines[7]</i> - <i class='r'>$lines[8]</i><br>\\n" );
      
      print( "<u>Player vs. Player:</u> " );
      if ( $lines[9] == "NONE" ) {
         print( "<i class='r'>" );
      } elseif ( $lines[9] == "FULL" ) {
         print( "<i class='r'>" );
      } else {
         print( "<i class='r'>" );
      }
      print( "$lines[9]</i><br>\\n" );
      
      print( "<u>Character Vault:</u> " );
      if ( $lines[19] == '1' ) {
         print( "<i class='r'>Local Vault</i><br>\\n" );
      } else {
         print( "<i class='r'>Server Vault</i><br>\\n" );
      }
      print( "<u>Only One Party:</u> " );
      if ( $lines[12] == '1' ) {
         print( "<i class='r'>enabled</i><br>\\n" );
      } else {
         print( "<i class='r'>disabled</i><br>\\n" );
      }
      
      print( "<u>Player Pause:</u> " );
      if ( $lines[13] == '1' ) {
         print( "<i class='r'>enabled</i><br>\\n" );
      } else {
         print( "<i class='r'>disabled</i><br>\\n" );
      }
      
      print( "<u>Item Level Restrictions:</u> " );
      if ( $lines[18] == '1' ) {
         print( "<i class='r'>enabled</i><br>\\n" );
      } else {
         print( "<i class='r'>disabled</i><br>\\n" );
      }
      print( "<u>Enforce Legal Characters:</u> " );
      if ( $lines[17] == '1' ) {
         print( "<i class='r'>enabled</i><br>\\n" );
      } else {
         print( "<i class='r'>disabled</i><br>\\n" );
      }
      
      print( "<u>Password:</u> " );
      if ( $lines[10] == '0' ) {
         print( "<i class='r'>not required</i><br>\\n" );
      } else {
         print( "<i class='r'>required</i><br>\\n" );
      }
      
      print( "<u>Players:</u> <i class='r'>$lines[5]</i> / <i class='r'>$lines[6]</i><br><br>\\n" );
      $dlines = explode( "\\n", $lines[15] );
      print( "<u>Server Description:</u><br>" );
      print( "<i>\\n" );
      foreach ( $dlines as $dline ) {
         print( "$dline<br>\\n" );
      }
      print( "</i>\\n" );
   }
}
fclose( $connect );         
?>
</center>
</body>
</html>