diff --git a/ColourMeOKGame/Assets/Scripts/Backend.cs b/ColourMeOKGame/Assets/Scripts/Backend.cs index 0354950..da7166e 100644 --- a/ColourMeOKGame/Assets/Scripts/Backend.cs +++ b/ColourMeOKGame/Assets/Scripts/Backend.cs @@ -65,7 +65,7 @@ public enum UserAccountDetailTargetEnum /// /// callback functions to be invoked when the user signs out /// - private readonly List> _onSignOutCallback = new(); + private readonly List _onSignOutCallback = new(); /// /// the firebase authentication object @@ -81,6 +81,11 @@ public enum UserAccountDetailTargetEnum /// the current user object, if authenticated /// private FirebaseUser _user; + + /// + /// whether the user is signed in + /// + public bool IsSignedIn; /// /// the current user's username, if authenticated @@ -135,6 +140,44 @@ public void Initialise() Debug.Log("firebase status is" + Status); }); } + + /// + /// function to fire all on sign in callbacks + /// + private void FireOnSignInCallbacks() + { + Debug.Log($"firing on sign in callbacks ({_onSignInCallback.Count})"); + foreach (var callback in _onSignInCallback) + { + try + { + callback.Invoke(_user); + } + catch (Exception e) + { + Debug.LogError(e); + } + } + } + + /// + /// function to fire all on sign out callbacks + /// + private void FireOnSignOutCallbacks() + { + Debug.Log($"firing on sign out callbacks ({_onSignOutCallback.Count})"); + foreach (var callback in _onSignOutCallback) + { + try + { + callback.Invoke(); + } + catch (Exception e) + { + Debug.LogError(e); + } + } + } /// /// async function to retry initialisation after a delay @@ -166,44 +209,44 @@ private void AuthStateChanged(object sender, EventArgs eventArgs) if (_auth.CurrentUser == _user) return; // if the user has changed, check if they've signed in or out - var signedIn = _user != _auth.CurrentUser && _auth.CurrentUser != null; + IsSignedIn = _user != _auth.CurrentUser && _auth.CurrentUser != null; // if we're not signed in, but we still hold _user locally, we've signed out - if (!signedIn && _user != null) + if (!IsSignedIn && _user != null) { - Debug.Log($"signed out successfully as {_user.UserId}"); - foreach (var callback in _onSignOutCallback) - callback(_user); + Debug.Log("moi-moi"); + FireOnSignOutCallbacks(); } // they have signed in, update _user _user = _auth.CurrentUser; - if (!signedIn) return; + if (!IsSignedIn) return; - Debug.Log($"signed in successfully as {_user.UserId}"); + Debug.Log($"signed in successfully as {_user?.UserId}"); RetrieveUsernameWithCallback((_, _) => { - foreach (var callback in _onSignInCallback) callback(_user); + FireOnSignInCallbacks(); }); } /// /// function to register a callback for when the user signs in /// - /// callback function that takes in a FirebaseUser argument + /// callback function that takes in a FirebaseUser object public void RegisterOnSignInCallback(Action callback) { + Debug.Log("registering on sign in callback"); _onSignInCallback.Add(callback); } - /// - /// function to register a callback for when the user signs out - /// - /// callback function that takes in a FirebaseUser argument - public void RegisterOnSignOutCallback(Action callback) - { - _onSignOutCallback.Add(callback); - } + // /// + // /// function to register a callback for when the user signs out + // /// + // /// callback function + // public void RegisterOnSignOutCallback(Action callback) + // { + // _onSignOutCallback.Add(callback); + // } /// /// abstraction function to authenticate the user @@ -345,7 +388,7 @@ public void RegisterOnSignOutCallback(Action callback) /// private void RetrieveUsername() { - RetrieveUsernameWithCallback((result, username) => { }); + RetrieveUsernameWithCallback((_, _) => { }); } /// diff --git a/ColourMeOKGame/Assets/Scripts/GameManager.cs b/ColourMeOKGame/Assets/Scripts/GameManager.cs index 56be8e9..ec2b3a3 100644 --- a/ColourMeOKGame/Assets/Scripts/GameManager.cs +++ b/ColourMeOKGame/Assets/Scripts/GameManager.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; using UnityEngine.UIElements; @@ -52,9 +53,24 @@ private void Start() _localPlayerData = new LocalPlayerData(); _localPlayerData.LoadFromTheWorld(); PopulateFields(); + try + { + RenderFromPlayerData(); + } + catch (Exception) + { + // TODO: remove this once the bug is fixed + Debug.LogWarning("handling known exception, remove this once the bug is fixed"); + } // register a callback to refresh the ui when the player signs in - Backend.RegisterOnSignInCallback(_ => { _localPlayerData.LoadFromTheWorld(); }); + Backend.RegisterOnSignInCallback(_ => + { + Debug.Log("sign in callback, refreshing GameManager-controlled SideView UI"); + _localPlayerData.LoadFromTheWorld(); + PopulateFields(); + RenderFromPlayerData(); + }); } /// @@ -82,7 +98,68 @@ private void OnApplicationQuit() /// public void PopulateFields() { + Debug.Log( + $"populating AccountView fields with lkUsername={_localPlayerData.LastKnownUsername} and lkEmail={_localPlayerData.LastKnownEmail}"); ui.UI.Q("UsernameField").value = _localPlayerData.LastKnownUsername; ui.UI.Q("EmailField").value = _localPlayerData.LastKnownEmail; } + + /// + /// function to update the ui with the latest data + /// + private void RenderFromPlayerData() + { + // calculate averages from both recent local scores and online scores + var totalLightnessAcc = 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) + { + totalLightnessAcc += localScore.AvgLightnessAccuracy; + totalChromaAcc += localScore.AvgChromaAccuracy; + totalHueAcc += localScore.AvgHueAccuracy; + totalRounds += localScore.NoOfRounds; + } + + foreach (var onlineScore in _localPlayerData.RecentOnlineScores) + { + totalLightnessAcc += onlineScore.AvgLightnessAccuracy; + totalChromaAcc += onlineScore.AvgChromaAccuracy; + totalHueAcc += onlineScore.AvgHueAccuracy; + totalRounds += onlineScore.NoOfRounds; + } + + foreach (var onlineScore in _localPlayerData.BestOnlineScores) + { + totalLightnessAcc += onlineScore.AvgLightnessAccuracy; + totalChromaAcc += onlineScore.AvgChromaAccuracy; + totalHueAcc += onlineScore.AvgHueAccuracy; + totalRounds += onlineScore.NoOfRounds; + } + + Debug.Log($"tL={totalLightnessAcc} tC={totalChromaAcc} tH={totalHueAcc} tR={totalRounds}"); + if (totalRounds == 0) totalRounds = 1; + var lightnessAcc = totalLightnessAcc / totalRounds; + var chromaAcc = totalChromaAcc / totalRounds; + var hueAcc = totalHueAcc / totalRounds; + var playerText = Backend.IsSignedIn ? _localPlayerData.LastKnownUsername : $"{_localPlayerData.LastKnownUsername} (Not Signed In)"; + + // finally, set the labels + ui.UI.Q private Button _accountButton; - /// - /// settings button for showing the settings menu - /// - private Label _chromaAccuracyText; - - /// - /// settings button for showing the settings menu - /// - private Label _hueAccuracyText; - /// /// leaderboard button for showing the leaderboard /// private Button _leaderboardButton; - /// - /// settings button for showing the settings menu - /// - private Label _lightnessAccuracyText; - /// /// play button for starting the game /// private Button _playButton; - /// - /// settings button for showing the settings menu - /// - private Label _playerNameText; - - /// - /// settings button for showing the settings menu - /// - private Label _playerRatingText; - /// /// function to subscribe button events to their respective functions /// @@ -61,63 +36,6 @@ private void OnEnable() _accountButton = ui.Q