Game server list

BlitzMax Forums/BlitzMax Programming/Game server list

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Well the method I use is to have a simple php script on a web server which each server can simply make requests to, to add and remove themselfs from the list (they also need to send requests every 50 seconds or so (this can be changed) otherwise they get removed from the list. This is almost exactly the same method that GNet uses, the only real difference is the following script is somewhat more expanded .

<?

	// 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);

?>


Within blitzmax all you need to do to make requests to this script, is to include the brl.httpstream module and open a stream to the script using the correct parameters and read in the first line (which will be an error number if something has gone wrong).

For example to add a server you would open a stream to;

local stream:TStream = OpenStream("http::www.YourDomain.com/script.php?action=addserver&gamename=YourGameName&servername=YourServerName")
local result:string = ReadLine(stream)
CloseStream(stream)


And so forth. Hope that helps. Not sure if the code will work I haven't tested it with blitz yet.