Skip to main content

Put it all together - Weekly leaderboard - (Unity module)

Last updated on March 22, 2024

What's in the Starter Pack

Since you are using the Leaderboard Menus, it means the user interface (UI) references are handled by the Leaderboard Handler scripts. To focus on the Periodic Leaderboard only, some event delegates and a helper script for implementing Periodic Leaderboards have been prepared for you.

To implement Periodic Leaderboards without changing the Leaderboard Menu scripts, a script has been prepared for you to modify called PeriodicLeaderboardHelper_Starter that subscribes to events related to changes in the Leaderboard menus.

  • PeriodicLeaderboardHelper_Starter file: /Assets/Resources/Modules/PeriodicLeaderboard/Scripts/PeriodicLeaderboardHelper_Starter.cs

Store the Cycle ID

  1. Open LeaderboardSelectionMenu_Starter.cs.

  2. The Leaderboard class GetLeaderboardListV3() result contains Cycle IDs that are configured in the AccelByte Gaming Services (AGS) Admin Portal. You will need this Cycle ID to query the ranking list later, so prepare a public static dictionary to store the Cycle ID results.

    public static Dictionary<string, string[]> leaderboardCycleIds;
  3. Navigate to the OnGetLeaderboardListCompleted() callback function and add the following highlighted code. This is to store the Cycle ID if the GetLeaderboardListV3() query is a success.

    private void OnGetLeaderboardListCompleted(Result<LeaderboardPagedListV3> result)
    {
    if (result.IsError) return;

    leaderboardCycleIds = new Dictionary<string, string[]>();
    foreach (LeaderboardDataV3 leaderboardData in result.Value.Data)
    {
    if (!leaderboardData.Name.Contains("Unity")) continue;
    ...

    leaderboardCycleIds.Add(leaderboardData.LeaderboardCode, leaderboardData.CycleIds);
    }
    }

Generate period button and ranking list

  1. Open PeriodicLeaderboardHelper_Starter.cs.

  2. Add some local variables for storing the essential data that will be used for calling the Periodic Leaderboard functions.

    private TokenData currentUserData; 
    private string chosenCycleId;
  3. Add some local variable objects for your Periodic Leaderboard and Auth Essentials Wrappers. Then, implement the Start() function and initialize the object by getting the Class Component using the instance of the utility class called TutorialModuleManager. Also, get the current user data stored from your Auth Essentials Wrapper and store it to the variable you prepared earlier.

    private PeriodicLeaderboardEssentialsWrapper_Starter periodicLeaderboardWrapper;
    private AuthEssentialsWrapper authWrapper;

    void Start()
    {
    periodicLeaderboardWrapper = TutorialModuleManager.Instance.GetModuleClass<PeriodicLeaderboardEssentialsWrapper_Starter>();
    authWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper>();
    currentUserData = authWrapper.userData;
    }
  4. Create a new function to generate the period button based on the available cycles for the current user.

    private void DisplayLeaderboardCycleButtons(Transform leaderboardListPanel, GameObject leaderboardItemButtonPrefab){
    string[] cycleIds = LeaderboardSelectionMenu_Starter.leaderboardCycleIds[LeaderboardSelectionMenu_Starter.chosenLeaderboardCode];

    foreach (string cycleId in cycleIds)
    {
    periodicLeaderboardWrapper.GetStatCycleConfig(cycleId, result =>
    {
    if (!result.IsError)
    {
    Button leaderboardButton = Instantiate(leaderboardItemButtonPrefab, leaderboardListPanel).GetComponent<Button>();
    TMP_Text leaderboardButtonText = leaderboardButton.GetComponentInChildren<TMP_Text>();
    leaderboardButtonText.text = result.Value.Name;
    leaderboardButton.onClick.AddListener(() => LeaderboardCycleMenu_Starter.ChangeToLeaderboardMenu(LeaderboardCycleMenu_Starter.LeaderboardCycleType.Weekly));

    chosenCycleId = cycleId;
    }
    });
    }
    }
  5. Prepare the displaying of the ranking list based on the chosen cycle. Add these local variables to limit the ranking list to show the top 10 players only.

    private const int RESULTOFFSET = 0;
    private const int RESULTLIMIT = 10;
  6. Prepare a new function to call the Periodic Leaderboard GetRankingsByCycle() function. Here, you bind the Leaderboard Menu's OnDisplayRankingListCompleted() function to handle the instantiating of ranking entry prefabs and update the ranking data processes since the GetRankingsByCycle() result is the same as the Leaderboard's GetRankings().

    private void DisplayCycleRankingList(LeaderboardMenu_Starter leaderboardMenu, UserCycleRanking[] userCycleRankings)
    {
    if (LeaderboardCycleMenu_Starter.chosenCycleType is LeaderboardCycleMenu_Starter.LeaderboardCycleType.Weekly)
    {
    periodicLeaderboardWrapper.GetRankingsByCycle(
    LeaderboardSelectionMenu_Starter.chosenLeaderboardCode,
    chosenCycleId,
    leaderboardMenu.OnGetRankingsCompleted,
    RESULTOFFSET,
    RESULTLIMIT
    );
    }
    }
  7. Create another function to update the User Ranking Panel data for the current cycle leaderboard. You will call the Leaderboard Menu's InstantiateRankingItem() instead since you are only updating the UI text data.

    private void DisplayUserCycleRanking(LeaderboardMenu_Starter leaderboardMenu, UserCycleRanking[] userCycleRankings)
    {
    if (LeaderboardCycleMenu_Starter.chosenCycleType is LeaderboardCycleMenu_Starter.LeaderboardCycleType.Weekly)
    {
    foreach (UserCycleRanking cycleRanking in userCycleRankings)
    {
    if (cycleRanking.CycleId == chosenCycleId)
    {
    leaderboardMenu.InstantiateRankingEntry(currentUserData.user_id, cycleRanking.Rank, currentUserData.display_name, cycleRanking.Point);
    break;
    }
    }
    }
    }
  8. Add the code below to subscribe to be notified when Leaderboard Cycle Menu and Leaderboard Menu changes and bind those events to the functions you prepared earlier to trigger the generation of the period button, and also display the ranking list based on the Periodic Leaderboard's cycle.

    void Start()
    {
    ...

    LeaderboardCycleMenu_Starter.onLeaderboardCycleMenuActivated += DisplayLeaderboardCycleButtons;
    LeaderboardMenu_Starter.onDisplayRankingListEvent += DisplayCycleRankingList;
    LeaderboardMenu_Starter.onDisplayUserRankingEvent += DisplayUserCycleRanking;
    }

Resources