Guest User

UserAccountManager

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