diff --git a/ColourMeOKGame/Assets/Scripts/AccountUI.cs b/ColourMeOKGame/Assets/Scripts/AccountUI.cs index a46cabc..f816cbc 100644 --- a/ColourMeOKGame/Assets/Scripts/AccountUI.cs +++ b/ColourMeOKGame/Assets/Scripts/AccountUI.cs @@ -378,13 +378,13 @@ private void OnUsernameUpdateButtonClick() GameManager.Instance.Backend.UpdateUserAccountDetail(Backend.UserAccountDetailTargetEnum.Username, _usernameField.value, - dtr => + res => { _accompanyingText.style.display = DisplayStyle.Flex; - _accompanyingText.text = dtr switch + _accompanyingText.text = res switch { - Backend.DatabaseTransactionResult.Ok => "Username updated!", - Backend.DatabaseTransactionResult.Unauthenticated => + Backend.TransactionResult.Ok => "Username updated!", + Backend.TransactionResult.Unauthenticated => "You are not signed in. Please sign in to update your username.", _ => "An error occurred updating the username. Please try again." }; @@ -414,9 +414,9 @@ private void OnEmailUpdateButtonClick() _accompanyingText.style.display = DisplayStyle.Flex; _accompanyingText.text = callback switch { - Backend.DatabaseTransactionResult.Ok => + Backend.TransactionResult.Ok => $"Verification email sent to {_emailField.value}! You may want to sign in again to see the changes.", - Backend.DatabaseTransactionResult.Unauthenticated => + Backend.TransactionResult.Unauthenticated => "You are not signed in. Please sign in to update your email.", _ => "An error occurred updating the email. Please try again." }; @@ -446,8 +446,8 @@ private void OnPasswordUpdateButtonClick() _accompanyingText.style.display = DisplayStyle.Flex; _accompanyingText.text = callback switch { - Backend.DatabaseTransactionResult.Ok => "Password updated!", - Backend.DatabaseTransactionResult.Unauthenticated => + Backend.TransactionResult.Ok => "Password updated!", + Backend.TransactionResult.Unauthenticated => "You are not signed in. Please sign in to update your password.", _ => "An error occurred updating the password. Please try again." }; @@ -635,12 +635,23 @@ private void OnTertiaryActionButtonClick() // delete local data case State.NotSignedIn: PlayerPrefs.DeleteAll(); + _accompanyingText.style.display = DisplayStyle.Flex; + _accompanyingText.text = "Local data deleted."; GameManager.Instance.Data.LoadFromTheWorld(GameManager.Instance.FireLocalPlayerDataChangeCallbacks); break; // delete user account case State.SignedIn: - GameManager.Instance.Backend.DeleteUser(); + GameManager.Instance.Backend.DeleteUser(result => + { + _accompanyingText.style.display = DisplayStyle.Flex; + _accompanyingText.text = result switch + { + Backend.TransactionResult.Ok => "Account deleted.", + Backend.TransactionResult.Unauthenticated => "You are not signed in.", + _ => "An error occurred deleting the account. Please try again." + }; + }); break; case State.AfterContinue: diff --git a/ColourMeOKGame/Assets/Scripts/Backend.cs b/ColourMeOKGame/Assets/Scripts/Backend.cs index 55fd00a..d3a665b 100644 --- a/ColourMeOKGame/Assets/Scripts/Backend.cs +++ b/ColourMeOKGame/Assets/Scripts/Backend.cs @@ -28,16 +28,6 @@ public enum AuthenticationResult GenericError } - /// - /// generic enum for the result of a database transaction - /// - public enum DatabaseTransactionResult - { - Ok, - Unauthenticated, - Error - } - /// /// enum for the connection status of the firebase backend /// @@ -51,6 +41,16 @@ public enum FirebaseConnectionStatus InternalError // "an unknown error occurred" } + /// + /// generic enum for the result of a database transaction + /// + public enum TransactionResult + { + Ok, + Unauthenticated, + Error + } + public enum UserAccountDetailTargetEnum { Username, @@ -454,14 +454,14 @@ private void RetrieveUsername() /// /// function to retrieve the user's username from the database /// - private void RetrieveUsernameWithCallback(Action callback) + private void RetrieveUsernameWithCallback(Action callback) { if (!Status.Equals(FirebaseConnectionStatus.Connected)) return; if (_user == null) { Debug.LogError("receiving username post-authentication but user is null (should be unreachable)"); - callback(DatabaseTransactionResult.Unauthenticated, "Unknown"); + callback(TransactionResult.Unauthenticated, "Unknown"); return; } @@ -471,16 +471,16 @@ private void RetrieveUsernameWithCallback(Action { - DatabaseTransactionResult result; + TransactionResult result; if (task.IsCompletedSuccessfully) { - result = DatabaseTransactionResult.Ok; + result = TransactionResult.Ok; _username = task.Result.Value.ToString(); Debug.Log($"our username is {_username}"); } else { - result = DatabaseTransactionResult.Error; + result = TransactionResult.Error; _username = "Unknown"; Debug.LogError("failed to get username"); } @@ -514,18 +514,33 @@ public void SignOutUser() /// /// abstraction function to delete the user /// - public void DeleteUser() + public void DeleteUser(Action callback) { + if (!Status.Equals(FirebaseConnectionStatus.Connected)) + { + callback(TransactionResult.Error); + return; + } + + if (_user == null) + { + callback(TransactionResult.Unauthenticated); + return; + } + _user.DeleteAsync().ContinueWithOnMainThread(task => { if (task.IsCompletedSuccessfully) { Debug.Log("user deleted"); - SignOutUser(); + _user = null; + FireOnSignOutCallbacks(); + callback(TransactionResult.Ok); } else { - Debug.LogError(task.Exception); + Debug.LogError($"error deleting user: {task.Exception}"); + callback(TransactionResult.Error); } }); } @@ -556,16 +571,16 @@ public void ResetUserPassword(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 + /// callback function that takes in a TransactionResult enum and a /// List<LocalPlayerData.Score> /// - public void GetRecentScores(Action> callback) + public void GetRecentScores(Action> callback) { if (!Status.Equals(FirebaseConnectionStatus.Connected)) return; if (_user == null) { - callback(DatabaseTransactionResult.Unauthenticated, new List(0)); + callback(TransactionResult.Unauthenticated, new List(0)); return; } @@ -578,7 +593,7 @@ public void GetRecentScores(Action(0)); + callback(TransactionResult.Error, new List(0)); return; } @@ -594,7 +609,7 @@ public void GetRecentScores(Action /// - /// callback function that takes in a DatabaseTransactionResult enum and a + /// callback function that takes in a TransactionResult enum and a /// List<LocalPlayerData.Score> /// - private void GetBestScores(Action> callback) + private void GetBestScores(Action> callback) { if (!Status.Equals(FirebaseConnectionStatus.Connected)) return; if (_user == null) { - callback(DatabaseTransactionResult.Unauthenticated, new List(0)); + callback(TransactionResult.Unauthenticated, new List(0)); return; } @@ -625,7 +640,7 @@ private void GetBestScores(Action(0)); + callback(TransactionResult.Error, new List(0)); return; } @@ -641,7 +656,7 @@ private void GetBestScores(Action /// score - /// callback function that takes in a DatabaseTransactionResult enum + /// callback function that takes in a TransactionResult enum public void SubmitScore( LocalPlayerData.Score score, - Action callback) + Action callback) { if (!Status.Equals(FirebaseConnectionStatus.Connected)) return; if (_user == null) { - callback(DatabaseTransactionResult.Unauthenticated); + callback(TransactionResult.Unauthenticated); return; } @@ -670,12 +685,12 @@ private void GetBestScores(Action /// - /// callback function that takes in a DatabaseTransactionResult enum and a user rating + /// callback function that takes in a TransactionResult enum and a user rating /// float /// public void CalculateUserRating( - Action callback) + Action callback) { GetRecentScores((recentRes, recentScores) => { - if (recentRes != DatabaseTransactionResult.Ok) + if (recentRes != TransactionResult.Ok) { Debug.Log("failed to get recent scores"); callback(recentRes, 0f); @@ -706,7 +721,7 @@ private void GetBestScores(Action { - if (bestRes != DatabaseTransactionResult.Ok) + if (bestRes != TransactionResult.Ok) { Debug.Log("failed to get recent scores"); GameManager.Instance.FireLocalPlayerDataChangeCallbacks(GameManager.Instance.Data); @@ -719,7 +734,7 @@ private void GetBestScores(Action LocalPlayerData.MaxBestScores) bestScoreQueue.Dequeue(); GameManager.Instance.FireLocalPlayerDataChangeCallbacks(GameManager.Instance.Data); - callback(DatabaseTransactionResult.Ok, GameManager.Instance.Data.CalculateUserRating()); + callback(TransactionResult.Ok, GameManager.Instance.Data.CalculateUserRating()); }); }); } @@ -727,20 +742,20 @@ private void GetBestScores(Action /// abstraction function to update the user's rating in the database /// - /// callback function that takes in a DatabaseTransactionResult enum + /// callback function that takes in a TransactionResult enum public void UpdateUserRating( - Action callback) + Action callback) { if (!Status.Equals(FirebaseConnectionStatus.Connected)) return; - + if (_user == null) { - callback(DatabaseTransactionResult.Unauthenticated); + callback(TransactionResult.Unauthenticated); return; } - + var userRating = GameManager.Instance.Data.CalculateUserRating(); - + _db.Child("users") .Child(_user.UserId) .Child("rating") @@ -750,12 +765,12 @@ private void GetBestScores(Action /// - /// callback function that takes in a DatabaseTransactionResult enum and a List<LeaderboardEntry> + /// callback function that takes in a TransactionResult enum and a List<LeaderboardEntry> /// public void GetLeaderboard( - Action callback) + Action callback) { throw new NotImplementedException(); } @@ -777,18 +792,18 @@ private void GetBestScores(Action /// the target account detail to update /// the new value for the target account detail - /// callback function that takes in a DatabaseTransactionResult enum + /// callback function that takes in a TransactionResult enum /// thrown when the target is not a valid UserAccountDetailTargetEnum public void UpdateUserAccountDetail( UserAccountDetailTargetEnum target, string newValue, - Action callback) + Action callback) { - if (!Status.Equals(FirebaseConnectionStatus.Connected)) callback(DatabaseTransactionResult.Unauthenticated); + if (!Status.Equals(FirebaseConnectionStatus.Connected)) callback(TransactionResult.Unauthenticated); if (_user == null) { - callback(DatabaseTransactionResult.Unauthenticated); + callback(TransactionResult.Unauthenticated); return; } @@ -801,12 +816,12 @@ private void GetBestScores(Action playedRounds) gameChromaAcc /= Gameplay.RoundsPerGame; gameHueAcc /= Gameplay.RoundsPerGame; gamePerceivedAcc /= Gameplay.RoundsPerGame; - + var adjustedHistoricalLightnessAcc = historicalLightnessAcc * (historicalPerceivedAcc / 100d); var adjustedHistoricalChromaAcc = historicalChromaAcc * (historicalPerceivedAcc / 100d); var adjustedHistoricalHueAcc = historicalHueAcc * (historicalPerceivedAcc / 100d); @@ -295,7 +295,7 @@ public void SignalGameEnd(List playedRounds) Backend.SubmitScore(score, submitRes => { - if (submitRes != Backend.DatabaseTransactionResult.Ok) + if (submitRes != Backend.TransactionResult.Ok) { Debug.Log("couldn't submit score"); TransitionToResultsView(_data.CalculateUserRating()); @@ -304,7 +304,7 @@ public void SignalGameEnd(List playedRounds) Backend.CalculateUserRating((urcRes, userRating) => { - if (urcRes != Backend.DatabaseTransactionResult.Ok) + if (urcRes != Backend.TransactionResult.Ok) { Debug.Log("couldn't calculate user rating"); TransitionToResultsView(_data.CalculateUserRating()); @@ -313,7 +313,7 @@ public void SignalGameEnd(List playedRounds) Backend.UpdateUserRating(updateRes => { - if (updateRes != Backend.DatabaseTransactionResult.Ok) + if (updateRes != Backend.TransactionResult.Ok) { Debug.Log("calculated user rating but couldn't update it"); TransitionToResultsView(userRating); @@ -330,15 +330,17 @@ public void SignalGameEnd(List playedRounds) void TransitionToResultsView(float rating) { Debug.Log("signal GameManager-UIManager transition to results view"); - + var ratingDifferenceDescriptor = rating > oldRating ? "increased" : "decreased"; var ratingText = rating >= 0 ? $"\nYour rating has {ratingDifferenceDescriptor} by {Math.Abs(rating - oldRating):F2}." : "\nYour rating could not be calculated."; // build the result text and show the results view - ui.UI.Q