Advertisement
Guest User

UserAccountManager

a guest
Jan 6th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DatabaseControl;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class UserAccountManager : MonoBehaviour
  8. {
  9.  
  10. public static UserAccountManager instance;
  11.  
  12. void Awake()
  13. {
  14. if (instance != null)
  15. {
  16. Destroy(gameObject);
  17. return;
  18. }
  19.  
  20. instance = this;
  21. DontDestroyOnLoad(this);
  22. }
  23.  
  24. public static string playerUsername { get; protected set; }
  25. public static string playerPassword { get; protected set; }
  26.  
  27. public static string LoggedIn_data { get; protected set; }
  28.  
  29. public static bool isLoggedIn { get; protected set; }
  30.  
  31. public void LogOut()
  32. {
  33. playerUsername = "";
  34. playerPassword = "";
  35. isLoggedIn = false;
  36. SceneManager.LoadScene(0);
  37. }
  38.  
  39. public void LogIn(string _Username, string _Password)
  40. {
  41. playerUsername = _Username;
  42. playerPassword = _Password;
  43. isLoggedIn = true;
  44. SceneManager.LoadScene(1);
  45. }
  46.  
  47. public void SendData(string data)
  48. {
  49. if (isLoggedIn)
  50. StartCoroutine(SetData(data));
  51. }
  52.  
  53. IEnumerator SetData(string data)
  54. {
  55. IEnumerator e = DCF.SetUserData(playerUsername, playerPassword, data); // << Send request to set the player's data string. Provides the username, password and new data string
  56. while (e.MoveNext())
  57. {
  58. yield return e.Current;
  59. }
  60. string response = e.Current as string; // << The returned string from the request
  61.  
  62. if (response == "Success")
  63. {
  64. //The data string was set correctly. Goes back to LoggedIn UI
  65. Debug.Log("Succes sending data");
  66. // loggedInParent.gameObject.SetActive(true);
  67. }
  68. else
  69. {
  70. Debug.Log("Error: Unknown Error. Please try again later. Send data problem");
  71. }
  72. }
  73.  
  74. public void GetData()
  75. {
  76. //Called when the player hits 'Get Data' to retrieve the data string on their account. Switches UI to 'Loading...' and starts coroutine to get the players data string from the server
  77. if (isLoggedIn)
  78. StartCoroutine(GetData_numerator());
  79. }
  80.  
  81. IEnumerator GetData_numerator()
  82. {
  83. string data = "ERROR";
  84. IEnumerator e = DCF.GetUserData(playerUsername, playerPassword); // << Send request to get the player's data string. Provides the username and password
  85. while (e.MoveNext())
  86. {
  87. yield return e.Current;
  88. }
  89. string response = e.Current as string; // << The returned string from the request
  90.  
  91. if (response == "Error")
  92. {
  93. Debug.Log("Error: Unknown Error. Please try again later. GetDataProblem");
  94. }
  95. else
  96. {
  97. //The player's data was retrieved. Goes back to loggedIn UI and displays the retrieved data in the InputField
  98. data = response;
  99. }
  100.  
  101. LoggedIn_data = data;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement