LoginMgr.lua 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. local LoginMgr = class('LoginMgr')
  2. local LoginStatus = {
  3. Error = 0,
  4. Clear = 1,
  5. OpenLoading = 2,
  6. OpenLoaded = 3,
  7. SdkInit = 4,
  8. SdkLogin = 5,
  9. CheckSpecialInfo = 6,
  10. CheckServer = 7,
  11. WaitClickEnter = 8,
  12. UpdateCheck = 9,
  13. CanEnterServerCheck = 10,
  14. ConnectGate = 11,
  15. GameLogin = 12,
  16. EnterGame = 13,
  17. InternalRelogin = 20,
  18. }
  19. local UseLoacdServerIDState =
  20. {
  21. None = -1, --沒有用
  22. Error = 0, --用了錯誤的ID
  23. Used = 1, -- 用了
  24. Success = 2,
  25. }
  26. function LoginMgr:ctor()
  27. self:RegisterNetEvents()
  28. self.UseLoacdServerIDState = UseLoacdServerIDState.None
  29. end
  30. function LoginMgr:Clear()
  31. self:Exit()
  32. end
  33. function LoginMgr:Destroy()
  34. self:Clear()
  35. self:UnRegisterNetEvents()
  36. end
  37. function LoginMgr:RegisterNetEvents()
  38. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_LOGIN_ACK, self.OnLoginAck, self)
  39. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_LOGOUT_NTF, self.OnLogoutNtf,self)
  40. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_KICK_OUT_NTF, self.OnKickoutNtf,self)
  41. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_RECONNECT_ACK, self.OnReconnectAck,self)
  42. end
  43. function LoginMgr:UnRegisterNetEvents()
  44. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_LOGIN_ACK)
  45. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_LOGOUT_NTF)
  46. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_KICK_OUT_NTF)
  47. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_RECONNECT_ACK)
  48. end
  49. function LoginMgr:OnLoginAck(data)
  50. self.logined = true
  51. self.loginErrorCode = data and data.error or nil
  52. if self.loginStatus == LoginStatus.GameLogin then
  53. self:StartEnterGame()
  54. end
  55. end
  56. function LoginMgr:OnLogoutNtf()
  57. ManagerContainer.LuaBattleMgr:StopSyncServerTimeTimer()
  58. ManagerContainer.NetManager:ResetNetMgr()
  59. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  60. local data = {"LoginTips8", nil, nil, self, self.ReLogin, self.QuitGame}
  61. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  62. end
  63. function LoginMgr:OnKickoutNtf(msgData)
  64. local noticeKey = 'LoginTips8'
  65. local noticeContent = nil
  66. if msgData then
  67. local remainTime = msgData.ban_end_time
  68. if remainTime and remainTime > 0 then
  69. remainTime = type(remainTime) == "number" and remainTime or #remainTime
  70. noticeContent = {DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, true)}
  71. end
  72. if msgData.error == Enum.NetErrorCode.ERROR_RELOGIN then
  73. noticeKey = 'LoginTips6'
  74. elseif msgData.error == Enum.NetErrorCode.ERROR_ROLE_BANED then
  75. if noticeContent then
  76. noticeKey = 'LoginTips10'
  77. end
  78. elseif msgData.error == Enum.NetErrorCode.ERROR_SERVER_KICK_CHEAT then
  79. if noticeContent then
  80. noticeKey = 'LoginTips10'
  81. end
  82. elseif msgData.error == Enum.NetErrorCode.ERROR_SERVER_MAINAIN then
  83. noticeKey = 'LoginTips7'
  84. elseif msgData.error == Enum.NetErrorCode.ERROR_ROLE_BANED_TYPE_PERSONALCHAT then
  85. noticeKey = 'LoginTips11'
  86. end
  87. end
  88. ManagerContainer.LuaBattleMgr:StopSyncServerTimeTimer()
  89. ManagerContainer.NetManager:ResetNetMgr()
  90. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  91. local data = {noticeKey, noticeContent, nil, self, self.ReLogin, self.QuitGame}
  92. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  93. end
  94. function LoginMgr:OnReconnectAck(msgData)
  95. if msgData then
  96. if msgData.error == Enum.NetErrorCode.ERROR_OK then
  97. ManagerContainer.LuaBattleMgr:SetReconnected(true)
  98. ManagerContainer.LuaBattleMgr:SendGetServerTimeReq()
  99. if ManagerContainer.FSMMgr:IsFightingState() then
  100. local battleMode = LuaBattleBridge.CurrentBattleMode()
  101. local battleSubMode = LuaBattleBridge.CurrentBattleSubMode()
  102. if battleMode == BattleMode.Versus
  103. or battleMode == BattleMode.Boss
  104. or battleMode == BattleMode.Time then
  105. local cmdId = nil
  106. if battleSubMode == BattleSubMode.Guild then
  107. cmdId = ProtoMsgId.CS_GUILD_BOSS_CHALLENGE_REQ
  108. elseif battleSubMode == BattleSubMode.ClimbingTower then
  109. cmdId = ProtoMsgId.CS_CLIMBING_TOWER_END_REQ
  110. elseif battleSubMode == BattleSubMode.Arena then
  111. cmdId = ProtoMsgId.CS_ARENA_RESULT_REQ
  112. elseif battleSubMode == BattleSubMode.HundredDojo then
  113. cmdId = ProtoMsgId.CS_DAO_CHANG100_CHALLENGE_RESULT_REQ
  114. end
  115. if not cmdId or not ManagerContainer.NetManager:HasCmdIdAtConfirmQueue(cmdId) then
  116. ManagerContainer.LuaGameMgr:EnterBattle(false)
  117. end
  118. end
  119. end
  120. elseif msgData.error == Enum.NetErrorCode.ERROR_ROLE_INVALID then
  121. self:ReconnectRefuse(true)
  122. else
  123. self:ReconnectRefuse()
  124. end
  125. else
  126. self:ReconnectRefuse()
  127. end
  128. end
  129. function LoginMgr:ReconnectRefuse(logout)
  130. ManagerContainer.LuaBattleMgr:StopSyncServerTimeTimer()
  131. ManagerContainer.NetManager:ResetNetMgr()
  132. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  133. local data = {logout and "LoginTips12" or 'LoginTips13', nil, logout, self, self.ReLogin, self.QuitGame}
  134. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  135. end
  136. function LoginMgr:ReconnectFail()
  137. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  138. local data = {"LoginTips1", nil, nil, self, self.TryReconnect, self.QuitGame}
  139. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  140. end
  141. function LoginMgr:TryReconnect()
  142. ManagerContainer.NetManager:StartReconnect()
  143. end
  144. ----------------------------- 以下是第一次登录或重新登录的流程 -----------------------------
  145. function LoginMgr:Enter(relogin)
  146. self:Exit()
  147. self.relogin = relogin
  148. self.preLoginStatus = nil
  149. self.loginStatus = LoginStatus.Clear
  150. self.logined = nil
  151. self.loginErrorCode = nil
  152. self.waitClicked = nil
  153. self.checkServerAgain = nil
  154. local leBianSDK = nil --Wenting.Lebian.LeBianSDK.instance
  155. if leBianSDK then
  156. local clientChId = leBianSDK:GetClientChId()
  157. if not clientChId then
  158. clientChId = ''
  159. end
  160. ManagerContainer.LuaGameMgr.channelName = clientChId
  161. end
  162. self.agreement = nil
  163. self.showNotify = true
  164. end
  165. function LoginMgr:Exit()
  166. self.relogin = nil
  167. self.stayTime = nil
  168. self.preLoginStatus = nil
  169. self.loginStatus = nil
  170. if self.checkWWW then
  171. self.checkWWW:Dispose()
  172. end
  173. self.checkWWW = nil
  174. self.logined = nil
  175. self.loginErrorCode = nil
  176. self.waitClicked = nil
  177. self.checkServerAgain = nil
  178. self.serverPorts = nil
  179. if self.endUpdateCheckCB then
  180. local leBianSDK = nil--Wenting.Lebian.LeBianSDK.instance
  181. if leBianSDK then
  182. leBianSDK.queryUpdateCallback = leBianSDK.queryUpdateCallback - self.endUpdateCheckCB
  183. self.endUpdateCheckCB = nil
  184. end
  185. end
  186. self.showNotify = nil
  187. end
  188. function LoginMgr:Update()
  189. if self.loginStatus == LoginStatus.Error then
  190. return
  191. end
  192. if self.loginStatus == LoginStatus.Clear then
  193. ManagerContainer:Reset()
  194. self:StartStartLoading()
  195. elseif ManagerContainer.LuaGameMgr.NetStatus == eNetType.NetType_None then
  196. self:NetWorkError()
  197. return
  198. elseif self.loginStatus == LoginStatus.OpenLoading then
  199. if ManagerContainer.LuaGameMgr.loadingPageOk then
  200. self:StartStartLoaded()
  201. end
  202. elseif self.loginStatus == LoginStatus.OpenLoaded then
  203. self.stayTime = self.stayTime - Time.deltaTime
  204. if self.stayTime <= 0 then
  205. self:StartSdkInit()
  206. end
  207. elseif self.loginStatus == LoginStatus.SdkInit then
  208. if ManagerContainer.LuaGameMgr.sdkInited == Enum.ParamState.Success then
  209. self:StartSdkLogin()
  210. elseif ManagerContainer.LuaGameMgr.sdkInited == Enum.ParamState.Fail then
  211. self:SdkInitFail()
  212. else
  213. self.stayTime = self.stayTime - Time.deltaTime
  214. if self.stayTime <= 0 then
  215. self:SdkInitFail()
  216. end
  217. end
  218. elseif self.loginStatus == LoginStatus.SdkLogin then
  219. if ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  220. self:StartCheckSpecialInfo()
  221. elseif ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Fail then
  222. self:SdkLoginFail()
  223. else
  224. if self.stayTime > 0 then
  225. self.stayTime = self.stayTime - Time.deltaTime
  226. if self.stayTime < 0 then
  227. self.waitClicked = nil
  228. end
  229. else
  230. if self.waitClicked then
  231. self:InternalRelogin()
  232. end
  233. end
  234. end
  235. elseif self.loginStatus == LoginStatus.CheckSpecialInfo then
  236. if self.checkWWW then
  237. if self.checkWWW.isDone then
  238. self:CheckSpecialInfoSuccess()
  239. else
  240. self.stayTime = self.stayTime - Time.deltaTime
  241. if self.stayTime <= 0 then
  242. self:CheckSpecialInfoFail()
  243. end
  244. end
  245. else
  246. self:CheckSpecialInfoFail()
  247. end
  248. elseif self.loginStatus == LoginStatus.CheckServer then
  249. if self.checkWWW then
  250. if self.checkWWW.isDone then
  251. self:CheckServerSuccess()
  252. else
  253. self.stayTime = self.stayTime - Time.deltaTime
  254. if self.stayTime <= 0 then
  255. self:CheckServerFail()
  256. end
  257. end
  258. else
  259. self:CheckServerFail()
  260. end
  261. elseif self.loginStatus == LoginStatus.WaitClickEnter then
  262. if self.waitClicked then
  263. self:EndWaitClickEnter()
  264. end
  265. elseif self.loginStatus == LoginStatus.UpdateCheck then
  266. elseif self.loginStatus == LoginStatus.CanEnterServerCheck then
  267. if ManagerContainer.LuaGameMgr.canEnterServerState == Enum.ParamState.Success then
  268. self:CheckCanEnterServerStateSuccess()
  269. elseif ManagerContainer.LuaGameMgr.canEnterServerState == Enum.ParamState.Fail then
  270. self:CheckCanEnterServerStateFail()
  271. end
  272. elseif self.loginStatus == LoginStatus.ConnectGate then
  273. if ManagerContainer.NetManager.connectGated == Enum.ParamState.Success then
  274. self:StartLoginGame()
  275. elseif ManagerContainer.NetManager.connectGated == Enum.ParamState.Fail then
  276. self:ReConnectGate()
  277. end
  278. elseif self.loginStatus == LoginStatus.GameLogin then
  279. if self.logined then
  280. self:StartEnterGame()
  281. else
  282. self.stayTime = self.stayTime - Time.deltaTime
  283. if self.stayTime <= 0 then
  284. self:LoginGameFail()
  285. end
  286. end
  287. elseif self.loginStatus == LoginStatus.InternalRelogin then
  288. self:StartStartLoading()
  289. end
  290. end
  291. function LoginMgr:GetTask()
  292. return nil
  293. end
  294. function LoginMgr:StartStartLoading()
  295. if ManagerContainer.LuaUIMgr:HasOpenPage(Enum.UIPageName.UILanuch) then
  296. self:StartStartLoaded()
  297. else
  298. self.preLoginStatus = self.loginStatus
  299. self.loginStatus = LoginStatus.OpenLoading
  300. ManagerContainer.LuaGameMgr:OpenLoading(Enum.UIPageName.UILanuch)
  301. end
  302. end
  303. function LoginMgr:StartStartLoaded()
  304. self.preLoginStatus = self.loginStatus
  305. self.loginStatus = LoginStatus.OpenLoaded
  306. self.stayTime = 0.5
  307. if self.relogin then
  308. ManagerContainer.LuaGameMgr:ReLoginClearData()
  309. end
  310. ManagerContainer.LuaUIMgr:CloseAllPagesExceptId(Enum.UIPageName.UILanuch)
  311. end
  312. function LoginMgr:StartSdkInit()
  313. if ManagerContainer.LuaGameMgr.sdkInited == Enum.ParamState.Success then
  314. self:StartSdkLogin()
  315. else
  316. SDKMgr.Instance:SetHttpCheckUrl(PlatformPack.URL_KEY)
  317. self.preLoginStatus = self.loginStatus
  318. self.loginStatus = LoginStatus.SdkInit
  319. self.stayTime = 35
  320. ManagerContainer.LuaGameMgr.isCallInit = true
  321. --LogError("===============sdk init=========".."isCallInit = "..Inspect(ManagerContainer.LuaGameMgr.isCallInit))
  322. ManagerContainer.LuaGameMgr.sdkInited = Enum.ParamState.None
  323. ManagerContainer.LuaGameMgr:SdkInit()
  324. end
  325. end
  326. function LoginMgr:SdkInitFail()
  327. self.preLoginStatus = self.loginStatus
  328. self.loginStatus = LoginStatus.Error
  329. local data = {"LoginTips2", nil, nil, self, self.InternalRelogin, self.QuitGame}
  330. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  331. end
  332. function LoginMgr:StartSdkLogin()
  333. if ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  334. self:StartCheckSpecialInfo()
  335. else
  336. self.preLoginStatus = self.loginStatus
  337. self.loginStatus = LoginStatus.SdkLogin
  338. self.stayTime = 5
  339. ManagerContainer.LuaGameMgr.sdkLogined = Enum.ParamState.None
  340. ManagerContainer.LuaGameMgr:SdkLogin()
  341. end
  342. end
  343. function LoginMgr:SdkLoginFail()
  344. self.preLoginStatus = self.loginStatus
  345. self.loginStatus = LoginStatus.Error
  346. local data = {"LoginTips3", nil, nil, self, self.InternalRelogin, self.QuitGame}
  347. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  348. end
  349. function LoginMgr:StartCheckSpecialInfo()
  350. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  351. self.preLoginStatus = self.loginStatus
  352. self.loginStatus = LoginStatus.CheckSpecialInfo
  353. self.stayTime = 5
  354. if self.checkWWW then
  355. self.checkWWW:Dispose()
  356. end
  357. local url = PlatformPack.SPECIAL_INFO_URL-- .. '?timesamp=' .. tostring(os.time())
  358. -- local platform = ManagerContainer.LuaGameMgr.platform
  359. -- if platform and platform ~= '' then
  360. -- url = url .. '&platform=' .. tostring(platform)
  361. -- end
  362. -- local subplatform = ManagerContainer.LuaGameMgr.channelName
  363. -- if subplatform and subplatform ~= '' then
  364. -- url = url .. '&sub_platform=' .. tostring(subplatform)
  365. -- end
  366. --LogError(url)
  367. --self.agreement = "我已详细阅读并同意<link=https://www.i7game.com/mobile/user/protocol.html>《用户协议》</link>和<link=https://www.i7game.com/mobile/user/privacy.html>《隐私政策》</link>"--
  368. self.checkWWW = UnityEngine.WWW(url)
  369. end
  370. function LoginMgr:CheckSpecialInfoSuccess()
  371. if not self.checkWWW then
  372. self:CheckSpecialInfoFail()
  373. return
  374. end
  375. if self.checkWWW.error and self.checkWWW.error ~= '' then
  376. self:CheckSpecialInfoFail()
  377. return
  378. end
  379. local ok, result = RO_XPCALL(JSON.decode, debug.traceback, JSON, self.checkWWW.text)
  380. if ok then
  381. self.agreement = result.agreement
  382. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SPECIAL_INFO_CHANGED)
  383. self:StartCheckServer()
  384. else
  385. LogError("[wboy] CheckSpecialInfoFail " .. tostring(self.checkWWW.text))
  386. self:CheckSpecialInfoFail()
  387. end
  388. end
  389. function LoginMgr:CheckSpecialInfoFail()
  390. if self.checkWWW then
  391. self.checkWWW:Dispose()
  392. end
  393. self.checkWWW = nil
  394. self:StartCheckServer()
  395. end
  396. function generateDeviceId()
  397. local seed = tostring({}):sub(7)
  398. math.randomseed(seed)
  399. math.random(); math.random(); math.random()
  400. local hexChars = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
  401. local deviceId = ""
  402. for i=1,8 do
  403. deviceId = deviceId .. hexChars[math.random(16)]
  404. end
  405. for i=1,4 do
  406. deviceId = deviceId .. "-" .. hexChars[math.random(16)] .. hexChars[math.random(16)] .. hexChars[math.random(16)] .. hexChars[math.random(16)]
  407. end
  408. return deviceId
  409. end
  410. function LoginMgr:StartCheckServer(isAgain)
  411. ManagerContainer.LuaBattleMgr:ResetBattleMode()
  412. self.checkServerAgain = isAgain
  413. local openId = "8bb8f18b-aeef-1b79-db12-a825"--ManagerContainer.LuaGameMgr.openId --"8bb8f18b-aeef-1b79-db12-a825"--generateDeviceId()--
  414. LogError("OpenID = "..openId);
  415. if not openId or openId == '' then
  416. openId = UnityEngine.PlayerPrefs.GetString("LoginName")
  417. end
  418. if not openId or openId == '' then
  419. openId = ManagerContainer.LuaGameMgr.DeviceId
  420. end
  421. ManagerContainer.LuaGameMgr.openId = openId
  422. if Constant.SERVER_FORCE_INFO then
  423. self:SetServerInfo(Constant.SERVER_FORCE_INFO)
  424. if isAgain then
  425. self:StartCheckCanEnterServerState()
  426. else
  427. self:StartWaitClickEnter()
  428. end
  429. return
  430. end
  431. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  432. self.stayTime = 5
  433. local serverId = ManagerContainer.LuaGameMgr.serverData.id
  434. --LogWarning('serverId-----'..serverId)
  435. if(serverId == nil and self.UseLoacdServerIDState ~= UseLoacdServerIDState.Error)then
  436. serverId = ManagerContainer.LuaGameMgr:GetServerID()
  437. --serverId = 3;
  438. --LogWarning('serverId-----'..serverId)
  439. self.UseLoacdServerIDState = UseLoacdServerIDState.Used;
  440. end
  441. local url = PlatformPack.SERVERLIST_URL
  442. if serverId and serverId > 0 then
  443. url = url .. "?type=21&id=" .. tostring(serverId)
  444. else
  445. url = url .. "?"
  446. end
  447. url = ManagerContainer.LuaGameMgr:ComposeCommonServerInfo(url)
  448. if self.checkWWW then
  449. self.checkWWW:Dispose()
  450. end
  451. --LogWarning('checkWWW-----'..url)
  452. self.preLoginStatus = self.loginStatus
  453. self.loginStatus = LoginStatus.CheckServer
  454. --url = "http://103.239.245.64:81/serverlist/test"
  455. self.checkWWW = UnityEngine.WWW(url)
  456. end
  457. function LoginMgr:CheckServerFail()
  458. self.preLoginStatus = self.loginStatus
  459. self.loginStatus = LoginStatus.Error
  460. local error = self.checkWWW.error
  461. if self.checkWWW then
  462. self.checkWWW:Dispose()
  463. end
  464. self.checkWWW = nil
  465. self.checkServerAgain = nil
  466. if error == "429 Too Many Requests" then
  467. --data = {"LoginTips14", nil, nil, self, self.InternalRelogin, self.QuitGame}
  468. ManagerContainer.LuaGameMgr:ClearServerData()
  469. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  470. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  471. self.waitNoticeTimer = ManagerContainer.LuaTimerMgr:AddLuaTimer(500, 1,function ()
  472. local seed = tostring({}):sub(7)
  473. math.randomseed(seed)
  474. math.random(); math.random(); math.random()
  475. local waitTime = math.random(2,10)
  476. local waitData ={2,"LoginGetServerListErrorString",{waitTime,1,function ()
  477. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  478. self.waitCloseNoticeTimer = ManagerContainer.LuaTimerMgr:AddLuaTimer(200, 1,function ()
  479. self:InternalRelogin()
  480. ManagerContainer.LuaTimerMgr:RemoveTimer(self.waitCloseNoticeTimer)
  481. end,nil)
  482. end}}
  483. ManagerContainer.LuaUIMgr:ShowNetWaiting_New(waitData)
  484. ManagerContainer.LuaTimerMgr:RemoveTimer(self.waitNoticeTimer)
  485. end, nil)
  486. return
  487. end
  488. --LoginTips14
  489. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  490. local data = {"LoginTips4", nil, nil, self, self.InternalRelogin, self.QuitGame}
  491. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  492. ManagerContainer.LuaGameMgr:ClearServerData()
  493. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  494. end
  495. function LoginMgr:CheckServerSuccess()
  496. if not self.checkWWW then
  497. self:CheckServerFail()
  498. return
  499. end
  500. if self.checkWWW.error and self.checkWWW.error ~= '' then
  501. LogError("[wboy] CheckServer error = " .. tostring(self.checkWWW.error))
  502. LogError("[wboy] CheckServer Text = " .. tostring(self.checkWWW.text))
  503. self:CheckServerFail()
  504. return
  505. end
  506. local ok, result = RO_XPCALL(JSON.decode, debug.traceback, JSON, self.checkWWW.text)
  507. --LogError("[wboy] CheckServer " .. tostring(self.checkWWW.text))
  508. if ok then
  509. self:SetServerInfo(result)
  510. self.checkWWW:Dispose()
  511. self.checkWWW = nil
  512. if result.err and self.UseLoacdServerIDState == UseLoacdServerIDState.Used then
  513. self.UseLoacdServerIDState = UseLoacdServerIDState.Error
  514. ManagerContainer.LuaGameMgr:SetServerID(-1)
  515. elseif self.UseLoacdServerIDState == UseLoacdServerIDState.Used or self.UseLoacdServerIDState == UseLoacdServerIDState.Error then
  516. self.UseLoacdServerIDState = UseLoacdServerIDState.Success
  517. end
  518. if self.checkServerAgain then
  519. self.checkServerAgain = nil
  520. self:StartCheckCanEnterServerState()
  521. else
  522. self:StartWaitClickEnter()
  523. end
  524. else
  525. LogError("[wboy] CheckServerFail " .. tostring(self.checkWWW.text))
  526. self:CheckServerFail()
  527. end
  528. end
  529. function LoginMgr:SetServerInfo(server)
  530. ManagerContainer.LuaGameMgr:InitServerData(server.ServerId, server.ServerName, server.Ip, server.Port, server.State)
  531. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  532. end
  533. function LoginMgr:StartWaitClickEnter()
  534. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  535. self.preLoginStatus = self.loginStatus
  536. self.loginStatus = LoginStatus.WaitClickEnter
  537. self.waitClicked = nil
  538. local serverData = ManagerContainer.LuaGameMgr.serverData
  539. if serverData and serverData.id and serverData.id > 0 and self.showNotify then
  540. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINotifyTips)
  541. self.showNotify = false
  542. end
  543. end
  544. function LoginMgr:EndWaitClickEnter()
  545. if ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  546. if self:IsAgreement() then
  547. local isOn = ManagerContainer.PlayerPrefsMgr:GetGlobalBoolean(Constant.AGREEMENT_RECORD_KEY, false)
  548. if not isOn then
  549. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('GameLogin19')
  550. self.preLoginStatus = self.loginStatus
  551. self.loginStatus = LoginStatus.WaitClickEnter
  552. self.waitClicked = nil
  553. return
  554. end
  555. end
  556. self:StartUpdateCheck()
  557. else
  558. self:InternalRelogin()
  559. end
  560. end
  561. function LoginMgr:StartUpdateCheck()
  562. self.preLoginStatus = self.loginStatus
  563. self.loginStatus = LoginStatus.UpdateCheck
  564. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  565. local leBianSDK = nil --Wenting.Lebian.LeBianSDK.instance
  566. if leBianSDK then
  567. if not self.endUpdateCheckCB then
  568. self.endUpdateCheckCB = System.Action_Wenting_Lebian_QueryUpdateErrorCode(self.EndUpdateCheck, self)
  569. end
  570. leBianSDK.queryUpdateCallback = leBianSDK.queryUpdateCallback + self.endUpdateCheckCB
  571. leBianSDK:QueryUpdate()
  572. else
  573. self:EndUpdateCheck(nil)
  574. --self:EndUpdateCheck(Wenting.Lebian.QueryUpdateErrorCode.Error)
  575. end
  576. end
  577. function LoginMgr:EndUpdateCheck(resultCode)
  578. local leBianSDK = nil -- Wenting.Lebian.LeBianSDK.instance
  579. if leBianSDK then
  580. if self.endUpdateCheckCB then
  581. leBianSDK.queryUpdateCallback = leBianSDK.queryUpdateCallback - self.endUpdateCheckCB
  582. self.endUpdateCheckCB = nil
  583. end
  584. local QueryUpdateErrorCode = Wenting.Lebian.QueryUpdateErrorCode
  585. if resultCode == QueryUpdateErrorCode.NoUpdate then
  586. self:StartCheckServer(true)
  587. return
  588. elseif resultCode == QueryUpdateErrorCode.Update then
  589. self:StartCheckServer(true)
  590. return
  591. elseif resultCode == QueryUpdateErrorCode.ForceUpdate then
  592. -- 停止后续流程
  593. self.preLoginStatus = self.loginStatus
  594. self.loginStatus = LoginStatus.Error
  595. return
  596. end
  597. end
  598. -- self:ResetClickState()
  599. --self:ErrorNoticeDisplay('GameLogin20')
  600. self:StartCheckServer(true)
  601. end
  602. function LoginMgr:StartCheckCanEnterServerState()
  603. if not SDKMgr.Instance:HasCanEnterServerJudge() then
  604. self:StartConnectGate()
  605. return
  606. end
  607. self.preLoginStatus = self.loginStatus
  608. self.loginStatus = LoginStatus.CanEnterServerCheck
  609. ManagerContainer.LuaGameMgr.canEnterServerState = Enum.ParamState.None
  610. local serverData = ManagerContainer.LuaGameMgr.serverData
  611. SDKMgr.Instance:CanEnterServerJudge(serverData.id, serverData.name)
  612. end
  613. function LoginMgr:CheckCanEnterServerStateSuccess()
  614. self:StartConnectGate()
  615. end
  616. function LoginMgr:CheckCanEnterServerStateFail()
  617. self:ResetClickState()
  618. end
  619. function LoginMgr:StartConnectGate()
  620. local serverData = ManagerContainer.LuaGameMgr.serverData
  621. if serverData.state == Enum.ServerState.Maintain then
  622. self:StartMaintain()
  623. return
  624. elseif serverData.state == Enum.ServerState.Full then
  625. self:ConnectGateFail()
  626. return
  627. end
  628. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  629. self.preLoginStatus = self.loginStatus
  630. self.loginStatus = LoginStatus.ConnectGate
  631. ManagerContainer.NetManager.connectGated = Enum.ParamState.None
  632. self.logined = nil
  633. self.loginErrorCode = nil
  634. local serverPorts = serverData.ports
  635. local len = serverPorts and #serverPorts or 0
  636. if len <= 0 then
  637. self:ConnectGateFail()
  638. elseif len <= 1 then
  639. serverData.portidx = 1
  640. ManagerContainer.NetManager:ConnectGate(serverData.ip, serverPorts[1])
  641. else
  642. math.randomseed(os.time())
  643. local serverPortIdx = math.random(len)
  644. serverData.portidx = serverPortIdx
  645. self.serverPorts = clone(serverData.ports)
  646. ManagerContainer.NetManager:ConnectGate(serverData.ip, serverPorts[serverPortIdx])
  647. end
  648. end
  649. function LoginMgr:ReConnectGate()
  650. ManagerContainer.NetManager.connectGated = Enum.ParamState.None
  651. self.logined = nil
  652. self.loginErrorCode = nil
  653. local serverData = ManagerContainer.LuaGameMgr.serverData
  654. if serverData.state == Enum.ServerState.Maintain then
  655. self:StartMaintain()
  656. return
  657. elseif serverData.state == Enum.ServerState.Full then
  658. self:ConnectGateFail()
  659. return
  660. end
  661. local len = self.serverPorts and #self.serverPorts or 0
  662. if len <= 0 then
  663. self:ConnectGateFail()
  664. else
  665. local serverPortIdx = serverData.portidx
  666. if serverPortIdx then
  667. table.remove(self.serverPorts, serverPortIdx)
  668. end
  669. len = #self.serverPorts
  670. if len <= 0 then
  671. self:ConnectGateFail()
  672. else
  673. serverPortIdx = math.random(len)
  674. serverData.portidx = serverPortIdx
  675. ManagerContainer.NetManager:ConnectGate(serverData.ip, self.serverPorts[serverPortIdx])
  676. end
  677. end
  678. end
  679. function LoginMgr:ConnectGateFail()
  680. self:ResetClickState()
  681. self:ErrorNoticeDisplay('GameLogin21')
  682. end
  683. function LoginMgr:StartMaintain()
  684. self:ResetClickState()
  685. self:ErrorNoticeDisplay('GameLogin11')
  686. end
  687. function LoginMgr:StartLoginGame()
  688. self.preLoginStatus = self.loginStatus
  689. self.loginStatus = LoginStatus.GameLogin
  690. self.stayTime = 10
  691. end
  692. function LoginMgr:LoginGameFail(error,...)
  693. self:ResetClickState()
  694. if not error then
  695. error = 'GameLogin14'
  696. end
  697. self:ErrorNoticeDisplay(error, ...)
  698. end
  699. function LoginMgr:StartEnterGame()
  700. local loginErrorCode = self.loginErrorCode
  701. if loginErrorCode == Enum.NetErrorCode.ERROR_OK then
  702. self.preLoginStatus = self.loginStatus
  703. self.loginStatus = LoginStatus.EnterGame
  704. ManagerContainer.LuaUIMgr:CloseAllPagesExceptId(Enum.UIPageName.UILanuch)
  705. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_Loading_Begin)
  706. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND then
  707. self.preLoginStatus = self.loginStatus
  708. self.loginStatus = LoginStatus.EnterGame
  709. ManagerContainer.LuaGameMgr:EnterCreateRole(false)
  710. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND_NEED_ACTIVE_CODE then
  711. self.preLoginStatus = self.loginStatus
  712. self.loginStatus = LoginStatus.EnterGame
  713. ManagerContainer.LuaGameMgr:EnterCreateRole(true)
  714. elseif loginErrorCode == Enum.NetErrorCode.ERROR_SDK_ACCOUNT_INVALID then
  715. self.preLoginStatus = self.loginStatus
  716. self.loginStatus = LoginStatus.Error
  717. ManagerContainer.NetManager:ResetNetMgr()
  718. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  719. local data = {"LoginTips9", nil, true, self, self.ReLogin, self.QuitGame}
  720. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  721. elseif loginErrorCode == Enum.NetErrorCode.ERROR_GAME_VERSION then
  722. self.preLoginStatus = self.loginStatus
  723. self.loginStatus = LoginStatus.Error
  724. ManagerContainer.NetManager:ResetNetMgr()
  725. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  726. local data = {"LoginTips5", nil, nil, self, self.InternalRelogin, self.QuitGame}
  727. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  728. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_BANED then
  729. self:GameBanedError()
  730. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_BANED_TYPE_PERSONALCHAT then
  731. self:GameBanedError()
  732. else
  733. local langStr = string.formatbykey(loginErrorCode)
  734. if not langStr or langStr == '' then
  735. self:LoginGameFail(loginErrorCode)
  736. else
  737. self:ResetClickState()
  738. end
  739. end
  740. end
  741. function LoginMgr:GameBanedError()
  742. self.preLoginStatus = self.loginStatus
  743. self.loginStatus = LoginStatus.Error
  744. end
  745. function LoginMgr:ResetClickState()
  746. self.preLoginStatus = self.loginStatus
  747. self.loginStatus = LoginStatus.Error
  748. ManagerContainer.NetManager:ResetNetMgr()
  749. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  750. self:StartWaitClickEnter()
  751. end
  752. function LoginMgr:IsLoginWaiting()
  753. return (not self.loginStatus or self.loginStatus == LoginStatus.WaitClickEnter)
  754. end
  755. function LoginMgr:NetWorkError()
  756. self.preLoginStatus = self.loginStatus
  757. self.loginStatus = LoginStatus.Error
  758. ManagerContainer.NetManager:ResetNetMgr()
  759. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  760. local data = {"LoginTips1", nil, nil, self, self.InternalRelogin, self.QuitGame}
  761. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  762. end
  763. function LoginMgr:ErrorNoticeDisplay(error, ...)
  764. if not ManagerContainer.LuaUIMgr:GetPage(Enum.UIPageName.UIErrorTips) then
  765. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIErrorTips, {errorId=error,params = {...}}, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 11)
  766. else
  767. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.ERROR_DESC_DISPLAY, {errorId=error,params = {...}})
  768. end
  769. end
  770. function LoginMgr:InternalRelogin()
  771. self.preLoginStatus = self.loginStatus
  772. self.loginStatus = LoginStatus.InternalRelogin
  773. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  774. ManagerContainer.LuaGameMgr:ClearServerData()
  775. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  776. end
  777. function LoginMgr:ReLogin(logout)
  778. if logout and ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  779. ManagerContainer.LuaGameMgr:SdkLogout()
  780. else
  781. ManagerContainer.LuaGameMgr:ClearServerData()
  782. ManagerContainer.LuaGameMgr:ReLogin()
  783. end
  784. end
  785. function LoginMgr:QuitGame()
  786. ManagerContainer.LuaGameMgr:QuitGame(false)
  787. return true
  788. end
  789. function LoginMgr:IsAgreement()
  790. return self.agreement and self.agreement ~= '' or false
  791. end
  792. function LoginMgr:GetAgreement()
  793. return self.agreement
  794. end
  795. return LoginMgr