69 lines
2 KiB
C#
69 lines
2 KiB
C#
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
|
|
}
|
|
}
|