Tracking Users

Process

Whenever Layar passes your server a request from somebody, it will include a unique ID. This normally relates to the device (e.g. phone, tablet) that they are using, but we'll use it to identify specific players. This isn't true if people share a device, but it will do for now.

The process that we are going to use is described below:

Connect to database
Search for user using their unique id

If no user was found, then add them to the database and search again

Extract user details so we can use them

And the actual code

Add the code below to the top of your main php file, just underneath the opening <?php

Most of the code is made up of calls to the database; but hopefully there are enough comments to make the overall process obvious. Try comparing it to the steps outlined above. The code is described in a bit more detail underneath.

$latitude = $_GET['lat'] + 0.00003;
$longitude = $_GET['lon'];
$userRef = $_GET['userId'];
 
$userID = -1;
$userST = 0;
$userT = 0;
$userHP = 0;
 
 
include 'dbinfo.php';
 
// Search for existing user
$sql = $db->prepare( "SELECT id, st, t, hp FROM players WHERE ref=:userRef" );
$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
$sql->execute();
 
// How many were found (should be 1 or 0)
$count = $sql->rowCount();
 
// First check
if ($count == 0){
	// Not found. Create an entry for the user
	$sql = $db->prepare( "INSERT INTO players (ref, st, t, hp) VALUES (:userRef, 6, 6, 30)" );
	$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
	$sql->execute();
 
	// Should now be added, so search and try to find them
	$sql = $db->prepare( "SELECT id, st, t, hp FROM players WHERE ref=:userRef" );
	$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
	$sql->execute();
 
	// How many were found (should be 1 or 0)
	$count = $sql->rowCount();
}
 
// This should always be 1, unless something went badly wrong
if ($count == 1){
	// Found. Save the player's ID for later
	$results = $sql->fetchAll(PDO::FETCH_ASSOC);
	$userID = $results[0]['id'];
	$userST = $results[0]['st'];
	$userT = $results[0]['t'];
	$userHP = $results[0]['hp'];
}

Database PDO Code

There are several different ways to connect to a database, and the code above uses a system called PDO. The PDO system is used in Layar's own examples, and is one of the currently recommended approaches.

The database connections start with a 3-step process:

  • Prepare - Create a query for the database, including placeholder names for the variables (values that will be different each time)
  • Bind - Map actual values to the placeholders that you included in the previous step. There will be a bind for each variable.
  • Execute - Run the query and store the result.

Once executed you can use different commands to see what the result was. For example $sql→rowCount() will tell you how many matches were found in the database, while $sql→fetchAll(PDO::FETCH_ASSOC) gives you an array of the actual results.

Viewing Stats

So now everytime a new user opens your layer it will create them a new account; and everytime an existing player opens your layer it will load their stats.

How will the player know what their current stats are though? Well for that we are going to have to fudge things a little. Layar is a generic platform, so trying to shoehorn an mmo into it will lead to some awkward compramises.

In this case we can't have a standard HUD, but we can attach some information to the layer itself. Add the code shown below to the top of your layer output (use the line - “layer”: “mmodemo” - to see where to include it). This will add a 'Layer Action', which can be accessed through the options button in the Layar app. It will show the current stats, and provide a link to webpage that we haven't created yet.

"layer": "mmodemo",
    "actions": [
        {
            "uri": "http://www.somesite.com/stats-page.php",
            "label": "ST:'.$userST.' T:'.$userT.' HP:'.$userHP.'",
            "contentType": "text/html"
        }
],