157 lines
No EOL
5 KiB
C#
157 lines
No EOL
5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
/// <summary>
|
|
/// singleton for a single source of truth game state and flow management
|
|
/// </summary>
|
|
public class SideViewUI : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// button to change to transition to the account view
|
|
/// </summary>
|
|
private Button _accountButton;
|
|
|
|
/// <summary>
|
|
/// text that displays the users stable chroma accuracy
|
|
/// </summary>
|
|
private Label _chromaAccuracyText;
|
|
|
|
/// <summary>
|
|
/// text that displays the connection status to the backend
|
|
/// </summary>
|
|
private Label _connectionStatusText;
|
|
|
|
/// <summary>
|
|
/// text that displays the users stable hue accuracy
|
|
/// </summary>
|
|
private Label _hueAccuracyText;
|
|
|
|
/// <summary>
|
|
/// button to transition to the leaderboard view
|
|
/// </summary>
|
|
private Button _leaderboardButton;
|
|
|
|
/// <summary>
|
|
/// text that displays the users stable lightness accuracy
|
|
/// </summary>
|
|
private Label _lightnessAccuracyText;
|
|
|
|
/// <summary>
|
|
/// button to transition to the game view
|
|
/// </summary>
|
|
private Button _playButton;
|
|
|
|
/// <summary>
|
|
/// text that displays the username
|
|
/// </summary>
|
|
private Label _playerNameText;
|
|
|
|
/// <summary>
|
|
/// text that displays the users' rating
|
|
/// </summary>
|
|
private Label _playerRatingText;
|
|
|
|
private void Start()
|
|
{
|
|
GameManager.Instance.RegisterOnLocalPlayerDataUpdate(RenderFromPlayerData);
|
|
}
|
|
|
|
/// <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">the local player data to render the ui from</param>
|
|
private void RenderFromPlayerData(LocalPlayerData localPlayerData)
|
|
{
|
|
// calculate averages from both recent local scores and online scores
|
|
var totalLightness = 0f;
|
|
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)
|
|
{
|
|
totalLightness += localScore.AvgLightnessAccuracy;
|
|
totalChromaAcc += localScore.AvgChromaAccuracy;
|
|
totalHueAcc += localScore.AvgHueAccuracy;
|
|
totalRounds += localScore.NoOfRounds;
|
|
}
|
|
|
|
foreach (var onlineScore in localPlayerData.RecentOnlineScores)
|
|
{
|
|
totalLightness += onlineScore.AvgLightnessAccuracy;
|
|
totalChromaAcc += onlineScore.AvgChromaAccuracy;
|
|
totalHueAcc += onlineScore.AvgHueAccuracy;
|
|
totalRounds += onlineScore.NoOfRounds;
|
|
}
|
|
|
|
foreach (var onlineScore in localPlayerData.BestOnlineScores)
|
|
{
|
|
totalLightness += onlineScore.AvgLightnessAccuracy;
|
|
totalChromaAcc += onlineScore.AvgChromaAccuracy;
|
|
totalHueAcc += onlineScore.AvgHueAccuracy;
|
|
totalRounds += onlineScore.NoOfRounds;
|
|
}
|
|
|
|
// finally, set the labels
|
|
_playerNameText.text = GameManager.Instance.Backend.GetUsername();
|
|
_lightnessAccuracyText.text = $"{totalLightness / totalRounds:F}";
|
|
_hueAccuracyText.text = $"{totalHueAcc / totalRounds:F}";
|
|
_chromaAccuracyText.text = $"{totalChromaAcc / totalRounds:F}";
|
|
|
|
// 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.GameView);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
} |