2014-01-09, 06:56 PM
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 -
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);
?>