Skip to main content

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

Last updated on October 24, 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. Declare the dictionary below to store cycle IDs, which you will use later to query the periodic leaderboard ranking list.

    public static Dictionary<string, string[]> leaderboardCycleIds = new Dictionary<string, string[]>();
  3. Now, modify the existing OnGetLeaderboardListCompleted() callback function and add the highlighted code below. The code is used to store the cycle IDs when querying the leaderboard list is completes.

    private void OnGetLeaderboardListCompleted(Result<LeaderboardPagedListV3> result)
    {
    ...
    leaderboardCycleIds.Clear();
    foreach (LeaderboardDataV3 leaderboard in leaderboardList)
    {
    ...
    leaderboardCycleIds.Add(leaderboard.LeaderboardCode, leaderboard.CycleIds);
    }
    ...
    }

Generate period button and ranking list

  1. Open PeriodicLeaderboardHelper_Starter.cs.

  2. Declare a local variable to hold the reference of the PeriodicLeaderboardEssentialsWrapper_Starter wrapper.

    private PeriodicLeaderboardEssentialsWrapper_Starter periodicLeaderboardWrapper;
  3. Create a new function to generate the Period button based on the available cycles stored in the leaderboardCycleIds dictionary.

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

    // No valid leaderboard cycles.
    if (cycleIds.Length <= 0)
    {
    BytewarsLogger.LogWarning("Cannot display leaderboard cycle buttons. No leaderboard cycle ID was found.");
    return;
    }

    // Query leaderboard cycles.
    int queriedCycles = 0;
    leaderboardCycleMenu.CurrentView = LeaderboardCycleMenu_Starter.LeaderboardCycleView.Loading;
    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.Replace("unity-", "");
    leaderboardButton.onClick.AddListener(() => LeaderboardCycleMenu_Starter.ChangeToLeaderboardMenu(LeaderboardCycleMenu_Starter.LeaderboardCycleType.Weekly, cycleId));

    queriedCycles++;

    // Show the leaderboard cycle list if all cycles are already queried.
    if (queriedCycles == cycleIds.Length)
    {
    BytewarsLogger.Log("Success to display leaderboard cycle buttons.");
    leaderboardCycleMenu.CurrentView = LeaderboardCycleMenu_Starter.LeaderboardCycleView.Default;
    }
    }
    else
    {
    BytewarsLogger.LogWarning($"Failed to display leaderboard cycle buttons. Error: {result.Error.Message}");
    leaderboardCycleMenu.CurrentView = LeaderboardCycleMenu_Starter.LeaderboardCycleView.Failed;
    }
    });
    }
    }
  4. Declare the local variables shown below to limit the ranking list to query. For this example, you want to query the top 10 players only.

    private const int RESULTOFFSET = 0;
    private const int RESULTLIMIT = 10;
  5. Next, create a new function to query and display the periodic leaderboard ranking list.

    private void DisplayCycleRankingList(LeaderboardMenu_Starter leaderboardMenu, UserCycleRanking[] userCycleRankings)
    {
    if (LeaderboardCycleMenu_Starter.chosenCycleType == LeaderboardCycleMenu_Starter.LeaderboardCycleType.AllTime)
    {
    BytewarsLogger.LogWarning("Cannot display leaderboard cycle ranking list. Chosen cycle type is not a periodic leaderboard.");
    return;
    }

    periodicLeaderboardWrapper.GetRankingsByCycle(
    LeaderboardSelectionMenu_Starter.chosenLeaderboardCode,
    LeaderboardCycleMenu_Starter.chosenCycleId,
    leaderboardMenu.OnGetRankingsCompleted,
    RESULTOFFSET,
    RESULTLIMIT);
    }
  6. Finally, create the Start() function. Then, initialize the periodic leaderboard wrapper and subscribe to the leaderboard menu events.

    void Start()
    {
    periodicLeaderboardWrapper = TutorialModuleManager.Instance.GetModuleClass<PeriodicLeaderboardEssentialsWrapper_Starter>();

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

Resources