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/SideViewUI.cs

146 lines
4.8 KiB
C#
Raw Normal View History

2024-11-17 19:10:01 +08:00
using UnityEngine;
using UnityEngine.UIElements;
/// <summary>
/// singleton for a single source of truth game state and flow management
/// </summary>
public class SideViewUI : MonoBehaviour
{
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Button _accountButton;
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Label _chromaAccuracyText;
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Label _hueAccuracyText;
/// <summary>
/// leaderboard button for showing the leaderboard
2024-11-17 19:10:01 +08:00
/// </summary>
private Button _leaderboardButton;
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Label _lightnessAccuracyText;
/// <summary>
/// play button for starting the game
2024-11-17 19:10:01 +08:00
/// </summary>
private Button _playButton;
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Label _playerNameText;
/// <summary>
/// settings button for showing the settings menu
2024-11-17 19:10:01 +08:00
/// </summary>
private Label _playerRatingText;
/// <summary>
/// function to subscribe button events to their respective functions
/// </summary>
private void OnEnable()
{
var ui = GetComponent<UIDocument>().rootVisualElement;
_playButton = ui.Q<Button>("PlayButton");
_playButton.clicked += OnPlayButtonClicked;
_leaderboardButton = ui.Q<Button>("LeaderboardButton");
_leaderboardButton.clicked += OnLeaderboardButtonClicked;
_accountButton = ui.Q<Button>("AccountButton");
_accountButton.clicked += OnAccountButtonClicked;
_lightnessAccuracyText = ui.Q<Label>("LightnessAccuracyText");
_hueAccuracyText = ui.Q<Label>("HueAccuracyText");
_chromaAccuracyText = ui.Q<Label>("ChromaAccuracyText");
}
/// <summary>
/// function to update the ui with the latest data
/// </summary>
/// <param name="localPlayerData"></param>
2024-11-17 19:10:01 +08:00
private void RenderFromPlayerData(LocalPlayerData localPlayerData)
{
// calculate averages from both recent local scores and online scores
var totalLightessAcc = 0f;
2024-11-17 19:10:01 +08:00
var totalChromaAcc = 0f;
var totalHueAcc = 0f;
var totalRounds = 0;
// average out all the scores we have to get a stable-ish average
foreach (var localScore in localPlayerData.RecentLocalScores)
{
totalLightessAcc += localScore.AvgLightnessAccuracy;
2024-11-17 19:10:01 +08:00
totalChromaAcc += localScore.AvgChromaAccuracy;
totalHueAcc += localScore.AvgHueAccuracy;
totalRounds += localScore.NoOfRounds;
}
foreach (var onlineScore in localPlayerData.RecentOnlineScores)
{
totalLightessAcc += onlineScore.AvgLightnessAccuracy;
2024-11-17 19:10:01 +08:00
totalChromaAcc += onlineScore.AvgChromaAccuracy;
totalHueAcc += onlineScore.AvgHueAccuracy;
totalRounds += onlineScore.NoOfRounds;
}
2024-11-17 19:10:01 +08:00
foreach (var onlineScore in localPlayerData.BestOnlineScores)
{
totalLightessAcc += onlineScore.AvgLightnessAccuracy;
2024-11-17 19:10:01 +08:00
totalChromaAcc += onlineScore.AvgChromaAccuracy;
totalHueAcc += onlineScore.AvgHueAccuracy;
totalRounds += onlineScore.NoOfRounds;
}
2024-11-17 19:10:01 +08:00
// finally, set the labels
_playerNameText.text = GameManager.Instance.Backend.GetUsername();
_lightnessAccuracyText.text = $"{(totalLightessAcc / totalRounds):F}";
_hueAccuracyText.text = $"{(totalHueAcc / totalRounds):F}";
_chromaAccuracyText.text = $"{(totalChromaAcc / totalRounds):F}";
2024-11-17 19:10:01 +08:00
// and set the player rating, but after we get it from the backend
// (god I LOVE async (I am LYING out of my teeth))
GameManager.Instance.Backend.CalculateUserRating((dtr, rating) =>
{
if (dtr != Backend.DatabaseTransactionResult.Ok) return;
_playerRatingText.text = $"{rating:F}";
});
}
/// <summary>
/// function to show the play view
/// </summary>
private static void OnPlayButtonClicked()
{
GameManager.Instance.SetDisplayState(GameManager.DisplayState.PlayView);
2024-11-17 19:10:01 +08:00
}
/// <summary>
/// function to show the leaderboard view
/// </summary>
private static void OnLeaderboardButtonClicked()
{
GameManager.Instance.SetDisplayState(GameManager.DisplayState.LeaderboardView);
}
/// <summary>
/// function to show the account view
/// </summary>
private static void OnAccountButtonClicked()
{
GameManager.Instance.SetDisplayState(GameManager.DisplayState.AccountView);
}
}