LoginController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using ET;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class LoginController
  6. {
  7. public static string ageTipsTitle = "适龄提示说明";
  8. public static string ageTips = "1)本游戏是一款角色扮演类游戏,适用于年满16周岁及以上的用户,建议未成年人在家长监护下使用游戏产品。\n2)本游戏基于架空的故事背景和幻想世界观,剧情简单且积极向上,没有基于真实历史和现实事件的改编内容游戏玩法以收集不同服装进行搭配展开,有较为简单的竞技和对抗玩法,游戏中有基于文字的陌生人社交系统。\n3)本游戏中有用户实名认证系统,认证为未成年人的用户将接受以下管理:游戏中部分玩法和道具需要付费。未满8周岁的用户不能付费; 8周岁以上未满16周岁的未成年人用户,单次充值金额不得超过50元人民币,每月充值金额累计不得超过200元人民币; 16周岁以上的未成年人用户,单次充值金额不得超过100元人民币,每月充值金额累计不得超过400元人民币。\n4)本游戏以服装收集及搭配为主要玩法,有助于玩家开阔眼界、进一步提升审美能力。游戏中很多服装融入了中华传统元素,可以有助于中华传统文化的传播,体验中国的古典美。";
  9. public static void ShowLogin()
  10. {
  11. LauncherView.Instance.Close();
  12. ViewManager.Show<LoginView>();
  13. }
  14. public static async ET.ETTask LoginTest(string account)
  15. {
  16. ViewManager.Show<ModalStatusView>("登录中...");
  17. int errorCode = await ET.LoginHelper.LoginTest(GameGlobal.zoneScene, GameConfig.LoginAddress, account);
  18. if (errorCode == ET.ErrorCode.ERR_Success)
  19. {
  20. GameGlobal.isVisitor = false;
  21. ViewManager.Hide<LoginInputView>();
  22. ViewManager.Hide<RegisterView>();
  23. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  24. GameGlobal.userId = accountInfoComponent.AccountId;
  25. GameGlobal.userAge = accountInfoComponent.Age;
  26. if (GameGlobal.isVisitor)
  27. {
  28. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  29. }
  30. else
  31. {
  32. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  33. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, "");
  34. }
  35. GameController.CheckSpecialAccount("sygfg");
  36. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  37. PlayerPrefs.Save();
  38. await OnLoginSuccess();
  39. }
  40. else
  41. {
  42. OnLoginFail(errorCode);
  43. }
  44. }
  45. public static async ET.ETTask Login(string account, string password, bool isMD5 = false)
  46. {
  47. ViewManager.Show<ModalStatusView>("登录中...");
  48. int errorCode = await ET.LoginHelper.Login(GameGlobal.zoneScene, GameConfig.LoginAddress, account, password, isMD5);
  49. if (errorCode == ET.ErrorCode.ERR_Success)
  50. {
  51. GameGlobal.isVisitor = false;
  52. ViewManager.Hide<LoginInputView>();
  53. ViewManager.Hide<RegisterView>();
  54. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  55. GameGlobal.userId = accountInfoComponent.AccountId;
  56. GameGlobal.userAge = accountInfoComponent.Age;
  57. if (GameGlobal.isVisitor)
  58. {
  59. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  60. }
  61. else
  62. {
  63. var passwordMD5 = password;
  64. //密码禁止明文传输
  65. if (!isMD5)
  66. {
  67. passwordMD5 = MD5Helper.stringMD5(password);
  68. }
  69. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  70. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, passwordMD5);
  71. }
  72. GameController.CheckSpecialAccount(account);
  73. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  74. PlayerPrefs.Save();
  75. await OnLoginSuccess();
  76. }
  77. else
  78. {
  79. OnLoginFail(errorCode);
  80. }
  81. }
  82. private static async ETTask OnLoginSuccess()
  83. {
  84. await GetServerInfos();
  85. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  86. EventAgent.DispatchEvent(ConstMessage.SERVER_CHANGE, serverInfosComponent.CurrentServerId);
  87. EventAgent.DispatchEvent(ConstMessage.LOGIN_SUCCESS);
  88. if (AntiAddictionController.CheckAntiAddictionWhenLogin())
  89. {
  90. ViewManager.Hide<ModalStatusView>();
  91. return;
  92. }
  93. await ReqNoticeInfo();
  94. ViewManager.Hide<ModalStatusView>();
  95. }
  96. private static async ETTask ReqNoticeInfo()
  97. {
  98. int result = await LoginHelper.ReqGetLatestNotice();
  99. if (result == ErrorCode.ERR_Success)
  100. {
  101. NoticeInfo noticeInfo = NoticeDataManager.Instance.LastNoticeInfo;
  102. // Debug.Log("noticeTime:" + noticeInfo.time + " currentTime:" + (TimeInfo.Instance.ServerNow() / 1000));
  103. long noticeTime = TimeUtil.GetDayTimeBySec(noticeInfo.time, GlobalCfgArray.globalCfg.refreshTime);
  104. long currentTime = TimeUtil.GetDayTimeBySec(TimeInfo.Instance.ServerNow(), GlobalCfgArray.globalCfg.refreshTime);
  105. // Debug.Log("noticeTime:" + noticeTime + " currentTime:" + currentTime);
  106. if ((currentTime - noticeTime) / 1000 / TimeUtil.SECOND_PER_DAY < 3)
  107. {
  108. ViewManager.Show<SystemNoticeView>(new object[] { NoticeDataManager.Instance.LastNoticeInfo.title, NoticeDataManager.Instance.LastNoticeInfo.content });
  109. }
  110. }
  111. else
  112. {
  113. ErrorCodeController.Handler(result);
  114. }
  115. return;
  116. }
  117. private static void OnLoginFail(int errorCode)
  118. {
  119. ViewManager.Hide<ModalStatusView>();
  120. if (ErrorCodeController.Handler(errorCode))
  121. {
  122. GameController.QuitToLoginView(true);
  123. }
  124. EventAgent.DispatchEvent(ConstMessage.LOGIN_FAIL);
  125. }
  126. public static async ETTask Register(string account, string password, string name, string identityNum, string code)
  127. {
  128. ViewManager.Show<ModalStatusView>("注册中...");
  129. int errorCode = await LoginHelper.Register(GameGlobal.zoneScene, GameConfig.LoginAddress, account, password, name, identityNum, code);
  130. if (errorCode == ErrorCode.ERR_Success)
  131. {
  132. Login(account, password).Coroutine();
  133. }
  134. else
  135. {
  136. ViewManager.Hide<ModalStatusView>();
  137. ErrorCodeController.Handler(errorCode);
  138. }
  139. }
  140. public static async ETTask GetServerInfos()
  141. {
  142. ViewManager.Show<ModalStatusView>("获取服务器列表...");
  143. int errorCode = await LoginHelper.GetServerInfos(GameGlobal.zoneScene);
  144. if (errorCode != ErrorCode.ERR_Success)
  145. {
  146. ViewManager.Hide<ModalStatusView>();
  147. ErrorCodeController.Handler(errorCode);
  148. await ETTask.Create();
  149. return;
  150. }
  151. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  152. if (serverInfosComponent.ServerInfoList.Count <= 0)
  153. {
  154. AlertSystem.Show("服务器列表为空:\n请检查网络和服务器状态")
  155. .SetRightButton(true, "知道啦", (object data) =>
  156. {
  157. Application.Quit();
  158. });
  159. await ETTask.Create();
  160. return;
  161. }
  162. if (serverInfosComponent.CurrentServerId <= 0)
  163. {
  164. var serverInfo = serverInfosComponent.ServerInfoList[0];
  165. serverInfosComponent.CurrentServerId = int.Parse(serverInfo.Id.ToString());
  166. }
  167. }
  168. public static async ETTask GetRoles()
  169. {
  170. GameGlobal.OpenServerTime = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>().GetCurrentServerInfo().OpenServerTime;
  171. ViewManager.Show<ModalStatusView>("获取角色信息...");
  172. int errorCode = await LoginHelper.GetRoles(GameGlobal.zoneScene);
  173. if (errorCode != ErrorCode.ERR_Success)
  174. {
  175. ErrorCodeController.Handler(errorCode);
  176. await ETTask.Create();
  177. }
  178. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  179. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  180. {
  181. var roleInfo = roleInfosComponent.RoleInfos[0];
  182. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  183. await ReqEnterGame();
  184. }
  185. else
  186. {
  187. GameGlobal.isFirstEntry = true;
  188. ViewManager.Hide<ModalStatusView>();
  189. //GameController.ShowCreateRole();不显示创角界面,在剧情中改名
  190. await ReqCreateRole();
  191. }
  192. }
  193. public static async ETTask<string> ReqRandomRoleName()
  194. {
  195. (int result, string name) = await LoginHelper.ReqRandomRoleName();
  196. if (result == ErrorCode.ERR_Success)
  197. {
  198. return name;
  199. }
  200. ErrorCodeController.Handler(result);
  201. return "";
  202. }
  203. public static async ETTask ReqCreateRole(string roleName = null)
  204. {
  205. if(string.IsNullOrEmpty(roleName))
  206. {
  207. roleName = await ReqRandomRoleName();
  208. }
  209. ViewManager.Show<ModalStatusView>("创建角色中...");
  210. int errorCode = await ET.LoginHelper.CreateRole(GameGlobal.zoneScene, roleName);
  211. if (errorCode != ErrorCode.ERR_Success)
  212. {
  213. ViewManager.Hide<ModalStatusView>();
  214. ErrorCodeController.Handler(errorCode);
  215. return;
  216. }
  217. ViewManager.Hide<CreateRoleView>();
  218. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  219. var roleInfo = roleInfosComponent.RoleInfos[0];
  220. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  221. QDManager.OnCreateRole();
  222. await ReqEnterGame();
  223. }
  224. public static async ETTask ReqEnterGame()
  225. {
  226. ViewManager.Show<ModalStatusView>("请求进入游戏...");
  227. int errorCode = await LoginHelper.GetRealmKey(GameGlobal.zoneScene);
  228. if (errorCode != ErrorCode.ERR_Success)
  229. {
  230. ViewManager.Hide<ModalStatusView>();
  231. ErrorCodeController.Handler(errorCode);
  232. return;
  233. }
  234. ViewManager.Hide<ModalStatusView>();
  235. ViewManager.Hide<LoginView>();
  236. ViewManager.Show<LoadingView>();
  237. LoadingView.Instance.SetProgress(99);
  238. LoadingView.Instance.SetDesc("正在加载数据...");
  239. LogServerHelper.SendNodeLog((int)LogNode.StartEnterGame);
  240. errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  241. if (errorCode != ErrorCode.ERR_Success)
  242. {
  243. ErrorCodeController.Handler(errorCode);
  244. return;
  245. }
  246. await GameController.PreEnterGameAsync();
  247. LogServerHelper.SendNodeLog((int)LogNode.OnEnterGame);
  248. }
  249. public static async ETTask ReqReConnectGate()
  250. {
  251. ViewManager.Show<ModalStatusView>("重新连接中...");
  252. int errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  253. if (errorCode != ErrorCode.ERR_Success)
  254. {
  255. if (errorCode != ErrorCode.ERR_SessionStateError && errorCode != ErrorCode.ERR_NetWorkError)
  256. {
  257. Log.Error($"Reconnect fail, errorCode {errorCode}!");
  258. }
  259. AlertSystem.Show("重新连接失败,游戏或已更新,请重新进入游戏获得最佳体验。")
  260. .SetRightButton(true, "好的", (object data) =>
  261. {
  262. Application.Quit();
  263. });
  264. return;
  265. }
  266. EventAgent.DispatchEvent(ConstMessage.NUMERIC_CHANGE, NumericType.All);
  267. GameController.OnReconnected();
  268. ViewManager.Hide<ModalStatusView>();
  269. }
  270. }
  271. }