LoginMgr.lua 32 KB

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