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