How do I implement FB Instant Game Leaderboard?

0 favourites
  • 11 posts
From the Asset Store
Easy Leaderboard is a simple 3-column (position, name, score) leaderboard with a customizable appearance
  • Hi everyone,

    I was searching for hours now how to implement a Leaderboard on my FB Instant Game. It's a very simple runner, so I just need to display the player's rank, ID and score. Everything is done on the facebook for dev page, I can even play my game.

    But, that's crazy, I can find a proper example capx file on the forum or either on the tutorial page.

    I found those topics:

    construct.net/en/forum/construct-3/general-discussion-7/fb-instant-games-leaderboard-133334

    construct.net/en/forum/construct-3/general-discussion-7/fb-instant-games-leaderboard-149192

    But .capx links are broken or there is no example attached.

    I'm not a dev, just a game designer that's why I love to create on Construct but I feel very helpless with this.

    Please if anyone has a file to share I would be very thankful. 🙏

  • FB instant game is not support leaderboard any more.

  • If you want to add a leaderboard in your game i suggest you to make your own leaderboard system using ajax and some phps, this would be help you adding leaderboards in any of your games.

    You only need a place to host a MySQL database "i use hostinger" , some phps files and ajax.

  • Well, as I can see non-contextual Leaderboards are still available…

    In any case, I did try first to add my own leaderboard using this tutorial:

    https://www.construct.net/en/tutorials/creating-your-own-leaderboard-highscores-easy-and-free-php-mysql-1032

    It's quite good but not complete. There is no global rank, or pages and it's not using a single score per user (which is quite important for a runner, as I want only to keep the best score).

    Another thing that could be great is to have the possibility to use facebook ID to connect to this leaderboard. I'm quite sure that it's possible.

    Maybe if I get some help I could be able to make this kind of leaderboard and post it as a tutorial for all the people (non-dev) who are just in the same situation.

  • Yes, non contexual are not enough.

    I have allready implement a leaderboard in a game that store the fb uniqe id, picture, name, score and rank and retreive it by rank.

    It take time to make a tutorial and now i have No time for make this due to my first work.

    I only work on construct 30 minutes 2 days in a week, so If i found some time this weekend i will send you some informations.

  • Could be great. savvito123 I understand that time is precious, I also cannot spend much time as I work during the week. I have a bit more time actually because I work from my place but if you can manage to give me some information I would be glad to write this tutorial if I understand a bit how it works.

    I spend so much time looking for a proper example on the forum… It really deserves to do something now for every guy in the same situation.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So how its possible to send some codes while this?

    Im writing about 1 hour here and it want let me upload it.

    Hmmmm, solutions please....

  • Oh my…

    Could you post your code here?

  • Hello,

    I will try to make it as soon as i can.

    It means you have allready create a database somewhere (i use hostinger) it accept PHP 7.2.

    On start of layout check if that is the first run of the game (use a global variable "firstRun" = 0)

    and run this action.

    saveinfo.php must create it and save it to a folder to you site file manager.

    DOMAIN_SCORE variable is your filepath.

    We run this because we do not want every time the game end and return to menu to run the saveinfo.php

    This php file look if a player compared by his uniqe id (uniqe id is the fb instant game id or whatever platfor you want) allredy exist.If its not exist it create a row in your database with (uniqe,name,pics,wins and loses)

    Notice that i delete the first and last line (<php.....?>) because want let me insert it.

    saveinfo.php

    header('Access-Control-Allow-Origin: *');

    $servername = "localhost";

    $username = "***********";

    $password = "**************";

    $dbname = "*************";

    // Create connection

    $conn = new mysqli($servername, $username, $password, $dbname);

    $name = strip_tags(mysqli_real_escape_string($conn, $_GET['name']));

    $uniqe = strip_tags(mysqli_real_escape_string($conn, $_GET['uniqe']));

    $pics = strip_tags(mysqli_real_escape_string($conn, $_GET['pics']));

    $win = strip_tags(mysqli_real_escape_string($conn, $_GET['win']));

    $lose = strip_tags(mysqli_real_escape_string($conn, $_GET['lose']));

    // Check connection

    if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

    }

    $sql = "SELECT id, name, uniqe, pics FROM scores WHERE uniqe='$uniqe'";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

    // output data of each row

    while($row = $result->fetch_assoc()) {

    echo "id: " . $row["id"]. "uniqe: " . $row["uniqe"]. "pics: " . $row["pics"]. " - Name: " . $row["name"]. " " . "<br>";

    $sql =mysqli_query($conn, "UPDATE scores SET pics='$pics' WHERE uniqe='$uniqe'");

    }

    } else {

    echo "0 results";

    $sql = mysqli_query($conn, "INSERT INTO scores (name, uniqe, pics)

    VALUES ('$name','$uniqe','$pics');" );

    }

    $conn->close();

    On AJAX request "PostScore" complete we run this action

    This will retrieve a number of entries you want.

    getscores.php

    header('Access-Control-Allow-Origin: *');

    include 'login.php';

    // Connect to server and select database.

    $con = mysqli_connect($host, $db_username, $db_password, $db_name);

    // Retrieve data from database

    $sql = "SELECT *

    FROM scores

    ORDER BY score DESC

    LIMIT 100000000"; // The 'LIMIT 100000000' part will only read 100 scores. Feel free to change this value

    $result = mysqli_query($con, $sql);

    // Start looping rows in mysql database.

    while($rows = mysqli_fetch_array($result)){

    echo $rows['name'] ." ". $rows['score'] ." ". $rows['pics'] ." ". $rows['uniqe'] ." ". $rows['lose'] ." ". $rows['win'] . "|";

    }

    // close MySQL connection

    mysqli_close($con);

    You notice include 'login.php';

    Its a file that you create also in your file manager so no need every time to enter your informations.

    login.php

    header('Access-Control-Allow-Origin: *');

    $host = "localhost"; // Host name

    $db_username = "***********"; // Mysql username

    $db_password = "***********"; // Mysql password

    $db_name = "**************"; // Database name

    $db_table = "*********"; // Table name

    After you retreive the AJAX "getscores" request save the information to an array and run this action.

    ran.php file will retreive your inforamtion

    header('Access-Control-Allow-Origin: *');

    $servername = "localhost";

    $username = "***********";

    $password = "****************";

    $dbname = "****************";

    $mysqli = new mysqli($servername, $username, $password, $dbname);

    $uniqe = strip_tags(mysqli_real_escape_string($mysqli, $_GET['uniqe']));

    if( $result = $mysqli->query( "SELECT * FROM scores WHERE uniqe ='".$uniqe."'") )

    {

    while( $row = $result->fetch_assoc() ) {

    echo $row["name"]." ".$row["score"]." ".$row["win"]." ".$row["lose"]." ".$row["pics"]." ".$row["emo1"]." ".$row["emo2"]." ".$row["emo3"]." ".$row["spells"]." ". "<br>";

    }

    }

    After that you need to run this action

    This will run the rank1 php file and this file will rank all the players.

    header('Access-Control-Allow-Origin: *');

    $servername = "localhost";

    $username = "*************";

    $password = "*************";

    $dbname = "*************";

    $mysqli = new mysqli($servername, $username, $password, $dbname);

    $uniqe = strip_tags(mysqli_real_escape_string($mysqli, $_GET['uniqe']));

    $sql3 = $mysqli->query("SELECT * FROM scores ORDER BY score DESC") or die("Could not allocate information!");

    $rank = 0;

    while($row = mysqli_fetch_assoc($sql3)) {

    $rank++;

    if ($row['uniqe'] == $uniqe){

    echo $rank;

    }

    }

    The only think now is to send the score to the database.

    We run this action

    And the savewin php file is

    header('Access-Control-Allow-Origin: *');

    $servername = "localhost";

    $username = "*************";

    $password = "***************";

    $dbname = "***************";

    // Create connection

    $conn = new mysqli($servername, $username, $password, $dbname);

    $name = strip_tags(mysqli_real_escape_string($conn, $_GET['name']));

    $uniqe = strip_tags(mysqli_real_escape_string($conn, $_GET['uniqe']));

    $win = strip_tags(mysqli_real_escape_string($conn, $_GET['win']));

    $lose = strip_tags(mysqli_real_escape_string($conn, $_GET['lose']));

    // Check connection

    if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

    }

    $sql = "SELECT id, name, score, uniqe FROM scores WHERE uniqe='$uniqe'";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

    // output data of each row

    while($row = $result->fetch_assoc()) {

    echo "id: " . $row["id"]. "uniqe: " . $row["uniqe"]. " - Name: " . $row["name"]. " " . $row["win"]. " " . $row["lose"]. "<br>";

    $sql =mysqli_query($conn, "UPDATE scores SET win=win+'$win' WHERE uniqe='$uniqe'");

    $sql =mysqli_query($conn, "UPDATE scores SET lose='$lose'+lose WHERE uniqe='$uniqe'");

    $sql =mysqli_query($conn, "UPDATE scores SET score=(win/lose)*100 WHERE uniqe='$uniqe'");

    }

    } else {

    echo "0 results";

    $sql = mysqli_query($conn, "INSERT INTO scores (name, uniqe, win, lose)

    VALUES ('$name','$uniqe','$win','$lose');" );

    }

    $conn->close();

    Note that you maybe need to change the formula in the last php for sending the score.

    I forgot to mention , when is the first run , "firstrun = 0" run the actions and set the "firstrun to 1"

    When the players return to the menu and the firstrun = 1 just run the getscores php.

  • Update:

    On start of layout you have to set the "uniqe" string variable to the fb instant game uniqe id.

    Here is a way to fill your array

    Also when you retrieve images you have to URLdecode it.

    Good luck with that and remember, if you try to edit some php files search for MySQLi and not for MySQL.

  • Whoa, savvito123 your are fantastic my friend! Can't wait to test this! I did some progress on my version but I really can complete it now! Gonna post the result asap!

    Thank you so much!!

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