Lennaerts PHP mysql multiplayer tutorial

Index

Attached Files

The following files have been attached to this tutorial:

.capx

multiplayer.capx

Download now 501.64 KB

Stats

5,677 visits, 15,752 views

Tools

UpdatePlayerPositions and data clusters

The next bit might not be so advanced, but the difficulty lays here in the compactness of the data ... and how the itterating through it is done.

Also, the following bit of PHP already includes the relaying back to construct 2 of messages and shots fired. Though, we do not have any data yet.

Looking at the last action from on request complete checkplayer, you can see we directly follow up with calling the requesting function call, passing request parameter of "updateplayerpositions", combined with data relating to our player followed by our CheckCode.

The second paramter is crucial:

int(playerbase.X) & "-"& int(playerbase.Y) &"-"& int(playerbase.Angle)&"-"&player.unitid

when formed, it should look something like 50-100-0-1 (x-y-angle-database_ref_id)

In PHP we pull these values apart with explode !!! YES oh wait, it wont go boom

if I do

     $locs = explode("-", $_POST[extra]);  

then the $locs variable will give me the following result:

    $locs[0] == 50  x
    $locs[1] == 100  y 
    $locs[2] == 0  angle
    $locs[3] == ID 
    

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.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!