Put it all together - Weekly leaderboard - (Unity module)
Connect the UI to Display Periodic Leaderboard Rankings
Open the
PeriodicLeaderboardMenu_Starter
class and define a variable to store the wrapper you created.private PeriodicLeaderboardWrapper_Starter periodicLeaderboard;
Next, replace the
OnEnable()
function with the code below. It initializes the wrapper using thePeriodicLeaderboardWrapper_Starter
class and will only display the ranking list if the wrapper is valid.private void OnEnable()
{
periodicLeaderboard ??= TutorialModuleManager.Instance.GetModuleClass<PeriodicLeaderboardWrapper_Starter>();
if (periodicLeaderboard)
{
DisplayRankingList();
}
}Create these helper variables to reference the leaderboard code based on the selected game mode from the
LeaderboardsMenu
class, and the selected period from theLeaderboardPeriodMenu
class.private string LeaderboardCode =>
LeaderboardEssentialsModels.GetLeaderboardCodeByGameMode(LeaderboardsMenu.SelectedGameMode);
private string PeriodName =>
LeaderboardEssentialsModels.GetFormattedLeaderboardPeriod(LeaderboardPeriodMenu.SelectedPeriod);Next, update the
DisplayRankingList()
function with the code below. This function sends a request to get the statistics cycle config based on the selected period. Then, it uses the cycle ID from that config to send a request to get periodic leaderboard rankings. After receiving the list, it queries all player information (e.g., display name) from the leaderboard list. Once done, it instantiates the leaderboard entries to be displayed.private void DisplayRankingList()
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
leaderboardListPanel.DestroyAllChildren();
// Get statistics cycle config by selected period.
periodicLeaderboard.GetStatCycleConfigByName(PeriodName, (result) =>
{
// Abort if the period is not found.
if (result.IsError)
{
BytewarsLogger.LogWarning($"Failed to display leaderboard rankings. Error: {result.Error.Message}");
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
// Get leaderboard ranking list by statistics cycle.
string cycleId = result.Value.Id;
periodicLeaderboard.GetRankingsByCycle(
LeaderboardCode,
cycleId,
(Result<LeaderboardRankingResult> rankingResult) =>
{
// The backend treats an empty leaderboard as an error. Therefore, display the empty state instead.
if (rankingResult.IsError || rankingResult.Value.data.Length <= 0)
{
BytewarsLogger.LogWarning($"Failed to display leaderboard rankings. Error: {rankingResult.Error.Message}");
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Empty);
return;
}
// Query the users info in the leaderboard rankings.
UserPoint[] rankings = rankingResult.Value.data;
AccelByteSDK.GetClientRegistry().GetApi().GetUser().GetUserOtherPlatformBasicPublicInfo(
"ACCELBYTE",
rankingResult.Value.data.Select(x => x.userId).ToArray(),
(Result<AccountUserPlatformInfosResponse> usersInfoResult) =>
{
// Abort if failed to query users info.
if (usersInfoResult.IsError)
{
BytewarsLogger.LogWarning($"Failed to display leaderboard rankings. Error: {rankingResult.Error.Message}");
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
// Generate leaderboard ranking entries.
int rankOrder = 0;
bool isCurrentPlayerInRanked = false;
Dictionary<string, AccountUserPlatformData> usersInfo =
usersInfoResult.Value.Data.ToDictionary(x => x.UserId, x => x);
foreach (UserPoint ranking in rankings)
{
LeaderboardEntry entry = Instantiate(leaderboardEntryPrefab, leaderboardListPanel).GetComponent<LeaderboardEntry>();
usersInfo.TryGetValue(ranking.userId, out AccountUserPlatformData userInfo);
entry.SetRankingDetails(ranking.userId, ++rankOrder, userInfo?.DisplayName, ranking.point);
// If the user is the current logged-in player, display the rank on the player ranking entry card.
if (ranking.userId == GameData.CachedPlayerState.PlayerId)
{
isCurrentPlayerInRanked = true;
DisplayPlayerRanking(new UserCycleRanking()
{
Rank = rankOrder,
Point = ranking.point,
CycleId = cycleId
});
}
}
// Get the logged-in player's rank if it is not included in the leaderboard.
if (!isCurrentPlayerInRanked)
{
periodicLeaderboard.GetUserRanking(
GameData.CachedPlayerState.PlayerId,
LeaderboardCode,
(Result<UserRankingDataV3> userRankResult) =>
{
// Display the leaderboard result.
DisplayPlayerRanking(userRankResult.IsError ? null : userRankResult.Value.Cycles.FirstOrDefault(x => x.CycleId == cycleId));
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
});
return;
}
// Display the leaderboard result.
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
});
},
0, LeaderboardEssentialsModels.QueryRankingsLimit);
});
}
Resources
The files used in this tutorial are available in the Unity Byte Wars GitHub repository.