Differences
This shows you the differences between two versions of the page.
layarmmo-users [2012/05/11 08:12] matthewleach created |
layarmmo-users [2012/11/09 12:55] (current) matthewleach Added some description for the instructions |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | == Tracking Users == | + | ==== Tracking Users ==== |
- | Process: | + | ===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: | ||
<code> | <code> | ||
Connect to database | Connect to database | ||
- | Search for user, using their unique id | + | Search for user using their unique id |
If no user was found, then add them to the database and search again | If no user was found, then add them to the database and search again | ||
Line 12: | Line 16: | ||
</code> | </code> | ||
- | And the actual code: | + | ===And the actual code=== |
- | <code php> | + | Add the code below to the top of your main php file, just underneath the opening <?php |
- | <?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. | ||
+ | |||
+ | <code php> | ||
$latitude = $_GET['lat'] + 0.00003; | $latitude = $_GET['lat'] + 0.00003; | ||
$longitude = $_GET['lon']; | $longitude = $_GET['lon']; | ||
Line 64: | Line 70: | ||
</code> | </code> | ||
- | Might look a bit scary - explain. | + | ===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. | ||
- | Add something to the response to view player stats | + | 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. |
<code> | <code> | ||
"layer": "mmodemo", | "layer": "mmodemo", | ||
- | "actions": [ | + | "actions": [ |
- | { | + | { |
- | "uri": "http://www.somesite.com/stats-page.php", | + | "uri": "http://www.somesite.com/stats-page.php", |
- | "label": "ST:'.$userST.' T:'.$userT.' HP:'.$userHP.'", | + | "label": "ST:'.$userST.' T:'.$userT.' HP:'.$userHP.'", |
- | "contentType": "text/html" | + | "contentType": "text/html" |
- | } | + | } |
- | ], | + | ], |
</code> | </code> |