This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
colourmeok/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs

70 lines
2 KiB
C#
Raw Normal View History

2024-11-19 14:38:18 +08:00
using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// class that loads leaderboard data and displays it in the UI
/// </summary>
public class LeaderboardUI : MonoBehaviour
{
/// <summary>
/// maximum number of entries to display in the leaderboard
/// </summary>
private const int MaxEntries = 10;
/// <summary>
/// leaderboard data
/// </summary>
private List<Backend.LeaderboardEntry> _leaderboardData = new(MaxEntries);
/// <summary>
/// register callbacks
/// </summary>
private void OnEnable()
{
UIManager.Instance.RegisterOnDisplayStateChangeCallback((_, newState) =>
{
if (newState == UIManager.DisplayState.LeaderboardView)
{
LoadLeaderboardData();
}
});
}
/// <summary>
/// load leaderboard data from the backend
/// </summary>
private void LoadLeaderboardData()
{
if (GameManager.Instance.Backend.Status != Backend.FirebaseConnectionStatus.Connected)
{
Debug.LogError("attempted to load leaderboard data without a connection to the backend");
RenderLeaderboardData("Not connected to the backend, can't load leaderboard data.");
return;
}
GameManager.Instance.Backend.GetLeaderboard((result, entries) =>
{
if (result == Backend.TransactionResult.Ok)
{
_leaderboardData = entries;
RenderLeaderboardData();
}
else
{
Debug.LogError("failed to load leaderboard data");
RenderLeaderboardData("An error occured, couldn't load leaderboard data.");
}
});
}
/// <summary>
/// render leaderboard data
/// </summary>
private void RenderLeaderboardData(string message = "")
{
// render leaderboard data
}
}