LoginController.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using ET;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class LoginController
  6. {
  7. public static async ETTask CheckVersion()
  8. {
  9. ViewManager.Show<ModalStatusView>("连接中...");
  10. int errorCode = 0;
  11. errorCode = await LoginHelper.CheckVersion(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, GameConst.SERVER_VERSION);
  12. ViewManager.Hide<ModalStatusView>();
  13. if (errorCode != ErrorCode.ERR_Success)
  14. {
  15. ErrorCodeController.Handler(errorCode);
  16. return;
  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.SetString(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. EventAgent.DispatchEvent(ConstMessage.SERVER_CHANGE, serverInfosComponent.CurrentServerId);
  92. await ReqNoticeInfo();
  93. ViewManager.Hide<ModalStatusView>();
  94. }
  95. private static async ETTask ReqNoticeInfo()
  96. {
  97. bool result = await LoginHelper.ReqGetLatestNotice();
  98. if (result)
  99. {
  100. NoticeInfo noticeInfo = NoticeDataManager.Instance.LastNoticeInfo;
  101. Debug.Log("noticeTime:" + noticeInfo.time + " currentTime:" + (TimeInfo.Instance.ServerNow() / 1000));
  102. int noticeTime = TimeUtil.GetDayTimeBySec(noticeInfo.time * 1000, GlobalCfgArray.globalCfg.refreshTime);
  103. int currentTime = TimeUtil.GetDayTimeBySec(TimeInfo.Instance.ServerNow(), GlobalCfgArray.globalCfg.refreshTime);
  104. Debug.Log("noticeTime:" + noticeTime + " currentTime:" + currentTime);
  105. if ((currentTime - noticeTime) / TimeUtil.SECOND_PER_DAY < 3)
  106. {
  107. ViewManager.Show<SystemNoticeView>();
  108. }
  109. }
  110. return;
  111. }
  112. private static void OnLoginFail(int errorCode)
  113. {
  114. ViewManager.Hide<ModalStatusView>();
  115. if (ErrorCodeController.Handler(errorCode))
  116. {
  117. ViewManager.Show<LoginInputView>();
  118. }
  119. }
  120. public static void Logout()
  121. {
  122. GameGlobal.zoneScene.GetComponent<SessionComponent>().AccountSession?.Dispose();
  123. GameGlobal.zoneScene.GetComponent<SessionComponent>().Session?.Dispose();
  124. }
  125. public static async ETTask Register(string account, string password, string name, string identityNum, string code)
  126. {
  127. ViewManager.Show<ModalStatusView>("注册中...");
  128. int errorCode = await LoginHelper.Register(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account, password, name, identityNum, code);
  129. if (errorCode == ErrorCode.ERR_Success)
  130. {
  131. Login(account, password).Coroutine();
  132. }
  133. else
  134. {
  135. ViewManager.Hide<ModalStatusView>();
  136. ErrorCodeController.Handler(errorCode);
  137. }
  138. }
  139. public static async ETTask GetServerInfos()
  140. {
  141. int errorCode = await LoginHelper.GetServerInfos(GameGlobal.zoneScene);
  142. if (errorCode != ErrorCode.ERR_Success)
  143. {
  144. ErrorCodeController.Handler(errorCode);
  145. await ETTask.Create();
  146. return;
  147. }
  148. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  149. if (serverInfosComponent.ServerInfoList.Count <= 0)
  150. {
  151. Alert.Show("服务器列表为空:\n请检查网络和服务器状态")
  152. .SetRightButton(true, "知道啦", (object data) =>
  153. {
  154. Application.Quit();
  155. });
  156. await ETTask.Create();
  157. return;
  158. }
  159. if (serverInfosComponent.CurrentServerId <= 0)
  160. {
  161. var serverInfo = serverInfosComponent.ServerInfoList[0];
  162. serverInfosComponent.CurrentServerId = int.Parse(serverInfo.Id.ToString());
  163. }
  164. }
  165. public static async ETTask GetRoles()
  166. {
  167. int errorCode = await LoginHelper.GetRoles(GameGlobal.zoneScene);
  168. if (errorCode != ErrorCode.ERR_Success)
  169. {
  170. ErrorCodeController.Handler(errorCode);
  171. await ETTask.Create();
  172. }
  173. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  174. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  175. {
  176. var roleInfo = roleInfosComponent.RoleInfos[0];
  177. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  178. await ReqEnterGame();
  179. }
  180. else
  181. {
  182. GameGlobal.isFirstEntry = true;
  183. ViewManager.Hide<ModalStatusView>();
  184. GameController.ShowCreateRole();
  185. }
  186. }
  187. public static async ETTask ReqCreateRole(string roleName)
  188. {
  189. ViewManager.Show<ModalStatusView>("创建角色中...");
  190. int errorCode = await ET.LoginHelper.CreateRole(GameGlobal.zoneScene, roleName);
  191. if (errorCode != ErrorCode.ERR_Success)
  192. {
  193. ViewManager.Hide<ModalStatusView>();
  194. ErrorCodeController.Handler(errorCode);
  195. return;
  196. }
  197. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  198. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  199. {
  200. var roleInfo = roleInfosComponent.RoleInfos[0];
  201. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  202. await ReqEnterGame();
  203. }
  204. ViewManager.Hide(ViewName.CREATE_ROLE_VIEW);
  205. }
  206. public static async ETTask ReqEnterGame()
  207. {
  208. LogServerHelperHttp.SendNodeLog((int)LogNode.StartEnterGame);
  209. int errorCode = await LoginHelper.GetRealmKey(GameGlobal.zoneScene);
  210. if (errorCode != ErrorCode.ERR_Success)
  211. {
  212. ErrorCodeController.Handler(errorCode);
  213. return;
  214. }
  215. errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  216. if (errorCode != ErrorCode.ERR_Success)
  217. {
  218. ErrorCodeController.Handler(errorCode);
  219. return;
  220. }
  221. await GameController.PreEnterGameAsync();
  222. ViewManager.Hide<ModalStatusView>();
  223. LogServerHelperHttp.SendNodeLog((int)LogNode.OnEnterGame);
  224. }
  225. }
  226. }