Here is the php script (Thanks to "Helios" for this):
<?
// Master Server
// Copyright 2008 Binary Phoenix
// This script is pretty simple, it allows you to pass an argument to this file that
// specifies what statistic you want. Normally this will be a request to get all
// servers running for a given game.
// MySql DB Structure;
//
// - servers (table)
// - ID, int, auto_increment, primary key
// - IP, tinytext
// - Name, text
// - TimeoutTimer, int
// - GameName, text
// Arguments;
//
// - action : This specifies the action to preform, it can be any of the following;
// - listservers : This will cause the script to emit a list of currently
// running servers. This must be used with the gameid argument
// to specify what game the servers must be running.
// - addserver : This will add the current ip to the server list. This must be
// used with the gameid argument to specify what game the server is be running.
// - removeserver : This will remove the current ip from the server list.
// - refreshserver: Resets the servers timeout timer so that its not removed from
// the server list.
//
// - gamename : This is used by several actions to decide what game the servers it
// is dealing with should be running.
// - servername : This is used when adding a server to the list. It just contains
// a name based description of the server.
// - ip : Returns the current ip address.
//
// Errors;
//
// The server can return certain error strings depending on the situation, what follows
// is a list of them;
//
// - "error 1" : This is a mysql error. Its usually temporary.
// - "error 2" : Argument missing. This occurs when the request url dosen't
// contain an required argument.
// This variable stores how long it takes for a server to timeout.
$timeoutDelay = 60; // 1 minutes.
// Connect to the database.
$connection = mysql_connect("localhost", "InsertDataBaseUsername", "InsertDataBasePassword") or die("error 1");
mysql_select_db("InsertDataBaseName") or die("error 1");
// Make sure we have been given an action.
if (isset($_GET['action']))
{
// Process the action.
switch ($_GET['action'])
{
case 'ip':
die($_SERVER['REMOTE_ADDR']);
break;
case 'listservers':
// Check correct arguments have been passed.
if (!isset($_GET['gamename'])) die("error 2");
// Get required arguemnts and clean them up.
$gamename = $_GET['gamename'];
// Kill off any servers that haven't refreshed themselfs in a long time.
mysql_query("DELETE FROM servers WHERE TimeoutTimer <= " . time());
// Grab server list.
$result = mysql_query("SELECT * FROM servers WHERE GameName='" . mysql_escape_string($gamename) . "'");
if ($result)
{
// Emit all the servers.
while ($row = mysql_fetch_assoc($result))
{
echo $row['IP'] . '|' . $row['Name'] . "\n";
}
}
break;
case 'addserver':
// Check correct arguments have been passed.
if (!isset($_GET['gamename'])) die("error 2");
if (!isset($_GET['servername'])) die("error 2");
// Get required arguemnts and clean them up.
$gamename = $_GET['gamename'];
$serverName = $_GET['servername'];
// Remove any old servers with the same ip and name.
mysql_query("DELETE FROM servers WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "' and Name='" . mysql_escape_string($serverName) ."' and GameName='" . mysql_escape_string($gamename) . "'");
// Insert it into the database.
mysql_query("INSERT INTO servers(IP, Name, TimeoutTimer, GameName) VALUES('" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "','" . mysql_escape_string($serverName) . "'," . (time() + $timeoutDelay) . ",'" . mysql_escape_string($gamename) . "')");
break;
case 'removeserver':
// Remove this server based on its ip.
mysql_query("DELETE FROM servers WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "'");
break;
case 'refreshserver':
// Refersh the servers timeout timer.
mysql_query("UPDATE servers SET TimeoutTimer='" . (time() + $timeoutDelay) . "' WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "'");
break;
}
}
else
{
die("error 2");
}
// Close database connection.
mysql_close($connection);
?>
|