LoginMgr.lua 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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.sdkInited = Enum.ParamState.None
  321. ManagerContainer.LuaGameMgr:SdkInit()
  322. end
  323. end
  324. function LoginMgr:SdkInitFail()
  325. self.preLoginStatus = self.loginStatus
  326. self.loginStatus = LoginStatus.Error
  327. local data = {"LoginTips2", nil, nil, self, self.InternalRelogin, self.QuitGame}
  328. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  329. end
  330. function LoginMgr:StartSdkLogin()
  331. if ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  332. self:StartCheckSpecialInfo()
  333. else
  334. self.preLoginStatus = self.loginStatus
  335. self.loginStatus = LoginStatus.SdkLogin
  336. self.stayTime = 5
  337. ManagerContainer.LuaGameMgr.sdkLogined = Enum.ParamState.None
  338. ManagerContainer.LuaGameMgr:SdkLogin()
  339. end
  340. end
  341. function LoginMgr:SdkLoginFail()
  342. self.preLoginStatus = self.loginStatus
  343. self.loginStatus = LoginStatus.Error
  344. local data = {"LoginTips3", nil, nil, self, self.InternalRelogin, self.QuitGame}
  345. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  346. end
  347. function LoginMgr:StartCheckSpecialInfo()
  348. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  349. self.preLoginStatus = self.loginStatus
  350. self.loginStatus = LoginStatus.CheckSpecialInfo
  351. self.stayTime = 5
  352. if self.checkWWW then
  353. self.checkWWW:Dispose()
  354. end
  355. local url = PlatformPack.SPECIAL_INFO_URL .. '?timesamp=' .. tostring(os.time())
  356. local platform = ManagerContainer.LuaGameMgr.platform
  357. if platform and platform ~= '' then
  358. url = url .. '&platform=' .. tostring(platform)
  359. end
  360. local subplatform = ManagerContainer.LuaGameMgr.channelName
  361. if subplatform and subplatform ~= '' then
  362. url = url .. '&sub_platform=' .. tostring(subplatform)
  363. end
  364. self.checkWWW = UnityEngine.WWW(url)
  365. end
  366. function LoginMgr:CheckSpecialInfoSuccess()
  367. if not self.checkWWW then
  368. self:CheckSpecialInfoFail()
  369. return
  370. end
  371. if self.checkWWW.error and self.checkWWW.error ~= '' then
  372. self:CheckSpecialInfoFail()
  373. return
  374. end
  375. local ok, result = RO_XPCALL(JSON.decode, debug.traceback, JSON, self.checkWWW.text)
  376. if ok then
  377. self.agreement = result.agreement
  378. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SPECIAL_INFO_CHANGED)
  379. self:StartCheckServer()
  380. else
  381. LogError("[wboy] CheckSpecialInfoFail " .. tostring(self.checkWWW.text))
  382. self:CheckSpecialInfoFail()
  383. end
  384. end
  385. function LoginMgr:CheckSpecialInfoFail()
  386. if self.checkWWW then
  387. self.checkWWW:Dispose()
  388. end
  389. self.checkWWW = nil
  390. self:StartCheckServer()
  391. end
  392. function generateDeviceId()
  393. local seed = tostring({}):sub(7)
  394. math.randomseed(seed)
  395. math.random(); math.random(); math.random()
  396. local hexChars = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
  397. local deviceId = ""
  398. for i=1,8 do
  399. deviceId = deviceId .. hexChars[math.random(16)]
  400. end
  401. for i=1,4 do
  402. deviceId = deviceId .. "-" .. hexChars[math.random(16)] .. hexChars[math.random(16)] .. hexChars[math.random(16)] .. hexChars[math.random(16)]
  403. end
  404. return deviceId
  405. end
  406. function LoginMgr:StartCheckServer(isAgain)
  407. ManagerContainer.LuaBattleMgr:ResetBattleMode()
  408. self.checkServerAgain = isAgain
  409. local openId = ManagerContainer.LuaGameMgr.openId --"8bb8f18b-aeef-1b79-db12-a825"--generateDeviceId()--
  410. --LogError("OpenID = "..openId);
  411. if not openId or openId == '' then
  412. openId = UnityEngine.PlayerPrefs.GetString("LoginName")
  413. end
  414. if not openId or openId == '' then
  415. openId = ManagerContainer.LuaGameMgr.DeviceId
  416. end
  417. ManagerContainer.LuaGameMgr.openId = openId
  418. if Constant.SERVER_FORCE_INFO then
  419. self:SetServerInfo(Constant.SERVER_FORCE_INFO)
  420. if isAgain then
  421. self:StartCheckCanEnterServerState()
  422. else
  423. self:StartWaitClickEnter()
  424. end
  425. return
  426. end
  427. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  428. self.stayTime = 5
  429. local serverId = ManagerContainer.LuaGameMgr.serverData.id
  430. --LogWarning('serverId-----'..serverId)
  431. if(serverId == nil and self.UseLoacdServerIDState ~= UseLoacdServerIDState.Error)then
  432. serverId = ManagerContainer.LuaGameMgr:GetServerID()
  433. --serverId = 3;
  434. --LogWarning('serverId-----'..serverId)
  435. self.UseLoacdServerIDState = UseLoacdServerIDState.Used;
  436. end
  437. local url = PlatformPack.SERVERLIST_URL
  438. if serverId and serverId > 0 then
  439. url = url .. "?type=21&id=" .. tostring(serverId)
  440. else
  441. url = url .. "?"
  442. end
  443. url = ManagerContainer.LuaGameMgr:ComposeCommonServerInfo(url)
  444. if self.checkWWW then
  445. self.checkWWW:Dispose()
  446. end
  447. LogWarning('checkWWW-----'..url)
  448. self.preLoginStatus = self.loginStatus
  449. self.loginStatus = LoginStatus.CheckServer
  450. self.checkWWW = UnityEngine.WWW(url)
  451. end
  452. function LoginMgr:CheckServerFail()
  453. self.preLoginStatus = self.loginStatus
  454. self.loginStatus = LoginStatus.Error
  455. if self.checkWWW then
  456. self.checkWWW:Dispose()
  457. end
  458. self.checkWWW = nil
  459. self.checkServerAgain = nil
  460. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  461. local data = {"LoginTips4", nil, nil, self, self.InternalRelogin, self.QuitGame}
  462. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  463. ManagerContainer.LuaGameMgr:ClearServerData()
  464. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  465. end
  466. function LoginMgr:CheckServerSuccess()
  467. if not self.checkWWW then
  468. self:CheckServerFail()
  469. return
  470. end
  471. if self.checkWWW.error and self.checkWWW.error ~= '' then
  472. self:CheckServerFail()
  473. return
  474. end
  475. local ok, result = RO_XPCALL(JSON.decode, debug.traceback, JSON, self.checkWWW.text)
  476. --LogError("[wboy] CheckServer " .. tostring(self.checkWWW.text))
  477. if ok then
  478. self:SetServerInfo(result)
  479. self.checkWWW:Dispose()
  480. self.checkWWW = nil
  481. if result.err and self.UseLoacdServerIDState == UseLoacdServerIDState.Used then
  482. self.UseLoacdServerIDState = UseLoacdServerIDState.Error
  483. ManagerContainer.LuaGameMgr:SetServerID(-1)
  484. elseif self.UseLoacdServerIDState == UseLoacdServerIDState.Used or self.UseLoacdServerIDState == UseLoacdServerIDState.Error then
  485. self.UseLoacdServerIDState = UseLoacdServerIDState.Success
  486. end
  487. if self.checkServerAgain then
  488. self.checkServerAgain = nil
  489. self:StartCheckCanEnterServerState()
  490. else
  491. self:StartWaitClickEnter()
  492. end
  493. else
  494. LogError("[wboy] CheckServerFail " .. tostring(self.checkWWW.text))
  495. self:CheckServerFail()
  496. end
  497. end
  498. function LoginMgr:SetServerInfo(server)
  499. ManagerContainer.LuaGameMgr:InitServerData(server.ServerId, server.ServerName, server.Ip, server.Port, server.State)
  500. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  501. end
  502. function LoginMgr:StartWaitClickEnter()
  503. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  504. self.preLoginStatus = self.loginStatus
  505. self.loginStatus = LoginStatus.WaitClickEnter
  506. self.waitClicked = nil
  507. local serverData = ManagerContainer.LuaGameMgr.serverData
  508. if serverData and serverData.id and serverData.id > 0 and self.showNotify then
  509. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINotifyTips)
  510. self.showNotify = false
  511. end
  512. end
  513. function LoginMgr:EndWaitClickEnter()
  514. if ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  515. if self:IsAgreement() then
  516. local isOn = ManagerContainer.PlayerPrefsMgr:GetGlobalBoolean(Constant.AGREEMENT_RECORD_KEY, false)
  517. if not isOn then
  518. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('GameLogin19')
  519. self.preLoginStatus = self.loginStatus
  520. self.loginStatus = LoginStatus.WaitClickEnter
  521. self.waitClicked = nil
  522. return
  523. end
  524. end
  525. self:StartUpdateCheck()
  526. else
  527. self:InternalRelogin()
  528. end
  529. end
  530. function LoginMgr:StartUpdateCheck()
  531. self.preLoginStatus = self.loginStatus
  532. self.loginStatus = LoginStatus.UpdateCheck
  533. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  534. local leBianSDK = nil --Wenting.Lebian.LeBianSDK.instance
  535. if leBianSDK then
  536. if not self.endUpdateCheckCB then
  537. self.endUpdateCheckCB = System.Action_Wenting_Lebian_QueryUpdateErrorCode(self.EndUpdateCheck, self)
  538. end
  539. leBianSDK.queryUpdateCallback = leBianSDK.queryUpdateCallback + self.endUpdateCheckCB
  540. leBianSDK:QueryUpdate()
  541. else
  542. self:EndUpdateCheck(nil)
  543. --self:EndUpdateCheck(Wenting.Lebian.QueryUpdateErrorCode.Error)
  544. end
  545. end
  546. function LoginMgr:EndUpdateCheck(resultCode)
  547. local leBianSDK = nil -- Wenting.Lebian.LeBianSDK.instance
  548. if leBianSDK then
  549. if self.endUpdateCheckCB then
  550. leBianSDK.queryUpdateCallback = leBianSDK.queryUpdateCallback - self.endUpdateCheckCB
  551. self.endUpdateCheckCB = nil
  552. end
  553. local QueryUpdateErrorCode = Wenting.Lebian.QueryUpdateErrorCode
  554. if resultCode == QueryUpdateErrorCode.NoUpdate then
  555. self:StartCheckServer(true)
  556. return
  557. elseif resultCode == QueryUpdateErrorCode.Update then
  558. self:StartCheckServer(true)
  559. return
  560. elseif resultCode == QueryUpdateErrorCode.ForceUpdate then
  561. -- 停止后续流程
  562. self.preLoginStatus = self.loginStatus
  563. self.loginStatus = LoginStatus.Error
  564. return
  565. end
  566. end
  567. -- self:ResetClickState()
  568. --self:ErrorNoticeDisplay('GameLogin20')
  569. self:StartCheckServer(true)
  570. end
  571. function LoginMgr:StartCheckCanEnterServerState()
  572. if not SDKMgr.Instance:HasCanEnterServerJudge() then
  573. self:StartConnectGate()
  574. return
  575. end
  576. self.preLoginStatus = self.loginStatus
  577. self.loginStatus = LoginStatus.CanEnterServerCheck
  578. ManagerContainer.LuaGameMgr.canEnterServerState = Enum.ParamState.None
  579. local serverData = ManagerContainer.LuaGameMgr.serverData
  580. SDKMgr.Instance:CanEnterServerJudge(serverData.id, serverData.name)
  581. end
  582. function LoginMgr:CheckCanEnterServerStateSuccess()
  583. self:StartConnectGate()
  584. end
  585. function LoginMgr:CheckCanEnterServerStateFail()
  586. self:ResetClickState()
  587. end
  588. function LoginMgr:StartConnectGate()
  589. local serverData = ManagerContainer.LuaGameMgr.serverData
  590. if serverData.state == Enum.ServerState.Maintain then
  591. self:StartMaintain()
  592. return
  593. elseif serverData.state == Enum.ServerState.Full then
  594. self:ConnectGateFail()
  595. return
  596. end
  597. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  598. self.preLoginStatus = self.loginStatus
  599. self.loginStatus = LoginStatus.ConnectGate
  600. ManagerContainer.NetManager.connectGated = Enum.ParamState.None
  601. self.logined = nil
  602. self.loginErrorCode = nil
  603. local serverPorts = serverData.ports
  604. local len = serverPorts and #serverPorts or 0
  605. if len <= 0 then
  606. self:ConnectGateFail()
  607. elseif len <= 1 then
  608. serverData.portidx = 1
  609. ManagerContainer.NetManager:ConnectGate(serverData.ip, serverPorts[1])
  610. else
  611. math.randomseed(os.time())
  612. local serverPortIdx = math.random(len)
  613. serverData.portidx = serverPortIdx
  614. self.serverPorts = clone(serverData.ports)
  615. ManagerContainer.NetManager:ConnectGate(serverData.ip, serverPorts[serverPortIdx])
  616. end
  617. end
  618. function LoginMgr:ReConnectGate()
  619. ManagerContainer.NetManager.connectGated = Enum.ParamState.None
  620. self.logined = nil
  621. self.loginErrorCode = nil
  622. local serverData = ManagerContainer.LuaGameMgr.serverData
  623. if serverData.state == Enum.ServerState.Maintain then
  624. self:StartMaintain()
  625. return
  626. elseif serverData.state == Enum.ServerState.Full then
  627. self:ConnectGateFail()
  628. return
  629. end
  630. local len = self.serverPorts and #self.serverPorts or 0
  631. if len <= 0 then
  632. self:ConnectGateFail()
  633. else
  634. local serverPortIdx = serverData.portidx
  635. if serverPortIdx then
  636. table.remove(self.serverPorts, serverPortIdx)
  637. end
  638. len = #self.serverPorts
  639. if len <= 0 then
  640. self:ConnectGateFail()
  641. else
  642. serverPortIdx = math.random(len)
  643. serverData.portidx = serverPortIdx
  644. ManagerContainer.NetManager:ConnectGate(serverData.ip, self.serverPorts[serverPortIdx])
  645. end
  646. end
  647. end
  648. function LoginMgr:ConnectGateFail()
  649. self:ResetClickState()
  650. self:ErrorNoticeDisplay('GameLogin21')
  651. end
  652. function LoginMgr:StartMaintain()
  653. self:ResetClickState()
  654. self:ErrorNoticeDisplay('GameLogin11')
  655. end
  656. function LoginMgr:StartLoginGame()
  657. self.preLoginStatus = self.loginStatus
  658. self.loginStatus = LoginStatus.GameLogin
  659. self.stayTime = 10
  660. end
  661. function LoginMgr:LoginGameFail(error,...)
  662. self:ResetClickState()
  663. if not error then
  664. error = 'GameLogin14'
  665. end
  666. self:ErrorNoticeDisplay(error, ...)
  667. end
  668. function LoginMgr:StartEnterGame()
  669. local loginErrorCode = self.loginErrorCode
  670. if loginErrorCode == Enum.NetErrorCode.ERROR_OK then
  671. self.preLoginStatus = self.loginStatus
  672. self.loginStatus = LoginStatus.EnterGame
  673. ManagerContainer.LuaUIMgr:CloseAllPagesExceptId(Enum.UIPageName.UILanuch)
  674. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_Loading_Begin)
  675. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND then
  676. self.preLoginStatus = self.loginStatus
  677. self.loginStatus = LoginStatus.EnterGame
  678. ManagerContainer.LuaGameMgr:EnterCreateRole(false)
  679. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND_NEED_ACTIVE_CODE then
  680. self.preLoginStatus = self.loginStatus
  681. self.loginStatus = LoginStatus.EnterGame
  682. ManagerContainer.LuaGameMgr:EnterCreateRole(true)
  683. elseif loginErrorCode == Enum.NetErrorCode.ERROR_SDK_ACCOUNT_INVALID then
  684. self.preLoginStatus = self.loginStatus
  685. self.loginStatus = LoginStatus.Error
  686. ManagerContainer.NetManager:ResetNetMgr()
  687. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  688. local data = {"LoginTips9", nil, true, self, self.ReLogin, self.QuitGame}
  689. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  690. elseif loginErrorCode == Enum.NetErrorCode.ERROR_GAME_VERSION then
  691. self.preLoginStatus = self.loginStatus
  692. self.loginStatus = LoginStatus.Error
  693. ManagerContainer.NetManager:ResetNetMgr()
  694. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  695. local data = {"LoginTips5", nil, nil, self, self.InternalRelogin, self.QuitGame}
  696. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  697. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_BANED then
  698. self:GameBanedError()
  699. elseif loginErrorCode == Enum.NetErrorCode.ERROR_ROLE_BANED_TYPE_PERSONALCHAT then
  700. self:GameBanedError()
  701. else
  702. local langStr = string.formatbykey(loginErrorCode)
  703. if not langStr or langStr == '' then
  704. self:LoginGameFail(loginErrorCode)
  705. else
  706. self:ResetClickState()
  707. end
  708. end
  709. end
  710. function LoginMgr:GameBanedError()
  711. self.preLoginStatus = self.loginStatus
  712. self.loginStatus = LoginStatus.Error
  713. end
  714. function LoginMgr:ResetClickState()
  715. self.preLoginStatus = self.loginStatus
  716. self.loginStatus = LoginStatus.Error
  717. ManagerContainer.NetManager:ResetNetMgr()
  718. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  719. self:StartWaitClickEnter()
  720. end
  721. function LoginMgr:IsLoginWaiting()
  722. return (not self.loginStatus or self.loginStatus == LoginStatus.WaitClickEnter)
  723. end
  724. function LoginMgr:NetWorkError()
  725. self.preLoginStatus = self.loginStatus
  726. self.loginStatus = LoginStatus.Error
  727. ManagerContainer.NetManager:ResetNetMgr()
  728. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  729. local data = {"LoginTips1", nil, nil, self, self.InternalRelogin, self.QuitGame}
  730. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UISystemNoticeTips, data, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 10)
  731. end
  732. function LoginMgr:ErrorNoticeDisplay(error, ...)
  733. if not ManagerContainer.LuaUIMgr:GetPage(Enum.UIPageName.UIErrorTips) then
  734. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIErrorTips, {errorId=error,params = {...}}, nil, nil, nil, Enum.UISibling[Enum.UIType.Top + 1] + 11)
  735. else
  736. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.ERROR_DESC_DISPLAY, {errorId=error,params = {...}})
  737. end
  738. end
  739. function LoginMgr:InternalRelogin()
  740. self.preLoginStatus = self.loginStatus
  741. self.loginStatus = LoginStatus.InternalRelogin
  742. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  743. ManagerContainer.LuaGameMgr:ClearServerData()
  744. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CURRENT_SERVER_CHANGED)
  745. end
  746. function LoginMgr:ReLogin(logout)
  747. if logout and ManagerContainer.LuaGameMgr.sdkLogined == Enum.ParamState.Success then
  748. ManagerContainer.LuaGameMgr:SdkLogout()
  749. else
  750. ManagerContainer.LuaGameMgr:ClearServerData()
  751. ManagerContainer.LuaGameMgr:ReLogin()
  752. end
  753. end
  754. function LoginMgr:QuitGame()
  755. ManagerContainer.LuaGameMgr:QuitGame(true)
  756. return true
  757. end
  758. function LoginMgr:IsAgreement()
  759. return self.agreement and self.agreement ~= '' or false
  760. end
  761. function LoginMgr:GetAgreement()
  762. return self.agreement
  763. end
  764. return LoginMgr