Advertisement
Raychu

DataBaseControl

Apr 30th, 2017
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.02 KB | None | 0 0
  1. ===========================
  2. Class: Login Menu
  3. ===========================
  4.  
  5. using UnityEngine;
  6. using System.Collections;
  7. using UnityEngine.UI;
  8. using DatabaseControl; // << Remember to add this reference to your scripts which use DatabaseControl
  9.  
  10. public class LoginMenu : MonoBehaviour {
  11.     //All public variables bellow are assigned in the Inspector
  12.    
  13.     //These are the GameObjects which are parents of groups of UI elements. The objects are enabled and disabled to show and hide the UI elements.
  14.     public GameObject loginParent;
  15.     public GameObject registerParent;
  16.     public GameObject loadingParent;
  17.  
  18.     //These are all the InputFields which we need in order to get the entered usernames, passwords, etc
  19.     public InputField Login_UsernameField;
  20.     public InputField Login_PasswordField;
  21.     public InputField Register_UsernameField;
  22.     public InputField Register_PasswordField;
  23.     public InputField Register_ConfirmPasswordField;
  24.  
  25.     //These are the UI Texts which display errors
  26.     public Text Login_ErrorText;
  27.     public Text Register_ErrorText;
  28.  
  29.     //Called at the very start of the game
  30.     void Awake()
  31.     {
  32.         ResetAllUIElements();
  33.     }
  34.  
  35.     //Called by Button Pressed Methods to Reset UI Fields
  36.     void ResetAllUIElements ()
  37.     {
  38.         //This resets all of the UI elements. It clears all the strings in the input fields and any errors being displayed
  39.         Login_UsernameField.text = "";
  40.         Login_PasswordField.text = "";
  41.         Register_UsernameField.text = "";
  42.         Register_PasswordField.text = "";
  43.         Register_ConfirmPasswordField.text = "";
  44.         blankErrors ();
  45.     }
  46.  
  47.     void blankErrors () {
  48.         //blanks all error texts when part is changed e.g. login > Register
  49.         Login_ErrorText.text = "";
  50.         Register_ErrorText.text = "";
  51.     }
  52.  
  53.     //Called by Button Pressed Methods. These use DatabaseControl namespace to communicate with server.
  54.     IEnumerator LoginUser (string username, string password)
  55.     {
  56.         IEnumerator e = DCF.Login(username,password); // << Send request to login, providing username and password
  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.             //Username and Password were correct. Stop showing 'Loading...' and show the LoggedIn UI. And set the text to display the username.
  66.             ResetAllUIElements();
  67. //          loadingParent.gameObject.SetActive(false);
  68. //          backGround.gameObject.SetActive(false);
  69.            // loggedInParent.gameObject.SetActive(true);
  70.             UserAccountManager.instance.LogIn(username,password);
  71.         } else
  72.         {
  73.             //Something went wrong logging in. Stop showing 'Loading...' and go back to LoginUI
  74.             loadingParent.gameObject.SetActive(false);
  75.             loginParent.gameObject.SetActive(true);
  76.             if (response == "UserError")
  77.             {
  78.                 //The Username was wrong so display relevent error message
  79.                 Login_ErrorText.text = "Error: Username not Found";
  80.             } else
  81.             {
  82.                 if (response == "PassError")
  83.                 {
  84.                     //The Password was wrong so display relevent error message
  85.                     Login_ErrorText.text = "Error: Password Incorrect";
  86.                 } else
  87.                 {
  88.                     //There was another error. This error message should never appear, but is here just in case.
  89.                     Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  90.                 }
  91.             }
  92.         }
  93.     }
  94.    
  95.     IEnumerator RegisterUser(string username, string password,string data)
  96.     {
  97.         IEnumerator e = DCF.RegisterUser(username, password, data); // << Send request to register a new user, providing submitted username and password. It also provides an initial value for the data string on the account, which is "Hello World".
  98.         while (e.MoveNext())
  99.         {
  100.             yield return e.Current;
  101.         }
  102.         string response = e.Current as string; // << The returned string from the request
  103.  
  104.         if (response == "Success")
  105.         {
  106.             //Username and Password were valid. Account has been created. Stop showing 'Loading...' and show the loggedIn UI and set text to display the username.
  107.             ResetAllUIElements();
  108.             //loadingParent.gameObject.SetActive(false);
  109.             UserAccountManager.instance.LogIn (username, password);
  110.         } else
  111.         {
  112.             //Something went wrong logging in. Stop showing 'Loading...' and go back to RegisterUI
  113.             loadingParent.gameObject.SetActive(false);
  114.             registerParent.gameObject.SetActive(true);
  115.             if (response == "UserError")
  116.             {
  117.                 //The username has already been taken. Player needs to choose another. Shows error message.
  118.                 Register_ErrorText.text = "Error: Username Already Taken";
  119.             } else
  120.             {
  121.                 //There was another error. This error message should never appear, but is here just in case.
  122.                 Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  123.             }
  124.         }
  125.     }
  126.    
  127.     //UI Button Pressed Methods
  128.     public void Login_LoginButtonPressed ()
  129.     {
  130.         //Called when player presses button to Login
  131.  
  132.         //Get the username and password the player entered
  133.         string username = Login_UsernameField.text;
  134.         string pass = Login_PasswordField.text;
  135.         //Check the lengths of the username and password. (If they are wrong, we might as well show an error now instead of waiting for the request to the server)
  136.         if (username.Length > 3) {
  137.             if (pass.Length > 5) {
  138.                 //Username and password seem reasonable. Change UI to 'Loading...'. Start the Coroutine which tries to log the player in.
  139.                 loginParent.gameObject.SetActive (false);
  140.                 loadingParent.gameObject.SetActive (true);
  141.                 StartCoroutine (LoginUser (username, pass));
  142.             } else {
  143.                 //Password too short so it must be wrong
  144.                 Login_ErrorText.text = "Error: Password format Incorrect (length must be > 5)";
  145.             }
  146.         } else if (username.Length == 0 && pass.Length == 0)
  147.             Login_ErrorText.text = "Error : Blank Field, Try again please.";
  148.         else
  149.         {
  150.             //Username too short so it must be wrong
  151.             Login_ErrorText.text = "Error: Username format Incorrect (length must be > 3)";
  152.         }
  153.     }
  154.  
  155.     public void Login_RegisterButtonPressed () //QUAND LE MEC APPUIE SUR LE BOUTON REGISTER(qui n'estpas actif de base, pour ouvrir les elements)
  156.     {
  157.         //Called when the player hits register on the Login UI, so switches to the Register UI
  158.         ResetAllUIElements();
  159.         loginParent.gameObject.SetActive(false);
  160.         registerParent.gameObject.SetActive(true);
  161.     }
  162.  
  163.     public void Register_RegisterButtonPressed () //APRES AVOIR REMPLIT LES 3 champs USER PASS CONFIRM ...
  164.     {
  165.         //Called when the player presses the button to register
  166.  
  167.         //Get the username and password and repeated password the player entered
  168.         string username = Register_UsernameField.text;
  169.         string password = Register_PasswordField.text;
  170.         string confirmedPassword = Register_ConfirmPasswordField.text;
  171.  
  172.         //Make sure username and password are long enough
  173.         if (username.Length > 3)
  174.         {
  175.             if (password.Length > 5)
  176.             {
  177.                 //Check the two passwords entered match
  178.                 if (password == confirmedPassword)
  179.                 {
  180.                     //Username and passwords seem reasonable. Switch to 'Loading...' and start the coroutine to try and register an account on the server
  181.                     registerParent.gameObject.SetActive(false);
  182.                     loadingParent.gameObject.SetActive(true);
  183.                     StartCoroutine(RegisterUser(username,password,"[KILLS]0/[DEATHS]0"));
  184.                 }
  185.                 else
  186.                 {
  187.                     //Passwords don't match, show error
  188.                     Register_ErrorText.text = "Error: Password's don't Match";
  189.                 }
  190.             }
  191.             else
  192.             {
  193.                 //Password too short so show error
  194.                 Register_ErrorText.text = "Error: Password too Short";
  195.             }
  196.         }
  197.         else
  198.         {
  199.             //Username too short so show error
  200.             Register_ErrorText.text = "Error: Username too Short";
  201.         }
  202.     }
  203.    
  204.     public void Register_BackButtonPressed () //PAS ENVIE DE MENREGISTRER LOL, DONC REVIENS EN ARRIERE
  205.     {
  206.         //Called when the player presses the 'Back' button on the register UI. Switches back to the Login UI
  207.         ResetAllUIElements();
  208.         loginParent.gameObject.SetActive(true);
  209.         registerParent.gameObject.SetActive(false);
  210.     }
  211.  
  212.     public void LoggedIn_LogoutButtonPressed () // A METTRE UN BOUTON LOGOUT SUR LE MENU PRINCIPALE
  213.     {
  214.         //Called when the player hits the 'Logout' button. Switches back to Login UI and forgets the player's username and password.
  215.         //Note: Database Control doesn't use sessions, so no request to the server is needed here to end a session.
  216.         ResetAllUIElements();
  217.         UserAccountManager.instance.LogOut ();
  218.         loginParent.gameObject.SetActive(true);
  219.         //SceneManagement.LoadScene(0);
  220.        // loggedInParent.gameObject.SetActive(false);
  221.     }
  222. }
  223.  
  224. ==========================
  225. Class: UserAccountManager:
  226. ==========================
  227.  
  228. using System.Collections;
  229. using System.Collections.Generic;
  230. using UnityEngine;
  231. using DatabaseControl;
  232. using UnityEngine.SceneManagement;
  233.  
  234. public class UserAccountManager : MonoBehaviour {
  235.  
  236.     public static UserAccountManager instance;
  237.  
  238.     void Awake()
  239.     {
  240.         if (instance != null) {
  241.             Destroy (gameObject);
  242.             return;
  243.         }
  244.        
  245.         instance = this;
  246.         DontDestroyOnLoad (this);
  247.     }
  248.  
  249.     public static string playerUsername{ get; protected set; }
  250.     public static string playerPassword{ get; protected set; }
  251.  
  252.     public static string LoggedIn_data { get; protected set; }
  253.  
  254.     public static bool isLoggedIn{ get; protected set; }
  255.  
  256.     public void LogOut()
  257.     {
  258.         playerUsername = "";
  259.         playerPassword = "";
  260.         isLoggedIn = false;
  261.         SceneManager.LoadScene (0);
  262.     }
  263.  
  264.     public void LogIn(string _Username, string _Password)
  265.     {
  266.         playerUsername = _Username;
  267.         playerPassword = _Password;
  268.         isLoggedIn = true;
  269.         SceneManager.LoadScene (1);
  270.     }
  271.  
  272.     public void SendData(string data)
  273.     {
  274.         if(isLoggedIn)
  275.             StartCoroutine(SetData(data));
  276.     }
  277.  
  278.     IEnumerator SetData (string data)
  279.     {
  280.         IEnumerator e = DCF.SetUserData(playerUsername,playerPassword, data); // << Send request to set the player's data string. Provides the username, password and new data string
  281.         while (e.MoveNext())
  282.         {
  283.             yield return e.Current;
  284.         }
  285.         string response = e.Current as string; // << The returned string from the request
  286.  
  287.         if (response == "Success")
  288.         {
  289.             //The data string was set correctly. Goes back to LoggedIn UI
  290.             Debug.Log("Succes sending data");
  291.             // loggedInParent.gameObject.SetActive(true);
  292.         }
  293.         else
  294.         {
  295.             Debug.Log ("Error: Unknown Error. Please try again later. Send data problem");
  296.         }
  297.     }
  298.  
  299.     public void GetData ()
  300.     {
  301.         //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
  302.         if(isLoggedIn)
  303.             StartCoroutine(GetData_numerator());
  304.     }
  305.  
  306.     IEnumerator GetData_numerator ()
  307.     {
  308.         string data = "ERROR";
  309.         IEnumerator e = DCF.GetUserData(playerUsername,playerPassword); // << Send request to get the player's data string. Provides the username and password
  310.         while (e.MoveNext())
  311.         {
  312.             yield return e.Current;
  313.         }
  314.         string response = e.Current as string; // << The returned string from the request
  315.  
  316.         if (response == "Error") {
  317.             Debug.Log ("Error: Unknown Error. Please try again later. GetDataProblem");
  318.         }
  319.         else
  320.         {
  321.             //The player's data was retrieved. Goes back to loggedIn UI and displays the retrieved data in the InputField
  322.             data = response;
  323.         }
  324.  
  325.         LoggedIn_data = data;
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement