using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
///
/// class that loads leaderboard data and displays it in the UI
///
public class LeaderboardUI : MonoBehaviour
{
///
/// maximum number of entries to display in the leaderboard
///
private const int MaxEntries = 10;
///
/// leaderboard data
///
private List _leaderboardData = new(MaxEntries);
///
/// register callbacks
///
private void OnEnable()
{
UIManager.Instance.RegisterOnDisplayStateChangeCallback((_, newState) =>
{
if (newState == UIManager.DisplayState.LeaderboardView)
{
LoadLeaderboardData();
}
});
}
///
/// load leaderboard data from the backend
///
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.");
}
});
}
///
/// render leaderboard data
///
private void RenderLeaderboardData(string message = "")
{
// render leaderboard data
}
}