Game replay?

  • Hope that helps <img src="smileys/smiley1.gif" border="0" align="middle" />

  • 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 request

  • The custom AJAX POSTer does.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

    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

    connect.php

    <?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);
    ?>

    save.php

    <?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;
    ?>

    load.php

    <?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;
    ?>

    game_list.php

    <?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;
    ?>

    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 :

    username
    score
    x1,y1,a1
    x2,y2,a2
    x3,y3,a3
    x4,y4,a4
    ...

    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 all

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)