Lennaerts PHP mysql multiplayer tutorial

4
  • 42 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

multiplayer.capx

Download now 501.64 KB

Stats

11,922 visits, 28,321 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

The function Call and checkplayer with Ajax

Add these comon objects to your project:

Ajax, Keyboard, Audio, Touch, Function

Function with Ajax Call

Before the function calll; the checkcode gets set, if any, and used in the function call with 3 parameters to call 'requesting'. We will be using 1 function call for everything related to the Ajax postings.

If you take a closer look, you can see

"request="&Function.Param(0)&"&extra="&Function.Param(1)&"&code="&CheckCode

In every function call to requesting, we will make the first parameter, the request variable, and extra= will contain the second parameter, and the third obviously for the thrid which generally contains our CheckCode.

In the first call to request, we perform a request=checkplayer, with, if its present, sents the 3rd paramters CheckCode along for the ride.

In PHP I am going to coninue after what we have made thusfar, else it would become repetitive and tedious.

In PHP we are now awaiting for the Ajax call, the core of the functions will be defined by use the "requesting" variable. In this case, from layout start, checkplayer is request.

Here is the PHP side of things:

    if($_POST[request] == "checkplayer"){
    //Create codes
    $playeragent =  $_SERVER['HTTP_USER_AGENT'];
    $playerhost = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    $playerip=$_SERVER['REMOTE_ADDR'];
    //Check for existing code, or perhaps, we can regenerate the code:
    if(!empty($_POST[code])) {
        $playercode = stripslashes(mysql_real_escape_string($_POST[code]));
    } else {
    $playercode = md5($playeragent.$playerhost.$playerip);
    }
    //check for existing player code
    if(mysql_num_rows(mysql_query("select id from players where playercode = '$playercode'")) == 0){
    //player code does not exist, adding
    mysql_query("insert into players (playercode,state,kills,killed) values ('$playercode','no',0,0)");
    //Start building the return string, this is important
    $message= "playeractive|"; // Pipe sign is crucial
    } else {
            //The player code does exist, update status and kills to prevent cheaters
          mysql_query("update players set state='no',kills=0 where playercode='$playercode'");         $message="playeractive|";
    }
    $message .= $playercode; // Add our check code to the return
    //Grab the user ID which we will use for several game elements
    $playerid = mysql_fetch_object(mysql_query("select id from players where playercode = '$playercode'"));
    $message .= "|".$playerid->id; //We return the ID to the player as well
    }
    
    //The following will display the result string which is relayed back to Construct 2, we will keep this line on the end of the file, aways, every other function we will add later on, will be above this one.

echo $message;

    ?>

As you can see in the functionality in construct 2 from the "on request complete", wich also checks the string before the first pipe sign | , when it sees the first variable being "playeractive" it will update the CheckCode and set the unitid for the player.

This simplefied version does not distinct between new and existing players.

The next function we will pick up in the next part, as it is a bit bigger more advanced.

  • 0 Comments

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