LoginController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using ET;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class LoginController
  6. {
  7. public static async ET.ETTask CheckVersion()
  8. {
  9. ViewManager.Show<ModalStatusView>("连接中...");
  10. int errorCode = 0;
  11. errorCode = await ET.LoginHelper.CheckVersion(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, GameConst.SERVER_VERSION);
  12. ViewManager.Hide<ModalStatusView>();
  13. if (errorCode != ET.ErrorCode.ERR_Success)
  14. {
  15. ErrorCodeController.Handler(errorCode);
  16. await ETTask.Create();
  17. }
  18. }
  19. public static async ET.ETTask LoginTest(string account)
  20. {
  21. ViewManager.Show<ModalStatusView>("登录中...");
  22. int errorCode = await ET.LoginHelper.LoginTest(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account);
  23. if (errorCode == ET.ErrorCode.ERR_Success)
  24. {
  25. GameGlobal.isVisitor = false;
  26. ViewManager.Hide<LoginInputView>();
  27. ViewManager.Hide<RegisterView>();
  28. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  29. GameGlobal.userId = accountInfoComponent.AccountId;
  30. GameGlobal.userAge = accountInfoComponent.Age;
  31. if (GameGlobal.isVisitor)
  32. {
  33. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  34. }
  35. else
  36. {
  37. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  38. PlayerPrefs.DeleteKey(GameConst.PASSWORD_LAST_LOGIN_KEY);
  39. }
  40. GameController.CheckSpecialAccount("sygfg");
  41. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  42. PlayerPrefs.Save();
  43. await OnLoginSuccess();
  44. }
  45. else
  46. {
  47. OnLoginFail(errorCode);
  48. }
  49. }
  50. public static async ET.ETTask Login(string account, string password, bool isMD5 = false)
  51. {
  52. ViewManager.Show<ModalStatusView>("登录中...");
  53. int errorCode = await ET.LoginHelper.Login(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account, password, isMD5);
  54. if (errorCode == ET.ErrorCode.ERR_Success)
  55. {
  56. GameGlobal.isVisitor = false;
  57. ViewManager.Hide<LoginInputView>();
  58. ViewManager.Hide<RegisterView>();
  59. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  60. GameGlobal.userId = accountInfoComponent.AccountId;
  61. GameGlobal.userAge = accountInfoComponent.Age;
  62. if (GameGlobal.isVisitor)
  63. {
  64. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  65. }
  66. else
  67. {
  68. var passwordMD5 = password;
  69. //密码禁止明文传输
  70. if (!isMD5)
  71. {
  72. passwordMD5 = MD5Helper.stringMD5(password);
  73. }
  74. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  75. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, passwordMD5);
  76. }
  77. GameController.CheckSpecialAccount(account);
  78. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  79. PlayerPrefs.Save();
  80. await OnLoginSuccess();
  81. }
  82. else
  83. {
  84. OnLoginFail(errorCode);
  85. }
  86. }
  87. private static async ETTask OnLoginSuccess()
  88. {
  89. await GetServerInfos();
  90. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  91. if (serverInfosComponent.ServerInfoList.Count > 1)
  92. {
  93. ViewManager.Hide<ModalStatusView>();
  94. EventAgent.DispatchEvent(ConstMessage.SERVER_CHANGE, serverInfosComponent.CurrentServerId);
  95. }
  96. else
  97. {
  98. await GetRoles();
  99. }
  100. }
  101. private static void OnLoginFail(int errorCode)
  102. {
  103. ViewManager.Hide<ModalStatusView>();
  104. if (ErrorCodeController.Handler(errorCode))
  105. {
  106. ViewManager.Show<LoginInputView>();
  107. }
  108. }
  109. public static void Logout()
  110. {
  111. GameGlobal.zoneScene.GetComponent<SessionComponent>().Session?.Dispose();
  112. }
  113. public static async ETTask Register(string account, string password, string name, string identityNum, string code)
  114. {
  115. ViewManager.Show<ModalStatusView>("注册中...");
  116. int errorCode = await LoginHelper.Register(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account, password, name, identityNum, code);
  117. if (errorCode == ErrorCode.ERR_Success)
  118. {
  119. Login(account, password).Coroutine();
  120. }
  121. else
  122. {
  123. ViewManager.Hide<ModalStatusView>();
  124. ErrorCodeController.Handler(errorCode);
  125. }
  126. }
  127. public static async ETTask GetServerInfos()
  128. {
  129. int errorCode = await LoginHelper.GetServerInfos(GameGlobal.zoneScene);
  130. if (errorCode != ErrorCode.ERR_Success)
  131. {
  132. ErrorCodeController.Handler(errorCode);
  133. await ETTask.Create();
  134. return;
  135. }
  136. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  137. if (serverInfosComponent.ServerInfoList.Count <= 0)
  138. {
  139. Alert.Show("服务器列表为空:\n请检查网络和服务器状态")
  140. .SetRightButton(true, "知道啦", (object data) =>
  141. {
  142. Application.Quit();
  143. });
  144. await ETTask.Create();
  145. return;
  146. }
  147. if (serverInfosComponent.CurrentServerId <= 0)
  148. {
  149. var serverInfo = serverInfosComponent.ServerInfoList[0];
  150. serverInfosComponent.CurrentServerId = int.Parse(serverInfo.Id.ToString());
  151. }
  152. }
  153. public static async ETTask GetRoles()
  154. {
  155. int errorCode = await LoginHelper.GetRoles(GameGlobal.zoneScene);
  156. if (errorCode != ErrorCode.ERR_Success)
  157. {
  158. ErrorCodeController.Handler(errorCode);
  159. await ETTask.Create();
  160. }
  161. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  162. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  163. {
  164. var roleInfo = roleInfosComponent.RoleInfos[0];
  165. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  166. await ReqEnterGame();
  167. }
  168. else
  169. {
  170. GameGlobal.isFirstEntry = true;
  171. ViewManager.Hide<ModalStatusView>();
  172. GameController.ShowCreateRole();
  173. }
  174. }
  175. public static async ETTask ReqCreateRole(string roleName)
  176. {
  177. ViewManager.Show<ModalStatusView>("创建角色中...");
  178. int errorCode = await ET.LoginHelper.CreateRole(GameGlobal.zoneScene, roleName);
  179. if (errorCode != ErrorCode.ERR_Success)
  180. {
  181. ViewManager.Hide<ModalStatusView>();
  182. ErrorCodeController.Handler(errorCode);
  183. return;
  184. }
  185. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  186. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  187. {
  188. var roleInfo = roleInfosComponent.RoleInfos[0];
  189. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  190. await ReqEnterGame();
  191. }
  192. ViewManager.Hide(ViewName.CREATE_ROLE_VIEW);
  193. }
  194. public static async ETTask ReqEnterGame()
  195. {
  196. int errorCode = await LoginHelper.GetRealmKey(GameGlobal.zoneScene);
  197. if (errorCode != ErrorCode.ERR_Success)
  198. {
  199. ErrorCodeController.Handler(errorCode);
  200. return;
  201. }
  202. errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  203. if (errorCode != ErrorCode.ERR_Success)
  204. {
  205. ErrorCodeController.Handler(errorCode);
  206. return;
  207. }
  208. await GameController.PreEnterGameAsync();
  209. ViewManager.Hide<ModalStatusView>();
  210. }
  211. }
  212. }