Once you get your CMS or HTML page setup to display the games correctly, passing all the necessary information, you now need to make sure your server can catch the instuctions from the games and do what needs to be done.
Any time a pnFlashGames enabled game sends an instruction to the server it sends a few standard things. First of all, all communication between the game and the server is done via POST. A game always sends "gid". If the component in the game is version 1.0 or later, it will also send any extra vars passed to it (see section 2 above). All instructions are sent to the pn_script you specified and the first value pair will be pn_modvar=pn_modvalue (again, see section 2 above). Then it will send out func=???, telling the script what it is trying to do. Below you will find an explanation for the four different possible funcs a game might request.
Your CMS or Forum probably has API functions that will tell you if the current user is logged in or not. Those functions will also be able to provide the username of the player. For obvious security reasons, you would want to get that information from the CMS and not from the game because it is far to easy to trick a flash game. If your CMS or forum does not have any predefined functions for getting that information you may need to write your own. Alternatively, there are open source projects that are providing this API for some systems. For example, there is a project on SourceForge providing an extensive API for the Invision BBS.
These instructions will tell you to "send information to flash". This sounds complicated, but it isn't. Flash does a cool trick when it is communicating with a server. When flash sends information to the server it stops to listen for the response. That means that from PHP, to talk back to the movie all you have to do is issue print or echo statements and send variables back in URL encoded format. The examples below will help that make more sense, but it is very simple to do.
When a game requests storeScore it will send an additional variable, "score". Your script should then take that variable and save it in a database or flat file of some sort. Make sure you save it and associate that score with the game and the user currently playing it. You can save processing time by first checking to see if the current user is logged in, and if not you can exit the function quickly. If the score is saved successfully then you should have your function print &opSuccess=true&endvar=1
If the score is not saved succesfully, then you should print &opSuccess=false&error=Insert error msg here if possible&endvar=1
The saveGame function is used to store string (textual) information somehow for retrieval later on (see loadGame() below). Along with the usual variables, the game will send gameData. gameData is a string value, regardless. The trick to this function just making sure you store the gameData exactly as it is passed to the server from the game. If you encode the information in any way, just make absolutely sure you unencode it before you pass it back with loadGame(). If the operation is successful, print &opSuccess=true&endvar=1
If the operation is not successful, then as usual you can print &opSuccess=false&error=Insert error msg here if possible&endvar=1
This function checks for data previously saved with saveGame for the given user and returns it. If no data was found it returns an empty string (""). The reply should be structured like this (where $gameData is a variable containing the data retrieved from the database): &opSuccess=true&gameData=$gameData&endvar=1
Remember that you want to get only data for this particular game and this particular user. So when you do your SELECT statement, make sure to filter for both those attributes. Of course if the operation fails, then you can return the normal failure statement: &opSuccess=false&error=Insert error msg here if possible&endvar=1
Any time you are sending information to the game that could be specially formated, such as the gameData, make sure you use the PHP function urlencode() in order to make sure that Flash can recieve and interpret the data properly. Failure to do this could result in the game not functioning properly or at all.
This function prints out an XML formatted list of the scores for a given game. The gid is passed (as usual) and you should retrieve the list of scores from the database. Then, format the scores like this:
<scorelist> <score rank='1' score='123' player='Example Player' date='7-8-2004' /> <score rank='2' score='120' player='AnotherPlayer' date='7-6-2004 /> <score rank='3' score='98' player='Yet_another_player' date='7-12-2004 /> </scorelist>
As usual, when sending specially formated information such as with the loadGame() method, make sure you use the PHP function urlencode() in order to make sure Flash can recieve it properly. So for example, lets say you have stored the XML scorelist in a variable called "$scorelist", this is how you encode it just before you send it to flash:
$scorelist = urlencode($scorelist);
Once compiled, you simply print this line: &opSuccess=true&gameScores=$scorelist&endvar=1
If no scores were found, simply send the XML list opening and closing tag with no "<score>" elements in it. The component will handle the rest.