PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Space Wars/Assets/Scripts/RegisterUser.cs

https://bitbucket.org/RobertoDiBi/spacewars-project
C# | 184 lines | 154 code | 24 blank | 6 comment | 23 complexity | 6303f43ba60a0530a2ff2d2f7bd607f5 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System;
  7. using UnityEngine.UI;
  8. using UnityEngine.EventSystems;
  9. using System.Text.RegularExpressions;
  10. public class RegisterUser : MonoBehaviour
  11. {
  12. //Script Author: Roberto Di Biase
  13. public InputField userNameInput;
  14. public InputField emailInput;
  15. public InputField passwordInput;
  16. public InputField confirmPassInput;
  17. //Message box
  18. public Text mesTitle;
  19. public Text mesMessage;
  20. public Button mesButton;
  21. GameObject login;
  22. GameObject errorMessage;
  23. void Awake()
  24. {
  25. login = GameObject.FindWithTag("login");
  26. errorMessage = GameObject.FindWithTag("error");
  27. errorMessage.SetActive(false);
  28. }
  29. public void Registration()
  30. {
  31. Regex VALID_EMAIL_ADDRESS_REGEX =new Regex("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$");
  32. Regex passRegEx = new Regex("(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}");
  33. //Connect to database
  34. Database db = new Database();
  35. //Get Input from game
  36. string userName = userNameInput.text;
  37. string email = emailInput.text;
  38. string password = passwordInput.text;
  39. string confirmPass = confirmPassInput.text;
  40. Match emailMatch = VALID_EMAIL_ADDRESS_REGEX.Match(email);
  41. Match passMatch = passRegEx.Match(password);
  42. //Load all users from Db
  43. List<User> users = db.LoadUsers();
  44. if(userName == "")
  45. {
  46. login.SetActive(false);
  47. errorMessage.SetActive(true);
  48. mesTitle.text = "Missing information";
  49. mesMessage.text = "Please enter your username.";
  50. mesButton.onClick.AddListener(LoadBack);
  51. return;
  52. }
  53. else if (userName.Length < 2 || userName.Length > 15)
  54. {
  55. login.SetActive(false);
  56. errorMessage.SetActive(true);
  57. mesTitle.text = "Input Error";
  58. mesMessage.text = "Your username must be between 2 and 15 character long.";
  59. mesButton.onClick.AddListener(LoadBack);
  60. return;
  61. }
  62. else if(email == "")
  63. {
  64. login.SetActive(false);
  65. errorMessage.SetActive(true);
  66. mesTitle.text = "Missing information";
  67. mesMessage.text = "Please enter your e-mail address.";
  68. mesButton.onClick.AddListener(LoadBack);
  69. return;
  70. }
  71. else if (!emailMatch.Success)
  72. {
  73. login.SetActive(false);
  74. errorMessage.SetActive(true);
  75. mesTitle.text = "Incorect email format";
  76. mesMessage.text = "Please enter a valid e-mail address.";
  77. mesButton.onClick.AddListener(LoadBack);
  78. return;
  79. }
  80. else if(password == "")
  81. {
  82. login.SetActive(false);
  83. errorMessage.SetActive(true);
  84. mesTitle.text = "Missing information";
  85. mesMessage.text = "Please enter your password.";
  86. mesButton.onClick.AddListener(LoadBack);
  87. return;
  88. }
  89. else if (!passMatch.Success)
  90. {
  91. login.SetActive(false);
  92. errorMessage.SetActive(true);
  93. mesTitle.text = "Invalid password";
  94. mesMessage.text = "Weak password. Must contain:\n- 8 Characters or more.\n- 1 or more uppercase characters.\n- 1 or more numeric characters.";
  95. mesButton.onClick.AddListener(LoadBack);
  96. return;
  97. }
  98. else if (confirmPass == "")
  99. {
  100. login.SetActive(false);
  101. errorMessage.SetActive(true);
  102. mesTitle.text = "Missing information";
  103. mesMessage.text = "Please confirm your password.";
  104. mesButton.onClick.AddListener(LoadBack);
  105. return;
  106. }
  107. else if (!password.Equals(confirmPass))
  108. {
  109. login.SetActive(false);
  110. errorMessage.SetActive(true);
  111. mesTitle.text = "Invalid password";
  112. mesMessage.text = "Passwords don't match.";
  113. mesButton.onClick.AddListener(LoadBack);
  114. return;
  115. }
  116. //User and Email validation
  117. foreach (User u in users)
  118. {
  119. if (u.Username.Equals(userName,StringComparison.InvariantCultureIgnoreCase))
  120. {
  121. login.SetActive(false);
  122. errorMessage.SetActive(true);
  123. mesTitle.text = "Resistration error";
  124. mesMessage.text = "This username is already taken.\n\nYour username must be unique.";
  125. mesButton.onClick.AddListener(LoadBack);
  126. return;
  127. }
  128. else if (u.Email.Equals(email))
  129. {
  130. login.SetActive(false);
  131. errorMessage.SetActive(true);
  132. mesTitle.text = "Resistration error";
  133. mesMessage.text = "This email is already registered.";
  134. mesButton.onClick.AddListener(LoadBack);
  135. return;
  136. }
  137. }
  138. User uu = new User(userName, email, password);
  139. db.AddUser(uu);
  140. login.SetActive(false);
  141. errorMessage.SetActive(true);
  142. mesTitle.text = "Successfully registered!";
  143. mesMessage.text = "You are now part of Space Wars family.\nNow you can login and enjoy epic battles with friends.";
  144. mesButton.onClick.AddListener(LoadBack);
  145. Clear(userNameInput);
  146. Clear(emailInput);
  147. Clear(passwordInput);
  148. Clear(confirmPassInput);
  149. }
  150. public void LoadBack()
  151. {
  152. login.SetActive(true);
  153. errorMessage.SetActive(false);
  154. }
  155. public void Clear(InputField inputfield)
  156. {
  157. inputfield.Select();
  158. inputfield.text = "";
  159. }
  160. }