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 .