this technique is applied with various bits of data when sending information to the PHP file in a parameter, this makes it more compact and requires less posting variables, keeping it relatively simple.
When PHP echoes something similair back, the strings will look like:
50[]100[]0[]1[-]
The sepperators are the [] sqaure brackets, and [-] square brackets with minus, indicates end of that set of strings.
So, you can have a string like:
data1|data2-1-1[]data2-1-2[-]data2-2-1[]data2-2-2[-]|data3|data4
The bold bit shows 2 sets of smaller cluster data.
So, here is the core, not the prettiest, but I wrote everything in procederual code, so most who have a little understanding of coding are able to understand.
if($_POST[request] == "updateplayerpositions"){
//Start building our relay string
$message = "updateplayerspositions|";
//Explode the variables from post to get x,y,angle,and ID
$locs = explode("-", $_POST[extra]);
//some simple sanitizing
$_POST[code] == stripslashes(mysql_real_escape_string($_POST[code]));
//We delete messages and bullets fired older then 8 seconds immediantly first, they are outside of any form of sync.
//This value may be lower, but I found it was a good value to start with
mysql_query("delete from shotsfired where stamped < DATE_SUB( NOW(), INTERVAL 8 SECOND)");
mysql_query("delete from messages where stamped < DATE_SUB( NOW(), INTERVAL 8 SECOND)");
//Next we update thee information from our own player, based on the based from the explode above
mysql_query("update players set locx='$locs[0]', locy='$locs[1]',playerangle='$locs[2]' where id='$locs[3]' and playercode='$_POST[code]'"); //double check on the id and code
//Loop through alll user apart from your self, and build the relay string.
$playersquery = mysql_query("select * from players where id != '$locs[3]'");
while($playerstates = mysql_fetch_array($playersquery)){
$message .= $playerstates[locx]."[]".$playerstates[locy]."[]".$playerstates[playerangle]."[]".$playerstates[id]."[]".$playerstates[playercode]."[]".$playerstates[state]."[-]";
echo mysql_error();
}
//Adding our returned check code
$message .= "|".$_POST[code];
//Following will be the set of data fom the shots fired, they get relayed too, if any.
$message .="|gameshots|"; //Indicates beginning of shots fired string.
//Check if there are any shots fired we need to process for our own payer, (shots done by enemies)
if(mysql_num_rows(mysql_query("select * from shotsfired where playercodes ='$_POST[code]'")) > 0 ){
//there should be shots, loop trhough all of them and build the string
$query = mysql_query("select * from shotsfired where playercodes ='$_POST[code]'");
while($results = mysql_fetch_array($query)){
$message .= $results[shooter]."[]".$results[angle]."[-]"; // who shot in what angle
mysql_query("delete from shotsfired where id='$results[id]'"); // As soon as we aded it to our string, delte from the list
}
}
//Grab some data about our own player
$playerdata = mysql_fetch_object(mysql_query("select * from players where id ='$locs[3]]'"));
//Is our player still alilve ?
if($playerdata->state == "yes"){
$message .= "|died"; // nope he died
} else {
$message .= "|alive"; //Yup still alive
}
//Adding our current kills and deaths to the string
$message.= "|".$playerdata->kills."|".$playerdata->killed."|";
//Adding message inteded for us, if any
$qmessages = mysql_query("select * from messages where playercodes='$_POST[code]' limit 1");
if(mysql_num_rows($qmessages) > 0) {
//A short workaround to add the messages indicator
$xs = 0;
while($rmessages = mysql_fetch_array($qmessages)){
if($xs==0) {
$message .= "messages|"; //This should only get added once
$xs = 1;
}
$message .= $rmessages['message']; // Message added to string
}
//Delete the messages, if more then one message, they are likely already older, and will clutter info, delete all.
mysql_query("delete from messages where playercodes='$_POST[code]'");
$message .= "|";
}
}
Pfew .... broke your mind ? not yet ? ... kewl ... Onwards !!!
Next Part will add game elements to work with the above return strings.