With all the apparent hype over cross-server saving, I've decided to have my go at it and whipped up a nifty little piece of work (not fully complete) in the past two hours or so that currently handles inserting and updating user fields to an external SQL database using PHP as a middleman.
database
{
var
{
processor = "http://localhost/byond_db/processor.php"; // the location of the database connect page
Parser/dbParser = null;
client/user = null;
}
New() { . = ..(); }
proc
{
initialize( var/client/USER )
{
user = USER;
dbParser = new /Parser();
}
check_db()
{
var http[] = world.Export( processor );
if( !http ) return FALSE;
var/F = http["CONTENT"];
var/content;
if(F) content = html_encode(file2text(F));
if( dbParser.ParseBetween( content, "<dbcon>", "</dbcon>" ) == "Connected to database." ) return TRUE;
else return FALSE;
}
}
}
MySQL
{
var
{
database/db = null;
}
New( database/DB )
{
db = DB;
}
proc
{
// updates a current entry
// if the entry is not in the database, then it will be created.
update( var/table, var/list/arg_list )
{
if( !db.check_db() ) return;
var url = db.processor;
url = "[url]?action=update&table=[table]";
for( var/DATA_TYPE/argument in arg_list ) url = "[url]&[argument.field]=[argument.value]";
var http[] = world.Export( url );
if( !http ) return FALSE;
var/F = http["CONTENT"];
var/content;
if(F) content = html_encode(file2text(F));
if( db.dbParser.ParseBetween( content, "<updateStatus>", "</updateStatus>" ) == "Update successful." ) return TRUE;
else return FALSE;
}
read()
{
// write this function! and then saving is done!
}
}
}
DATA_TYPE
{
var
{
field;
value;
}
New( var/FIELD, var/VALUE )
{
. = ..();
field = FIELD;
value = VALUE;
}
}
You don't want to see the PHP code right now - it's far too messy, but if you were interested, it essentially parses each GET parameter and inserts into a table created to accept such parameters and values. Effectively, if I pass an argument_list holding DATA_TYPE with field="name" and value="cauti0n", it will place it in my SQL database, in the table "characters", column "name" with value "cauti0n".
It seems that a decent amount of people disapprove of this method of handling the saving, namely because of server reliability outside of BYOND. Unfortunately, there is more of a reason why I am working on this outside of just BYOND - I'm working on something that sort of ties BYOND and websites together, effectively requiring PHP as the middleground. However, I'm sure I'm not the first person to create something like this and as such, I was wondering if people had any nice performance tests that showed the reliability of this sort of system. I imagine that if the SQL database is hosted on a pretty decent server, there should be very minimal issues with this sort of process (unless someday I happen to have thousands of players playing my BYOND game).
Any information from others who have attempted something like this?
Note: Feel free to take this/modify this as you see fit. My career isn't bent on what I do in BYOND, so my thoughts on credit don't matter too much. As far as I see it, anything I do here can be open-source and I would never mind :). The above is fairly modular, and plug-in-play if you have a responding PHP file. The only other exception is the Parser, but that's a very minimalistic parser - you can create yours yourself if you wish. It's all an independent file, though :)