Documentation

Did you find this helpful?

Accessing Archived Tournament Results

This guide illustrates how you can access archived leaderboards states. 

Each leaderboard can be reset manually or automatically, meaning statistic value will be removed for all the players, leading to a "clear" state and the leaderboard version will be implemented. Before that happens, however, PlayFab creates a snapshot of all the leaderboard statistic value for each player. This allows your to access this "archived" version of the leaderboard.

Note: PlayFab free tier only allows access to the most recently archived version of the leaderboard, effectively giving you current and the most previous versions. (ex. with a free tier, if your current leaderboard version is 3, you may only access version 3 and archived version 2) 

Initial setup

To follow this guide, please ensure that you have some players already registered for the title. The screenshot below shows 5 players artificially registered using LoginWithCustomID API Call:

Defining and simulating leaderboard

In this step, we will create a leaderboard for our test purposes. Then, we will simulate the leaderboard iteration process by populating and resetting the leaderboard several times.

Open Game Manager and navigate to "Leaderboards" tab (1). Then click "New Leaderboard" button (2):

Configure new leaderboard: set Statistic name to "TestScore" (1), keep Reset Frequency as Manual (2) and leave the default Aggregation method (3). Submit by clicking "Save Leaderboard" (4):

You will end up on the new Leaderboards page which will, obviously, render blank data:

BEGIN TEST SECTION: The following section is an example of how to populate test data for the purpose of this example. Your real game will populate this data in a more natural way.

Our next step is simulating some data for our leaderboard. The quickest way to do it is to create Cloud Script handler, which will set random statistic for a given player. We will invoke this handler for every player over "All Players" segment. As a result, each player will get a random statistic value, which is a "good enough" approximation of a real world scenario.

Let's start with defining our Cloud Script (refer to comments for further information):

// Should be invoked from a task that runs over certain segment
handlers.PopulateLeaderboard = (args,ctx) => {
    // When handler is executed as a task over the segment
    // we can extract individual player id using the next line:
    let playerId = ctx.playerProfile.PlayerId;

    // Use player id and update player statistics as follows:
    server.UpdatePlayerStatistics({
        PlayFabId : playerId,
        Statistics : [
            {
                "StatisticName": "TestScore",
                "Value": getRandomInRange(100,1000)
            }
        ]
    });
}

// Utility method to generate random number
let getRandomInRange = (min, max) => {
    return Math.round(Math.random() * (max - min) + min);
}

Let's upload the Cloud Script. Using Game Manager, navigate to "Automation" (1) tab. Then navigate to "Cloud Script" (2) sub-tab. Insert the Cloud Script code (3) and click "Save as revision" (4). Finally, deploy the revision (5)

Next, we need to define task to execute our Cloud Script over a specific segment. Navigate to "Players" tab (1). Then, navigate to "Segments" sub-tab (2). By default, PlayFab generates "All Players" segment for you. This segment is specifically useful when you need all players registered in your title (which is exactly our case).  Select "All Players" segment (3) and click "Run task..." (4).

Note: If you have no "All Players" segment in the list, please, refer to our Player Segmentation guide to create one.

Configure the task: set up the name (1), make sure type is set to "Run actions on each player in a segment" (2), also make sure "All Players" segment is selected. Add new action (3) and select "Execute Cloud Script" type (4). Select "PopulateLeaderboard" handler. Finally, click "Save and Run".

Make sure the execution result is "Successful".

END OF TEST SECTION: Populate test data

Navigate to "Leaderboards" tab (1) again, then select our "Leaderboard" (2).

You will see that our leaderboard got populated with random values (1). Reset the leaderboard (2)

This will create a snapshot of all the data we currently have and then it will "nullify" statistic value on every player and increment the version. Once your leaderboard is reset, run the Cloud Script task again. Repeat this 2-3 times: reset and repopulate.

You will end up with several leaderboard versions (1). Current version data will be displayed in the table to the left (3). Archived data will be available for previous versions (2). With a free tier, only the very previous version will be available:

Accessing archived data using Game Manager

You can access archived results directly from Leaderboard page. Navigate to "Leaderboards" tab (1) again, then select the leaderboard you need (2):

If your leaderboard contains archived revisions, you will be able to download JSON data using the download link (1):

Accessing archived data using API

The following code allows you to pull the latest (current) version of the leaderboard:

PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest()
{
    StatisticName = "TestScore",
}, result =>
{
    Debug.Log("Leaderboard version: "+result.Version);
    foreach (var entry in result.Leaderboard)
    {
        Debug.Log(entry.PlayFabId+" "+entry.StatValue);
    }
}, FailureCallback);

The result will look like this:

Alternatively, you may specify a version of the leaderboard you want to load:

PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest()
{
    StatisticName = "TestScore",
    Version = 1
}, result =>
{
    Debug.Log("Leaderboard version: "+result.Version);

    foreach (var entry in result.Leaderboard)
    {
        Debug.Log(entry.PlayFabId+" "+entry.StatValue);
    }
}, FailureCallback);

The result will look like this:

Free tier only allows to access the very latest archived version of the leaderboard. Trying to pull older version will result in an error:

Same rules apply for all the leaderboard requests:

Please refer to GetLeaderboardResult documentation to learn about useful properties you may get with the leaderboard data.



Did you find this helpful?