|
|
|
@ -11,10 +11,48 @@ using UnityEngine;
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Backend : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// enum for the result of the authentication process
|
|
|
|
|
/// </summary>
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public enum AuthenticationResult
|
|
|
|
|
{
|
|
|
|
|
Ok,
|
|
|
|
|
AlreadyAuthenticated,
|
|
|
|
|
NonExistentUser,
|
|
|
|
|
AlreadyExistingUser,
|
|
|
|
|
InvalidCredentials,
|
|
|
|
|
GenericError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generic enum for the result of a database transaction
|
|
|
|
|
/// </summary>
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public enum DatabaseTransactionResult
|
|
|
|
|
{
|
|
|
|
|
Ok,
|
|
|
|
|
Unauthenticated,
|
|
|
|
|
Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// enum for the connection status of the firebase backend
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum FirebaseConnectionStatus
|
|
|
|
|
{
|
|
|
|
|
NotConnected,
|
|
|
|
|
Connected,
|
|
|
|
|
UpdateRequired, // "a required system component is out of date"
|
|
|
|
|
Updating, // "a required system component is updating, retrying in a bit..."
|
|
|
|
|
ExternalError, // "a system component is disabled, invalid, missing, or permissions are insufficient"
|
|
|
|
|
InternalError // "an unknown error occurred"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// whether the backend is connected to the firebase backend
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool connected;
|
|
|
|
|
public FirebaseConnectionStatus status = FirebaseConnectionStatus.NotConnected;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// the firebase authentication object
|
|
|
|
@ -56,6 +94,8 @@ public class Backend : MonoBehaviour
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
SignOutUser();
|
|
|
|
|
_auth.StateChanged -= AuthStateChanged;
|
|
|
|
|
_auth = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -65,6 +105,7 @@ public class Backend : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
|
|
|
|
|
{
|
|
|
|
|
// cher is this robust enough
|
|
|
|
|
switch (task.Result)
|
|
|
|
|
{
|
|
|
|
|
case DependencyStatus.Available:
|
|
|
|
@ -72,28 +113,33 @@ public class Backend : MonoBehaviour
|
|
|
|
|
_auth = FirebaseAuth.GetAuth(app);
|
|
|
|
|
_auth.StateChanged += AuthStateChanged;
|
|
|
|
|
_db = FirebaseDatabase.DefaultInstance.RootReference;
|
|
|
|
|
connected = true;
|
|
|
|
|
Debug.Log("firebase initialised");
|
|
|
|
|
status = FirebaseConnectionStatus.Connected;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DependencyStatus.UnavailableDisabled:
|
|
|
|
|
case DependencyStatus.UnavailableInvalid:
|
|
|
|
|
case DependencyStatus.UnavilableMissing:
|
|
|
|
|
case DependencyStatus.UnavailablePermission:
|
|
|
|
|
Debug.LogError("firebase error outside of our control");
|
|
|
|
|
status = FirebaseConnectionStatus.ExternalError;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DependencyStatus.UnavailableUpdating:
|
|
|
|
|
status = FirebaseConnectionStatus.Updating;
|
|
|
|
|
RetryInitialiseAfterDelay();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DependencyStatus.UnavailableUpdaterequired:
|
|
|
|
|
case DependencyStatus.UnavailableUpdating:
|
|
|
|
|
Debug.LogError("firebase is updating");
|
|
|
|
|
RetryInitialiseAfterDelay();
|
|
|
|
|
status = FirebaseConnectionStatus.UpdateRequired;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DependencyStatus.UnavailableOther:
|
|
|
|
|
default:
|
|
|
|
|
status = FirebaseConnectionStatus.InternalError;
|
|
|
|
|
Debug.LogError("firebase ??? blew up or something," + task.Result);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log("firebase status is" + status);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -104,17 +150,18 @@ public class Backend : MonoBehaviour
|
|
|
|
|
/// <param name="eventArgs">the event arguments</param>
|
|
|
|
|
private void AuthStateChanged(object sender, EventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
// if the user hasn't changed, do nothing
|
|
|
|
|
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;
|
|
|
|
|
switch (signedIn)
|
|
|
|
|
{
|
|
|
|
|
case false when _user != null:
|
|
|
|
|
_onSignOutCallback(_user);
|
|
|
|
|
break;
|
|
|
|
|
case true:
|
|
|
|
|
_onSignInCallback(_auth.CurrentUser);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we're not signed in, but we still hold _user locally, we've signed out
|
|
|
|
|
if (!signedIn && _user != null) _onSignOutCallback(_user);
|
|
|
|
|
|
|
|
|
|
// they have signed in, update _user
|
|
|
|
|
_user = _auth.CurrentUser;
|
|
|
|
|
if (signedIn) _onSignInCallback(_user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -180,10 +227,10 @@ public class Backend : MonoBehaviour
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// abstraction function to submit a play to the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="averageMatchAccuracy">the float percentage (0-100) of how accurate the user was when colour matching</param>
|
|
|
|
|
/// <param name="playData">play data</param>
|
|
|
|
|
/// <param name="callback">callback function that takes in one DatabaseTransactionResult argument</param>
|
|
|
|
|
private void SubmitPlay(
|
|
|
|
|
float averageMatchAccuracy,
|
|
|
|
|
PlayData playData,
|
|
|
|
|
Action<DatabaseTransactionResult> callback)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
@ -235,27 +282,15 @@ public class Backend : MonoBehaviour
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// enum for the result of the authentication process
|
|
|
|
|
/// struct for play data
|
|
|
|
|
/// </summary>
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public enum AuthenticationResult
|
|
|
|
|
public struct PlayData
|
|
|
|
|
{
|
|
|
|
|
Ok,
|
|
|
|
|
AlreadyAuthenticated,
|
|
|
|
|
NonExistentUser,
|
|
|
|
|
AlreadyExistingUser,
|
|
|
|
|
InvalidCredentials,
|
|
|
|
|
GenericError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generic enum for the result of a database transaction
|
|
|
|
|
/// </summary>
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public enum DatabaseTransactionResult
|
|
|
|
|
{
|
|
|
|
|
Ok,
|
|
|
|
|
Unauthenticated,
|
|
|
|
|
Error
|
|
|
|
|
public int RoundsPlayed;
|
|
|
|
|
public float AverageOverallAccuracy;
|
|
|
|
|
public float AverageLuminanceAccuracy;
|
|
|
|
|
public float AverageRedGreenAccuracy;
|
|
|
|
|
public float AverageBlueYellowAccuracy;
|
|
|
|
|
}
|
|
|
|
|
}
|