Game replay?

Get help using Construct 2

Post » Sun Jan 08, 2012 9:24 pm

@GingerBatMan, @Kyatric

Yeah I also had a look at strpos function

Came up with this, But it doesn't feel right:

<?php
// read variables from POST data.

$username = $HTTP_GET_VARS[name]; <-- DEFINE THE USER??
$log = $HTTP_GET_VARS[log]; // <-- MODIFY THIS TO SEND THE REPLAY??
$mystring = $HTTP_GET_VARS[CurrentUser];
$pos = strpos($mystring, $username);

// verify the username is set and not empty
if (!isset($username) || $username == "") {
    echo "failure";
    exit;
}

// verify the log is set and a number
if (!isset($log) || $log == "") {
    echo "failure";
    exit;
}
if ($pos === false) {
    echo "The string '$username' was not found in the string '$mystring'";
} else {
    echo "The string '$username' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

Then the read.php

<?php
/** Return a list of top logs. */
// read variables from GET data
$num_logs = $HTTP_GET_VARS[num_logs];

// read each line in logs.csv as a string into an array
$logs = file("logs.csv");

// define a comparator to sort items by log
function compare($s1, $s2) {
    // split the strings by their delimiter
    $a1 = explode("#", $s1);
    $a2 = explode("#", $s2);
    // compare the logs
    return $a2[1] - $a1[1];
}

// sort the array of logs
usort($logs, "compare");

// output the requested number of top logs
for ($i = 0; $i < $num_logs && $i < count($logs); $i++) {
    echo $logs[$i];
}
?>

Im not sure what im supposed to do the the read php to return the replay you need when you want it
smitchell2012-01-08 21:29:57
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Sun Jan 08, 2012 9:28 pm

Hope that helps
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Sun Jan 08, 2012 10:58 pm

Hum I remember something now.
You won't be able to save your 'log' variable.
The quantity of data you can send through a query string (ie: ?var=value) is limited.

The specification does not dictate a minimum or maximum URL length, but implementation varies by browser and version. For example, Internet Explorer does not support URLs that have more than 2083 characters.[8][9] There is no limit on the number of parameters in a URL; only the raw (as opposed to URL encoded) character length of the URL matters. Web servers may also impose limits on the length of the query string, depending on how the URL and query string is stored. If the URL is too long, the web server fails with the 414 Request-URI Too Long HTTP status code.
The common workaround for these problems is to use POST instead of GET and store the parameters in the request body. The length limits on request bodies are typically much higher than those on URL length. For example, the limit on POST size, by default, is 2 MB on IIS 4.0 and 128 KB on IIS 5.0.[10] the limit is configurable on Apache2 using the LimitRequestBody Directive which specifies the number of bytes from 0 (meaning unlimited) to 2147483647 (2GB) that are allowed in a request body [11].

Source: http://en.wikipedia.org/wiki/Query_string

And as far as I know, the Ajax object doesn't send POST requestYann2012-01-08 22:59:39
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sun Jan 08, 2012 11:16 pm

The custom AJAX POSTer does.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Mon Jan 09, 2012 12:27 am

Ok then, I had a little go at it
It's been a while since I last did some php/mysql so there might be some mistake but here's what I came up with :

Database:[code]table user
- 'id' bigint unsigned auto_increment primary_key
- 'name' varchar(50) unique
- 'password' varchar(256)

table games
- 'id' bigint unsigned auto_increment primary key
- 'id_user' bigint unsigned
- 'log' longtext
- 'score' bigint unsigned[/code]

connect.php[code]<?php
$username= 'plop';
$password= 'mystery';
$server = 'localhost';
$dbname = 'myDataBase';

$con = mysql_connect($server,$user,$password)
    or die('ERROR: Unable to connect to database: '.mysql_error());

mysql_select_db($dbname,$con);
?>[/code]

save.php[code]<?php
#connexion
include('connect.php');
#simplification
$id_user= isset($_POST['id_user']) ? $_POST['id_user'] : '';
$log    = isset($_POST['log']) ? $_POST['log'] : '';
$score = isset($_POST['score']) ? $_POST['score'] : '';

#check if all is ok
if($id_user && $log && $score)
{
#paranoiac escaping
$id_user= mysql_real_escape_string($id_user,$con);
$log    = mysql_real_escape_string($log,$con);
$score = mysql_real_escape_string($score,$con);

#check if the user exists
$query = '
    SELECT name
    FROM user
    WHERE id = '.$id_user
    ;
$result = mysql_query($query,$con)
      or die('ERROR: sh*tty request: '.mysql_error());

#if there's a user with this id
#we save
$rows   = mysql_num_rows($result);
if($rows)
{
    $query = '
      INSERT INTO games
      SET
        id_user='.$id_user.',
        log='.$log.',score='.$score
        ;
    mysql_query($query,$con)
        or die ('ERROR: sh*tty request: '.mysql_error());
    echo('SUCCESS');
    exit;
}
echo('ERROR: Wrong user id');
exit;
}
echo ('ERROR: Missing Data');
exit;
?>[/code]

load.php[code]<?php
#connexion
include('connect.php');
#simplification
$id_game= isset($_POST['id_game']) ? $_POST['id_game'] : '';

#check if all is ok
if($id_game)
{
#paranoiac escaping
$id_game = mysql_real_escape_string($id_game,$con);

#get the log with the username and the score... always usefull
$query   = '
      SELECT user.name, game.log, game.score
      FROM user,game
      WHERE game.id='.$id_game.'
      AND game.id_user=user.id
      ';
$result = mysql_query($query,$con)
      or die('ERROR: sh*tty request: '.mysql_error());
if($rows)
{
    $row = mysql_fetch_assoc($result)
    echo(
      $row['name']."\n".
      $row['log']."\n".
      $row['score']
      );
    exit;
}
echo('ERROR: No game found');
exit;
}
echo ('ERROR: Missing Data');
exit;
?>[/code]

game_list.php[code]<?php
#connexion
include('connect.php');
#simplification
$start    = isset($_POST['start']) ? $_POST['start'] : 0 ;        // start at the begining by default
$range    = isset($_POST['range']) ? $_POST['range'] : 10; // return 10 games by default
#paranoiac escaping
$start = mysql_real_escape_string($start,$con);
$range = mysql_real_escape_string($start,$con);

#get the list of games from start to range-1
$query = '
SELECT user.name, game.id, game.score
FROM user,game
WHERE game.id_user=user.id'
ORDER BY game.id ASC
LIMIT '.$start.','.$range
;
$result = mysql_query($query,$con)
or die('ERROR: sh*tty request: '.mysql_error());
$rows   = mysql_num_rows($result);
if($rows)
{
while($row = $mysql_fetch_assoc($result)
{
    echo($row['id'].','.$row['name'].','.$row['score']."\n");
}
exit;
}
echo('Error: nothing');
exit;
?>[/code]
Basically the only thing I didn't do is how to get the id of your user as it's something to do with how you want the user to log in. (session and stuff like that... meh... maybe you just need to call a get_id.php which return something like $_SESSION['id'] which you set in your login page...

But if you can get the id, basically to save a game you just have to do an ajax POST query with the id, the log and the score.

If you want a list of saved game, just call the game_list.php with the start (from which game) and the range (how much game) argument.
What's lacking maybe is a query to get the number of total games but meh... lazy.

If you want to replay a game, just call the load.php with the id of the game
You'll get something like :[code]username
score
x1,y1,a1
x2,y2,a2
x3,y3,a3
x4,y4,a4
...[/code]So you'll just have to strip the string from the two first token (username and score) and then you'll have the log variable back (:

Oh also if you look closely, all error report are begins with an "ERROR:" so you can check the ajax response and nicely display errors when you encounter them. (it won't be an ajax error but a php/mysql one so the ajax object won't return any error, just the string returned by php... I'm clear... I feel you understand....)
That's allYann2012-01-09 00:42:40
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Previous

Return to How do I....?

Who is online

Users browsing this forum: Braus, Draconinho, Magistross, nedzad, nimos100, R0J0hound and 30 guests