From 41d5629bf39b31ea9677350dbb08daf66c714575 Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Tue, 19 Nov 2024 14:38:18 +0800 Subject: [PATCH] game: leaderboard ui --- .../Assets/Scripts/LeaderboardUI.cs | 69 +++++++++++++++++++ .../Assets/Scripts/LeaderboardUI.cs.meta | 11 +++ ColourMeOKGame/Assets/Scripts/UIManager.cs | 30 ++++++++ ColourMeOKGame/Assets/UI/GameUI.uxml | 4 +- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs create mode 100644 ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs.meta diff --git a/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs b/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs new file mode 100644 index 0000000..9717a71 --- /dev/null +++ b/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs @@ -0,0 +1,69 @@ +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 + } +} diff --git a/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs.meta b/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs.meta new file mode 100644 index 0000000..0106c18 --- /dev/null +++ b/ColourMeOKGame/Assets/Scripts/LeaderboardUI.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f58221274607ad145b45792b8649c87f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ColourMeOKGame/Assets/Scripts/UIManager.cs b/ColourMeOKGame/Assets/Scripts/UIManager.cs index 6386337..1ae5f82 100644 --- a/ColourMeOKGame/Assets/Scripts/UIManager.cs +++ b/ColourMeOKGame/Assets/Scripts/UIManager.cs @@ -3,6 +3,9 @@ using UnityEngine; using UnityEngine.UIElements; +/// +/// class to handles overall game ui state and transitions +/// public class UIManager : MonoBehaviour { /// @@ -81,6 +84,30 @@ private void OnEnable() UI = GetComponent().rootVisualElement; } + /// + /// function to register a callback to be invoked when the display state changes + /// + /// + /// callback function that takes two DisplayState parameters: + /// + public void RegisterOnDisplayStateChangeCallback(Action callback) + { + _onDisplayStateChangeCallbacks.Add(callback); + } + + private void FireOnDisplayStateChange(DisplayState oldState, DisplayState newState) + { + foreach (var callback in _onDisplayStateChangeCallbacks) + try + { + callback.Invoke(oldState, newState); + } + catch (Exception e) + { + Debug.LogError($"error invoking OnSignOutCallback: {e.Message}"); + } + } + /// /// function to show a menu based on the enum passed, /// and any other necessary actions @@ -160,5 +187,8 @@ public void SetDisplayState(DisplayState newDisplayState) UI.Q("AccountSection").style.display = DisplayStyle.Flex; UI.Q("ConnectionStatus").style.display = DisplayStyle.Flex; } + + FireOnDisplayStateChange(state, newDisplayState); + state = newDisplayState; } } \ No newline at end of file diff --git a/ColourMeOKGame/Assets/UI/GameUI.uxml b/ColourMeOKGame/Assets/UI/GameUI.uxml index 00cc0cd..9ea0c74 100644 --- a/ColourMeOKGame/Assets/UI/GameUI.uxml +++ b/ColourMeOKGame/Assets/UI/GameUI.uxml @@ -108,7 +108,7 @@ - + @@ -120,7 +120,7 @@ - +