Android Question A leaderboard for each level.

mare1980k1

Member
Licensed User
Longtime User
If it's online game, I would suggest that you connect the app on MySql like this:

You can then group players by levels and sort players by number of points in SQL...

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    ExecuteRemoteQuery("SELECT * FROM `LeaderBoard` WHERE `UsersLevel` LIKE 12345....100", "SelectLevel")
End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Try
        Dim job As HttpJob
        job.Initialize(JobName, Me)
        job.PostString("https://example.com/GetFromMySQL.php", Query)
    Catch
        Log (LastException.Message)
    End Try
End Sub

Sub JobDone(Job As HttpJob)
    Try
        ProgressDialogHide
        If Job.Success Then
            Dim res As String
            res = Job.GetString
            Log("Response from server: " & res)
            Dim parser As JSONParser
            parser.Initialize(res)
            Select Job.JobName
                Case "SelectLevel"
                    Dim AllPlayersInALevel As String
                    AllPlayersInALevel=""
                    Dim l As List
                    l = parser.NextArray
                    If l.Size>0 Then
                        For i=0 To l.Size-1
                            AllPlayersInALevel=AllPlayersInALevel&l.Get(i)
                        Next
                    End If
                    .
                    .
                    .
                    ToastMessageShow(AllPlayersInALevel,False)
            End Select
            
        Else
            Log(Job.ErrorMessage)
        End If
        Job.Release
    Catch
        Log (LastException.Message)
    End Try
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

PHP:
<?php

$databasehost = "serverurl.com";
$databasename = "NameOfDatabase";
$databaseusername ="Username";
$databasepassword = "Password";

$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{
   $rows = array();
   while($r = mysqli_fetch_assoc($sth)) {
     $rows[] = $r;
   }
   $res = json_encode($rows);
    echo $res;
    mysqli_free_result($sth);
}
mysqli_close($con);
?>
 
Upvote 0

mare1980k1

Member
Licensed User
Longtime User
P.S.
1. I guess it works, Informatix is amazing.
2. You can make anything you want, but you "have to make it", which is a bit harder than just "can make it".
3. The level leaderboard is not hard, the 100 levels I think will be much harder.
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Hello, we thought about using a mysql solution but soon we would have a problem.
It is not an online game. We just need a leaderboard for each level.
the game today has 100k users. If all users play all levels then: 100k x 100 levels. = 10,000,000 records. each level should have a query in the table. If 10% of the players play on the same day then ... the mysql homemade server would simply not work.
 
Upvote 0

mare1980k1

Member
Licensed User
Longtime User
You can still use mysql, but store only 10 high scores or 20.. Also when you run querry, you would just run querry for one level at a time, so it would be very fast.

So, the number of records would be a few thousands max.

And when someone competes, you would run a query to get the minimum score for that level. If player doesn't jump over the minimum, there is no need to run another query.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
mare has a point.

if you only need to know if a player is in the top 10 you only need to store the top 10 for each level and update it is a score is bigger than the lowest (10th).

if you need to know if the player's current score position is #5325 you need to store all scores for all levels.

even in the second case we talk about a few mega bytes of data tables so lookups should be fast enough.
 
Upvote 0
Top