LoginMgr.lua 30 KB

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