Advertisement
Guest User

LoginMenu

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.67 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using DatabaseControl; // << Remember to add this reference to your scripts which use DatabaseControl
  5.  
  6.  
  7. public class LoginMenu : MonoBehaviour
  8. {
  9.     //All public variables bellow are assigned in the Inspector
  10.  
  11.     //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.
  12.     public GameObject loginParent;
  13.     public GameObject registerParent;
  14.     public GameObject loadingParent;
  15.  
  16.     //These are all the InputFields which we need in order to get the entered usernames, passwords, etc
  17.     public InputField Login_UsernameField;
  18.     public InputField Login_PasswordField;
  19.     public InputField Register_UsernameField;
  20.     public InputField Register_PasswordField;
  21.     public InputField Register_ConfirmPasswordField;
  22.  
  23.     //These are the UI Texts which display errors
  24.     public Text Login_ErrorText;
  25.     public Text Register_ErrorText;
  26.  
  27.     //Called at the very start of the game
  28.     void Awake()
  29.     {
  30.         ResetAllUIElements();
  31.     }
  32.  
  33.     //Called by Button Pressed Methods to Reset UI Fields
  34.     void ResetAllUIElements()
  35.     {
  36.         //This resets all of the UI elements. It clears all the strings in the input fields and any errors being displayed
  37.         Login_UsernameField.text = "";
  38.         Login_PasswordField.text = "";
  39.         Register_UsernameField.text = "";
  40.         Register_PasswordField.text = "";
  41.         Register_ConfirmPasswordField.text = "";
  42.         blankErrors();
  43.     }
  44.  
  45.     void blankErrors()
  46.     {
  47.         //blanks all error texts when part is changed e.g. login > Register
  48.         Login_ErrorText.text = "";
  49.         Register_ErrorText.text = "";
  50.     }
  51.  
  52.     //Called by Button Pressed Methods. These use DatabaseControl namespace to communicate with server.
  53.     IEnumerator LoginUser(string username, string password)
  54.     {
  55.         IEnumerator e = DCF.Login(username, password); // << Send request to login, providing username and password
  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.             //Username and Password were correct. Stop showing 'Loading...' and show the LoggedIn UI. And set the text to display the username.
  65.             ResetAllUIElements();
  66.             //          loadingParent.gameObject.SetActive(false);
  67.             //          backGround.gameObject.SetActive(false);
  68.             // loggedInParent.gameObject.SetActive(true);
  69.             UserAccountManager.instance.LogIn(username, password);
  70.         }
  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.             }
  81.             else
  82.             {
  83.                 if (response == "PassError")
  84.                 {
  85.                     //The Password was wrong so display relevent error message
  86.                     Login_ErrorText.text = "Error: Password Incorrect";
  87.                 }
  88.                 else
  89.                 {
  90.                     //There was another error. This error message should never appear, but is here just in case.
  91.                     Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     IEnumerator RegisterUser(string username, string password, string data)
  98.     {
  99.         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".
  100.         while (e.MoveNext())
  101.         {
  102.             yield return e.Current;
  103.         }
  104.         string response = e.Current as string; // << The returned string from the request
  105.  
  106.         if (response == "Success")
  107.         {
  108.             //Username and Password were valid. Account has been created. Stop showing 'Loading...' and show the loggedIn UI and set text to display the username.
  109.             ResetAllUIElements();
  110.             //loadingParent.gameObject.SetActive(false);
  111.             UserAccountManager.instance.LogIn(username, password);
  112.         }
  113.         else
  114.         {
  115.             //Something went wrong logging in. Stop showing 'Loading...' and go back to RegisterUI
  116.             loadingParent.gameObject.SetActive(false);
  117.             registerParent.gameObject.SetActive(true);
  118.             if (response == "UserError")
  119.             {
  120.                 //The username has already been taken. Player needs to choose another. Shows error message.
  121.                 Register_ErrorText.text = "Error: Username Already Taken";
  122.             }
  123.             else
  124.             {
  125.                 //There was another error. This error message should never appear, but is here just in case.
  126.                 Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  127.             }
  128.         }
  129.     }
  130.  
  131.     //UI Button Pressed Methods
  132.     public void Login_LoginButtonPressed()
  133.     {
  134.         //Called when player presses button to Login
  135.  
  136.         //Get the username and password the player entered
  137.         string username = Login_UsernameField.text;
  138.         string pass = Login_PasswordField.text;
  139.         //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)
  140.         if (username.Length > 3)
  141.         {
  142.             if (pass.Length > 5)
  143.             {
  144.                 //Username and password seem reasonable. Change UI to 'Loading...'. Start the Coroutine which tries to log the player in.
  145.                 loginParent.gameObject.SetActive(false);
  146.                 loadingParent.gameObject.SetActive(true);
  147.                 StartCoroutine(LoginUser(username, pass));
  148.             }
  149.             else
  150.             {
  151.                 //Password too short so it must be wrong
  152.                 Login_ErrorText.text = "Error: Password format Incorrect (length must be > 5)";
  153.             }
  154.         }
  155.         else if (username.Length == 0 && pass.Length == 0)
  156.             Login_ErrorText.text = "Error : Blank Field, Try again please.";
  157.         else
  158.         {
  159.             //Username too short so it must be wrong
  160.             Login_ErrorText.text = "Error: Username format Incorrect (length must be > 3)";
  161.         }
  162.     }
  163.  
  164.     public void Login_RegisterButtonPressed() //QUAND LE MEC APPUIE SUR LE BOUTON REGISTER(qui n'estpas actif de base, pour ouvrir les elements)
  165.     {
  166.         //Called when the player hits register on the Login UI, so switches to the Register UI
  167.         ResetAllUIElements();
  168.         loginParent.gameObject.SetActive(false);
  169.         registerParent.gameObject.SetActive(true);
  170.     }
  171.  
  172.     public void Register_RegisterButtonPressed() //APRES AVOIR REMPLIT LES 3 champs USER PASS CONFIRM ...
  173.     {
  174.         //Called when the player presses the button to register
  175.  
  176.         //Get the username and password and repeated password the player entered
  177.         string username = Register_UsernameField.text;
  178.         string password = Register_PasswordField.text;
  179.         string confirmedPassword = Register_ConfirmPasswordField.text;
  180.  
  181.         //Make sure username and password are long enough
  182.         if (username.Length > 3)
  183.         {
  184.             if (password.Length > 5)
  185.             {
  186.                 //Check the two passwords entered match
  187.                 if (password == confirmedPassword)
  188.                 {
  189.                     //Username and passwords seem reasonable. Switch to 'Loading...' and start the coroutine to try and register an account on the server
  190.                     registerParent.gameObject.SetActive(false);
  191.                     loadingParent.gameObject.SetActive(true);
  192.                     StartCoroutine(RegisterUser(username, password, "[KILLS]0/[DEATHS]0"));
  193.                 }
  194.                 else
  195.                 {
  196.                     //Passwords don't match, show error
  197.                     Register_ErrorText.text = "Error: Password's don't Match";
  198.                 }
  199.             }
  200.             else
  201.             {
  202.                 //Password too short so show error
  203.                 Register_ErrorText.text = "Error: Password too Short";
  204.             }
  205.         }
  206.         else
  207.         {
  208.             //Username too short so show error
  209.             Register_ErrorText.text = "Error: Username too Short";
  210.         }
  211.     }
  212.  
  213.     public void Register_BackButtonPressed() //PAS ENVIE DE MENREGISTRER LOL, DONC REVIENS EN ARRIERE
  214.     {
  215.         //Called when the player presses the 'Back' button on the register UI. Switches back to the Login UI
  216.         ResetAllUIElements();
  217.         loginParent.gameObject.SetActive(true);
  218.         registerParent.gameObject.SetActive(false);
  219.     }
  220.  
  221.     public void LoggedIn_LogoutButtonPressed() // A METTRE UN BOUTON LOGOUT SUR LE MENU PRINCIPALE
  222.     {
  223.         //Called when the player hits the 'Logout' button. Switches back to Login UI and forgets the player's username and password.
  224.         //Note: Database Control doesn't use sessions, so no request to the server is needed here to end a session.
  225.         ResetAllUIElements();
  226.         UserAccountManager.instance.LogOut();
  227.         loginParent.gameObject.SetActive(true);
  228.         //SceneManagement.LoadScene(0);
  229.         // loggedInParent.gameObject.SetActive(false);
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement