> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Accessing Archived Tournament results

> Access archived PlayFab tournament leaderboard snapshots after a manual or scheduled reset, and retrieve the previous version's player rankings and stats.

This tutorial illustrates how you can access archived leaderboard states.

Each leaderboard can be reset manually or automatically - meaning statistic values 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 values for each player. This allows you to access this *archived* version of the leaderboard.

<Note>
  All titles allow you to access the most recently archived version of a leaderboard, and this gives you the current and most previous versions. For example, if your current leaderboard version is **3**, you may access only version **3** and archived version **2**.
</Note>

## Initial setup

Before using this guide, please make sure that you have some players already registered for the title. The following screenshot shows 5 players artificially registered using the [LoginWithCustomID](xref:titleid.playfabapi.com.client.authentication.loginwithcustomid) API call.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-players-most-recent-logins.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=82e4840d634e73c5e5e9f0878f0365ab" alt="Game Manager - Players - Most recent logins" width="946" height="577" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-players-most-recent-logins.png" />

## Defining and simulating a 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:

1. Navigate to the **Leaderboards** section.
2. Select the **New Leaderboard** button, as shown below.

To configure the new leaderboard:

1. Set the **Statistic** name to **TestScore**.
2. Keep the **Reset Frequency** as **Manually**.
3. Leave the default **Aggregation** method.
4. Submit by selecting **Save Leaderboard**.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-new-leaderboard-properties.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=2cd9b12e09ccfe716626232cb6967815" alt="Game Manager - Leaderboards - New Leaderboard - Properties" width="1280" height="900" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-new-leaderboard-properties.png" />

You will end up on the new **Leaderboards** page which will render blank data (see below).

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-edit-leaderboard.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=f3b2e105a30073db23eb36d8cf232e92" alt="Game Manager - Leaderboards - Edit Leaderboard" width="1920" height="910" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-edit-leaderboard.png" />

## Begin test section

<Info>
  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.
</Info>

Our next step is simulating some data for our leaderboard. The quickest way to do this is to create a CloudScript handler, which will set random statistics for a given player. We will invoke this handler for every player over the 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 CloudScript (refer to the code comments for further information).

```javascript theme={null}
// 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 CloudScript. Using Game Manager:

1. Navigate to the **Automation** tab.
2. Then navigate to the **CloudScript** sub-tab.
3. Insert the **CloudScript** code.
4. Select **Save as Revision**.
5. Finally, **Deploy Revision**.

<img src="https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-automation-cloudscript-upload-cloudscript.png?fit=max&auto=format&n=5IpvKlT-jmAaVkeY&q=85&s=7aad030809ec7893abbc42ae6f928ca0" alt="Game Manager - Automation - CloudScript - upload CloudScript" width="1292" height="707" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-automation-cloudscript-upload-cloudscript.png" />

Next, we need to define a task to execute our CloudScript over a specific segment:

1. Navigate to the **Players** tab.
2. Then, navigate to the **Segments** sub-tab.

By default, **PlayFab** generates an **All Players** segment for you. This segment is specifically useful when you need all players registered in your title (which is exactly our case).

3. Select the **All Players Segment**.
4. Finally, select **Run Task...**.

<Note>
  If you have no All Players segment in the list, please, refer to our [Player Segmentation](/services/playfab/live-service-management/game-configuration/segmentation/segmentation-quickstart) quickstart to create one.
</Note>

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-players-segments-all-players-run-task.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=38b80b93c15928a0f22495e8a555b82d" alt="Game Manager - Players - Segments - All Players - Run Task" width="983" height="580" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-players-segments-all-players-run-task.png" />

To configure the task:

1. Set up the **Name**.
2. Make sure the type of task is set to **Run actions on each Player in a Segment**.
   * In addition, verify that **All Players**  is selected under **Segment**.
3. Add a new **Action**.
4. Select **Execute CloudScript** under **Type**.
5. Then select the **PopulateLeaderboard** handler under **CloudScript Function**.
6. Finally, select the **Save and Run** button.

<img src="https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-configure-task.png?fit=max&auto=format&n=5IpvKlT-jmAaVkeY&q=85&s=b0de60452d7b3bf3d2e4198ed1b11764" alt="Game Manager - Configure Task" width="1175" height="850" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-configure-task.png" />

* Make sure the execution result is **Successful**.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-task-execution-succeeded.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=80cba8a4162a8d74ec513be09bd71849" alt="Game Manager - Task Execution Succeeded" width="907" height="212" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-task-execution-succeeded.png" />

## End of Test Section

### Populate Test data

1. Navigate to the **Leaderboards** tab again.
2. Select our **Leaderboard**.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-test-score.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=5b9e840d77457f6a5d497a7b83d81d3a" alt="Game Manager - Leaderboards - Test Score" width="1272" height="668" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-test-score.png" />

1. You will see that your **Leaderboard** was populated with random values.
2. Reset the **Leaderboard**.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/reset-the-test-score-leaderboard.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=f91b98680e788338e37520102bb78f4f" alt="Reset the Test Score Leaderboard" width="1038" height="464" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/reset-the-test-score-leaderboard.png" />

This will create a snapshot of all the data we currently have, and then it will *nullify* statistic values on every player and increment the version.

* Once your leaderboard is reset, run the CloudScript task again.
* Repeat this 2-3 times, then 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)**.
* Only the latest version will be available. This applies to all titles.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-leaderboard-versions.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=2ca281d95f15295de606458c9f450734" alt="Game Manager - Leaderboards - Leaderboard Versions" width="1204" height="694" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-leaderboard-versions.png" />

### Accessing archived data using Game Manager

You can access archived results directly from the **Leaderboard** page:

1. Navigate to the **Leaderboards** tab.
2. Select the **Leaderboard** you need.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-select-leaderboard.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=db629e9b5dec8a479629276004bbf22d" alt="Game Manager - Leaderboards - Select Leaderboard" width="921" height="271" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-select-leaderboard.png" />

1. If your **Leaderboard** contains archived revisions, you will be able to download **JSON** data using the download link shown in the screenshot below.

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-download-archived-revision.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=4395cb8990431b53f7457010fd21fcab" alt="Game Manager - Leaderboards - Download Archived Revision" width="1204" height="694" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/game-manager-leaderboards-download-archived-revision.png" />

### Accessing archived data using API

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

```csharp theme={null}
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 the example provided below.

<img src="https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/debug-output-display-leaderboard-versions.png?fit=max&auto=format&n=5IpvKlT-jmAaVkeY&q=85&s=e345a4db86df875b598d2feec2458ca3" alt="Debug output - Display Leaderboard Versions" width="267" height="225" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/debug-output-display-leaderboard-versions.png" />

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

```csharp theme={null}
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 the example provided below.

<img src="https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/debug-output-display-leaderboard-version-1.png?fit=max&auto=format&n=5IpvKlT-jmAaVkeY&q=85&s=068921c7f391fca2889ee300c6504a4d" alt="Debug output - Display Leaderboard Version 1" width="272" height="221" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/debug-output-display-leaderboard-version-1.png" />

All titles allow you to access the *latest* archived version of the leaderboard. Trying to pull an older version will result in an error (see below).

<img src="https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/error-leaderboard-version-not-available.png?fit=max&auto=format&n=5IpvKlT-jmAaVkeY&q=85&s=75ed581ae4e92e67969cf002bd6b0dd7" alt="Error - Leaderboard Version not available" width="371" height="48" data-path="images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/error-leaderboard-version-not-available.png" />

The same rules apply for all the Leaderboard requests.

* [GetLeaderboard](xref:titleid.playfabapi.com.client.playerdatamanagement.getleaderboard)
* [GetLeaderboardAroundPlayer](xref:titleid.playfabapi.com.client.playerdatamanagement.getleaderboardaroundplayer)
* [GetFriendLeaderboardAroundPlayer](xref:titleid.playfabapi.com.client.playerdatamanagement.getfriendleaderboardaroundplayer)
* [GetFriendLeaderboard](xref:titleid.playfabapi.com.client.playerdatamanagement.getfriendleaderboard)

Please refer to documentation for the [GetLeaderboardResult](xref:titleid.playfabapi.com.client.playerdatamanagement.getleaderboard#getleaderboardresult) object, to learn about useful properties you may get with the leaderboard data.


## Related topics

- [Using the Profile for Advanced Leaderboards](/services/playfab/community/leaderboards/tournaments-leaderboards/using-the-profile-for-advanced-leaderboards.md)
- [Push Notifications quickstart](/services/playfab/live-service-management/game-configuration/title-communications/push-notifications/quickstart.md)
- [Using resettable statistics and leaderboards](/services/playfab/community/leaderboards/tournaments-leaderboards/using-resettable-statistics-and-leaderboards.md)
- [Using player statistics](/services/playfab/community/leaderboards/tournaments-leaderboards/using-player-statistics.md)
- [Tournaments & Leaderboards](/services/playfab/community/leaderboards/tournaments-leaderboards/index.md)
