From fe44ced9f8cc1eadba68ff4da7b36d7b65d9e8ba Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Mon, 18 Nov 2024 00:50:04 +0800 Subject: [PATCH] game: stdise whats done in Awake, OnEnable and Start - Awake -> singleton stuff or anything that needs to start really early - OnEnable -> init + register callbacks - Start -> do anything that might call a callback --- ColourMeOKGame/Assets/Scripts/AccountUI.cs | 9 ++-- ColourMeOKGame/Assets/Scripts/Backend.cs | 45 ++++++++-------- ColourMeOKGame/Assets/Scripts/GameManager.cs | 48 +++++++++-------- .../Assets/Scripts/OklchColourPickerUI.cs | 4 +- ColourMeOKGame/Assets/Scripts/SideViewUI.cs | 51 +++++++++---------- ColourMeOKGame/Assets/Scripts/UIManager.cs | 12 ++++- 6 files changed, 88 insertions(+), 81 deletions(-) diff --git a/ColourMeOKGame/Assets/Scripts/AccountUI.cs b/ColourMeOKGame/Assets/Scripts/AccountUI.cs index 6f7fac0..1b737a0 100644 --- a/ColourMeOKGame/Assets/Scripts/AccountUI.cs +++ b/ColourMeOKGame/Assets/Scripts/AccountUI.cs @@ -1,6 +1,5 @@ using System; using System.Net.Mail; -using Firebase.Auth; using UnityEngine; using UnityEngine.Assertions; using UnityEngine.UIElements; @@ -110,7 +109,7 @@ private void OnEnable() _secondaryActionButton.clicked += OnSecondaryActionButtonClick; TransitionStateTo(State.NotSignedIn); - + if (state == State.UnassociatedState) throw new Exception("unreachable state"); GameManager.Instance.Backend.RegisterOnSignInCallback(_ => { @@ -376,6 +375,8 @@ private void OnUsernameUpdateButtonClick() _ => "An error occurred updating the username. Please try again." }; }); + + // TODO: update lpdata } /// @@ -408,6 +409,8 @@ private void OnEmailUpdateButtonClick() _ => "An error occurred updating the email. Please try again." }; }); + + // TODO: update lpdata } /// @@ -608,7 +611,7 @@ private void OnSecondaryActionButtonClick() throw new ArgumentOutOfRangeException(); } } - + /// /// populate the fields with the given username and email, /// used as a callback to when local player data is changed diff --git a/ColourMeOKGame/Assets/Scripts/Backend.cs b/ColourMeOKGame/Assets/Scripts/Backend.cs index 12fc26c..31e4e42 100644 --- a/ColourMeOKGame/Assets/Scripts/Backend.cs +++ b/ColourMeOKGame/Assets/Scripts/Backend.cs @@ -57,6 +57,12 @@ public enum UserAccountDetailTargetEnum Password } + /// + /// callback functions to be invoked when the user signs in + /// + /// + private readonly List> _onConnectionStatusChangedCallbacks = new(); + /// /// callback functions to be invoked when the user signs in /// @@ -66,12 +72,6 @@ public enum UserAccountDetailTargetEnum /// callback functions to be invoked when the user signs out /// private readonly List _onSignOutCallbacks = new(); - - /// - /// callback functions to be invoked when the user signs in - /// - /// - private readonly List> _onConnectionStatusChangedCallbacks = new (); /// /// the firebase authentication object @@ -87,17 +87,17 @@ 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 /// private string _username; + /// + /// whether the user is signed in + /// + public bool IsSignedIn; + /// /// whether the backend is connected to the firebase backend /// @@ -151,7 +151,7 @@ public void Initialise(Action callback) Debug.Log("firebase status is" + Status); }); } - + /// /// function to fire all on sign in callbacks /// @@ -159,7 +159,6 @@ private void FireOnSignInCallbacks() { Debug.Log($"firing OnSignInCallbacks ({_onSignInCallbacks.Count})"); foreach (var callback in _onSignInCallbacks) - { try { callback.Invoke(_user); @@ -168,9 +167,8 @@ private void FireOnSignInCallbacks() { Debug.LogError($"error invoking OnSignInCallback: {e.Message}"); } - } } - + /// /// function to fire all on sign-out callbacks /// @@ -178,7 +176,6 @@ private void FireOnSignOutCallbacks() { Debug.Log($"firing OnSignOutCallbacks ({_onSignOutCallbacks.Count})"); foreach (var callback in _onSignOutCallbacks) - { try { callback.Invoke(); @@ -187,7 +184,6 @@ private void FireOnSignOutCallbacks() { Debug.LogError($"error invoking OnSignOutCallback: {e.Message}"); } - } } /// @@ -234,10 +230,7 @@ private void AuthStateChanged(object sender, EventArgs eventArgs) if (!IsSignedIn) return; Debug.Log($"signed in successfully as {_user?.UserId}"); - RetrieveUsernameWithCallback((_, _) => - { - FireOnSignInCallbacks(); - }); + RetrieveUsernameWithCallback((_, _) => { FireOnSignInCallbacks(); }); } /// @@ -482,11 +475,12 @@ public void ForgotPassword(string email, Action callback) /// abstraction function to get the user's recent scores from the database /// /// - /// callback function that takes in a DatabaseTransactionResult enum and a List<LocalPlayerData.Score> + /// callback function that takes in a DatabaseTransactionResult enum and a + /// List<LocalPlayerData.Score> /// public void GetRecentScores(Action> callback) { - // TODO + // TODO: implement this callback(DatabaseTransactionResult.Error, new List(0)); } @@ -506,7 +500,10 @@ public void GetRecentScores(Action - /// callback function that takes in a DatabaseTransactionResult enum and a user rating float + /// + /// callback function that takes in a DatabaseTransactionResult enum and a user rating + /// float + /// public void CalculateUserRating( Action callback) { diff --git a/ColourMeOKGame/Assets/Scripts/GameManager.cs b/ColourMeOKGame/Assets/Scripts/GameManager.cs index 44e2a6a..e6611ef 100644 --- a/ColourMeOKGame/Assets/Scripts/GameManager.cs +++ b/ColourMeOKGame/Assets/Scripts/GameManager.cs @@ -18,26 +18,26 @@ public class GameManager : MonoBehaviour /// public UIManager ui; + /// + /// list of callbacks to call when the local player data changes + /// + private readonly List> _onLocalPlayerDataChangeCallbacks = new(); + /// /// the local player data object for storing player data /// private LocalPlayerData _data; - /// - /// read-only property for accessing the local player data outside of this class - /// - public LocalPlayerData Data => _data; - - /// - /// list of callbacks to call when the local player data changes - /// - private readonly List> _onLocalPlayerDataChangeCallbacks = new List>(); - /// /// backend object for handling communication with the firebase backend /// public Backend Backend; + /// + /// read-only property for accessing the local player data outside of this class + /// + public LocalPlayerData Data => _data; + /// /// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances /// @@ -59,12 +59,21 @@ private void Awake() } /// - /// called when the game object is enabled, initialises variables + /// start modifying state + /// + private void Start() + { + Debug.Log("GameManager starts here"); + _data.LoadFromTheWorld(FireLocalPlayerDataChangeCallbacks); + } + + /// + /// initialise variables and ui elements /// private void OnEnable() { ui = UIManager.Instance; - + // load the local player data and refresh the ui _data = new LocalPlayerData(); @@ -91,7 +100,7 @@ private void OnEnable() ui.UI.Q