ShiVa Flash: XML Highscore Example – ShiVa Engine

ShiVa Flash: XML Highscore Example

This tutorial is a follow-up to last week’s ShiVa Flash: XML and crossdomain.xml tutorial. Today, we are programming a real-world example: We will be using the XML interface in order to store and receive a highscore list on a remote server.

File Structure

In order to make things work, you need to place the crossdomain.xml at the root level of your domain. Both highscore.php and highscore.xml have to be addressed from within your ShiVa application, you will need the full URL. The location of the SWF directory does not matter.

To learn more about the crossdomain.xml, please check last week’s ShiVa Flash: XML and crossdomain.xml tutorial.

Highscore XML Structure

All high scores will be stored in a single XML file. It has the following structure:

<HighScore>
<entry name="Player 1" score="0815" date="2012-01-02"/>
<entry name="Player 2" score="0814" date="2012-01-03"/>
<entry name="Player 3" score="0813" date="2012-01-04"/>
<entry name="Player 4" score="0812" date="2012-01-05"/>
<entry name="Player 5" score="0811" date="2012-01-06"/>
</HighScore>

To add our personal high score, we need to add a new <entry /> element to that list.

ShiVa Send and Receive

The whole sample works with a single user AI. Sending and Receiving is done using states, closely resembling the ShiVa XML manipulation tutorial.

XML_PHP and XML_URL are public member variables that need to be adjusted to fit your server.
Clicking on the HUD buttons will send either a “onHUDButtonLoadXML” or a “onHUDButtonSendXML” event to the main user AI, which will call the states Send() and Receive() respectively.

PHP Bridge

The heart of the whole implementation is the PHP backbone script that receives, sorts, and writes all data into the highscore XML file. To keep things simple, this script does almost no error checking and should not be used as-is.

<?php
	//globals -----------------------------------------
	$filename = "./highscore.xml";
	$linenum = 0;
	$newxmlarray=array();
	//read POST -----------------------------------------
if ( isset($_POST['STContent'] ) ) {
	//new XML content sent from SHiVa
	$xml_add = stripslashes($_POST['STContent']);
	//get the score for sorting later on
	$grep = strpos( $xml_add, "score="" );
	$newscore = substr( $xml_add, $grep+7, 4 );

After setting up some global variables, the script accepts the POST content. In a real game, make sure you check the input for validity! Next up, we will read the highscore.xml file into the cache, analyze all entries for the score and compare them to our new score. This replaces a sorting algorithm and though it is a little crude, it serves our purpose and is really fast.

	//read xml to array -----------------------------------------
	$lines = file($filename);
	$countlines = count($lines);
	foreach($lines as $line) {
		//search for score, match only 4-figure numbers
		$moregrep = preg_match ( '/"[0-9]{4}"/', $line ) ;
		if ($moregrep > 0) {
			$grep = strpos( $line, "score="" );
			$oldscore = substr ( $line, $grep+7, 4 );
			if ( intval($oldscore) <= intval($newscore) ) {
				//found lesser score
				break;
			}
		}
	$linenum += 1;
	}

Now we have an entry point for the new XML line. Let’s compose the new XML and write it to disk:

	//compose new XML order-----------------------------------
	for ( $k=0; $k<$linenum; $k++ ) { $newxmlarray[] = $lines[$k]; }
	$newxmlarray[] = $xml_add."
"; //add our new highscore, including a line break - important!
	for ( $j=$linenum; $j<=$countlines-1; $j++ ) { $newxmlarray[] = $lines[$j]; }
	//write xml to disk-----------------------------------------
	if (is_writable($filename)) {
		$handle = fopen($filename, 'w');
		for ($j=0; $j<count($newxmlarray); $j++) {
		    fwrite( $handle, $newxmlarray[$j] ) ;
		}
	    fclose($handle);
	}
	//-----------------------------------------
} else { echo "Error."; }
?>

Flash Sample

Click on the Image below to open a new window with a working ShiVa Flash Sample.

Keep in mind that the Flash exporter is still BETA. Sometimes, the highscore list will not refresh instantly, but sending works every time! If you do not see any changes after sending, do not click wildly on the Send button, but wait for 30 seconds and then refresh the list.


  • slackBanner