Advertisement
Guest User

Untitled

a guest
Jan 26th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. class ClassOne
  2. {
  3. /// <summary>
  4. ///
  5. /// Script which is called in start to check if we have nickname saved in player prefs
  6. /// if there is a name go and check all other data and if everythig is good switch it to GameScreen
  7. /// if there is no name then show add new user panel
  8. ///
  9. /// </summary>
  10.  
  11. public static void CheckUser()
  12. {
  13. User user = new User();
  14. bool isNew = false;
  15. //Check if users nickname is in PlayerPrefs
  16. string userNickname = GetUserNickname();
  17. if (isNew)
  18. {
  19. ShowNewUserPanel();
  20. }
  21. else
  22. {
  23. if (CheckUserInfo(userNickname))
  24. {
  25. //Wellcome Screen
  26. }
  27. else
  28. {
  29. //Something is wrong
  30. ShowNewUserPanel();
  31. }
  32. }
  33. }
  34.  
  35. public static bool CheckUserInfo(string userNickname)
  36. {
  37. bool isGood = false;
  38. //check all data related to that nickname
  39. // return true if everything is okej
  40. isGood = !IsNullOrEmpty(PlayerPrefs.GetString("user_username"))? true : false; // already done but ...
  41. isGood = !IsNullOrEmpty(PlayerPrefs.GetString("user_randpass")) ? true : false;
  42. return isGood;
  43.  
  44. }
  45. public static string GetUserNickname()
  46. {
  47.  
  48. return GetString("user_username");
  49.  
  50. }
  51. public static void ShowNewUserPanel()
  52. {
  53.  
  54. //Show add user panel
  55.  
  56. }
  57.  
  58. }
  59.  
  60. class ScriptTwo
  61. {
  62. /// <summary>
  63. ///
  64. /// Add new user panel script!
  65. /// Show panel to insert Nickname
  66. /// Make random password
  67. /// Save it to player prefs
  68. ///
  69. /// </summary>
  70.  
  71. public static void AddPlayer()
  72. {
  73. User user = new User();
  74. string nickname = ""; // get it from the panel
  75. string randompassword = CreatePassword();
  76.  
  77. user.Nickname = nickname;
  78. user.Randpassword = randompassword;
  79. user = UserTools.AddNewUser(user);
  80.  
  81.  
  82.  
  83.  
  84. }
  85. public static string CreatePassword()
  86. {
  87. int length = 20;
  88. const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  89. StringBuilder res = new StringBuilder();
  90. System.Random rnd = new System.Random();
  91. while (0 < length--)
  92. {
  93. res.Append(valid[rnd.Next(valid.Length)]);
  94. }
  95. return res.ToString();
  96. }
  97.  
  98.  
  99. }
  100. class GameManager
  101. {
  102. /// <summary>
  103. ///
  104. /// Game Manager
  105. /// This class is NeverDestroy Class
  106. /// Having public static user which we use everywhere and
  107. /// have Refresh function which refresing user after every action
  108. ///
  109. /// Script UserTools have all logic for user
  110. ///
  111. /// </summary>
  112.  
  113.  
  114. public static User user;
  115.  
  116.  
  117. public void UserInit()
  118. {
  119. user = CheckUserInit();
  120. }
  121.  
  122. public void RefreshUser(User tempUser)
  123. {
  124. user.Update(tempUser);
  125. }
  126.  
  127. public User GetStaticUser()
  128. {
  129. return user;
  130. }
  131.  
  132.  
  133. }
  134.  
  135. class UserTools
  136. {
  137.  
  138.  
  139. /// <summary>
  140. ///
  141. /// User Tools
  142. /// All the logic for user in more details
  143. /// to be separated from script that have Update
  144. ///
  145. /// </summary>
  146. ///
  147.  
  148. public User CheckUserInit()
  149. {
  150. User returnUser = new User();
  151. //Get user from PlayerPrefs
  152.  
  153. string status = "User Init Status: ";
  154. string nickname = PlayerPrefs.GetString("user_username");
  155. string randpassword = PlayerPrefs.GetString("user_randompass");
  156. int level = PlayerPrefs.GetInt("user_level");
  157. int experience = PlayerPrefs.GetInt("user_experience");
  158.  
  159.  
  160. if (!IsNullOrEmpty(nickname) &&
  161. !IsNullOrEmpty(randpassword) &&
  162. !IsNullOrEmpty(level) &&
  163. !IsNullOrEmpty(experience))
  164. {
  165.  
  166. status += "Successful!";
  167. }
  168. else
  169. {
  170. status += "Problem Getting Details -";
  171. status += !IsNullOrEmpty(nickname) ? "Username: Successful! , " : "Username: Failed! , ";
  172. status += !IsNullOrEmpty(randpassword) ? "Randompass: Successful! , " : "Randompass: Failed! , ";
  173. status += !IsNullOrEmpty(level) ? "Level: Successful! , " : "Level: Failed! , ";
  174. status += !IsNullOrEmpty(experience) ? "Experience: Successful! , " : "Experience: Failed!";
  175.  
  176. }
  177.  
  178. Debug.Log(status);
  179.  
  180. returnUser.Nickname = nickname;
  181. returnUser.Randpassword = randpassword;
  182. returnUser.Level = level;
  183. returnUser.Experience = experience;
  184.  
  185. return returnUser;
  186. }
  187. public User AddNewUser(User user)
  188. {
  189. user.Experience = 0;
  190. user.Level = 0;
  191.  
  192. return user;
  193. }
  194. public void SaveUser(User user)
  195. {
  196. PlayerPrefs.SetString("user_username", user.Nickname);
  197. PlayerPrefs.SetString("user_randompass", user.Randpassword);
  198. PlayerPrefs.SetInt("user_level", user.Level);
  199. PlayerPrefs.SetInt("user_experience", user.Experience);
  200. Debug.Log("Player Saved!");
  201. }
  202.  
  203.  
  204.  
  205. }
  206.  
  207.  
  208. class User
  209. {
  210. /// <summary>
  211. ///
  212. /// User model Class
  213. ///
  214. /// </summary>
  215. public string Nickname { get; set; }
  216. public string Randpassword { get; set; }
  217. public int Experience { get; set; }
  218. public int Level { get; set; }
  219.  
  220. public User()
  221. {
  222. }
  223.  
  224. public void Update(User user)
  225. {
  226.  
  227. if (IsNullOrEmpty(user.Nicknamame)) { return; }
  228.  
  229. // Add checking for every property
  230.  
  231. this.Experience = user.Experience != 0 ? user.Experience : this.Experience;
  232. this.Level = user.Level != 0 ? user.Level : this.Level;
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement