MyBB Community Forums

Full Version: Help with a game server listing plugin.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am going to take a stab at writing a game server listing plugin for MyBB. I am totally new to this and I am researching the basics on how to write a plugin now.

If you have some additional links to tutorials (not sticky'ed here) I would appreciate you porting them here.

I am going to use this PHP code (see below) as the basis for having my game servers post their name, IP and player count. If you have a better piece of code I would be interested in it.

Basically what I want to do is setup game server accounts on my MyBB board and have the servers login and report their status. This should prevent spamming and provide a level of security. I also want to have a page setup so the clients can access server status data via HTTP get that is available publicly and does not include any HTML just plain text.

Additionally I would like make it so that is generates a proper server listing available in a browser for registered users to view.

Anyway I'm a plugin newbie and could use some guidence.

- Rich -


<?

	// 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 = 3600; // 60 minutes.
	
	// Connect to the database.
	$connection = mysql_connect("localhost", "DATABASE USER", "PASSWORD") or die("error 1");
	mysql_select_db("YOUR DATABASE") 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);

?>
See, there is already a similar plugin: https://forum.mybboard.pl/watek-mybb-ser...beta_44726 Smile
(2014-01-09, 06:56 PM)Vidiot_X Wrote: [ -> ]Anyway I'm a plugin newbie and could use some guidence.
- Rich -

As a general rule of thumb, you should avoid using mysql_* functions as they're deprecated and going to be removed in the future.

MyBB has a perfectly good database class that can be accessed using the $db variable.

There doesn't seem to be a direct question here but those are a few things you should really incorporate
@Snake_

eek Polish not English. I'll see what I can glean form it. Wink

@ectomatt
Really I'm looking for a bit of guidance (like what you have mentioned) and any additional doc's on writing plugins.

Thanks,
- Rich -

@Snake_

Actually that plugin is based on GameQ (http://gameq.sourceforge.net/) which looks interesting.
@ectomatt, the $db class seems to be only for forum connection though, it won't work if you want to connect to game server or any other database afaik. Then you need to set another connection.
(2014-01-11, 02:54 PM)Destroy666 Wrote: [ -> ]@ectomatt, the $db class seems to be only for forum connection though, it won't work if you want to connect to game server or any other database afaik. Then you need to set another connection.

Reading the documentation on the DB class it says they have methods like $db->connect: Connects a new database and $db->select_db: Selects the database for the current MySQL Session. But you are right, I suggested that because I just assumed he was working with the same database. He didn't really give that much detail into what exactly he wants to accomplish though
(2014-01-09, 09:43 PM)Snake_ Wrote: [ -> ]See, there is already a similar plugin: https://forum.mybboard.pl/watek-mybb-ser...beta_44726 Smile

This plugin displays way to many MySQL errors. Not recommended for use.

This one is a lot better but only works for orange box engine games; http://mods.mybb.com/view/mygameservers
hi all
just for knowledge of some who are interested in this
those both plguins work like a charm and they are very good idd
but there is a catch
ur webhost must have the gameserver ports opened
if not it will show servers offline..:X
taking that off the both are very good
i would use mygameservers plugin if it wasnt only for orange box engine games.....