gameWorldController.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. local GameWorldController = class("GameWorldController", function ()
  2. return cc.Layer:create()
  3. end)
  4. local SCENE_GRAPH_FILE = "scene/GameWorld.csb"
  5. local CHECK_GAME_ACTIVE_TIME = 1800
  6. function GameWorldController:ctor()
  7. self.mSpeedCheatCount = 0
  8. self.mElapsedT = 0
  9. self.mLastElapsedT = 0
  10. self.mLoopCount = 0
  11. self.mHeartBeatT = os.time()
  12. self.mActiveT = os.time()
  13. self.mUnActiveState = false
  14. self._restart = false
  15. self._leaveWorld = false
  16. self:RegisterMsgHandler()
  17. end
  18. function GameWorldController:destory()
  19. end
  20. function GameWorldController.create()
  21. local layer = GameWorldController.new()
  22. if layer:Init() then
  23. return layer
  24. else
  25. return nil
  26. end
  27. end
  28. -----------------------------------------------------------------------------
  29. function GameWorldController:Init()
  30. --********* Mis
  31. self:scheduleUpdateWithPriorityLua(handler(self, self.update), 0)
  32. return true
  33. end
  34. -----------------------------------------------------------------------------
  35. function GameWorldController:handleMessage(msg)
  36. -- Message 885, anti SPEED cheat
  37. local hdr = msg:GetHeader()
  38. local dataSourceSize = msg:GetDataLength()
  39. local dataSource = msg:GetData()
  40. local sliceStr = dataSource:ReadString( dataSourceSize )
  41. local jsonData = string.split(sliceStr , "/")
  42. local localElapsedSinceLastMsg = self.mElapsedT - self.mLastElapsedT
  43. local serverElapsedSinceLastMsg = tonumber( jsonData[3] )
  44. local serverCurrTime = tonumber( jsonData[2] )
  45. local timeDiff = localElapsedSinceLastMsg - serverElapsedSinceLastMsg;
  46. --云真机启动
  47. if global.L_GameEnvManager:GetEnvDataByKey("isCloudLogin") then
  48. return
  49. end
  50. if self.mElapsedT == self.mLastElapsedT and self.mLastElapsedT > 0 then -- 885 message may be accumulated ( e.g. app process be suspend by onStop/onResume ) -- Make sure it is not initial state.
  51. self.mSpeedCheatCount = 0
  52. return 1
  53. end
  54. if self.s_isRoot and timeDiff >= hdr.param2 and 0 < self.mElapsedT and 1 == hdr.param1 and self.mLoopCount < 200 then
  55. self.mSpeedCheatCount = self.mSpeedCheatCount + 1
  56. if hdr.param3 <= self.mSpeedCheatCount then
  57. if not global.isDebugMode then
  58. -- issue, can not restart in handle msg
  59. self._restart = true
  60. else
  61. print( "Speed cheat has detected" )
  62. end
  63. end
  64. else
  65. -- cold down
  66. self.mSpeedCheatCount = ( 0 >= self.mSpeedCheatCount ) and 0 or (self.mSpeedCheatCount - 1)
  67. end
  68. -- Sync time line
  69. self.mElapsedT = serverCurrTime
  70. self.mLastElapsedT = self.mElapsedT
  71. self.mLoopCount = 0
  72. return 1
  73. end
  74. function GameWorldController:RespGameWorldInfoInit(msg)
  75. global.Facade:sendNotification(global.NoticeTable.GameWorldInfoInitBegin)
  76. -- 开始初始化
  77. local jsonData = ParseRawMsgToJson( msg )
  78. local header = msg:GetHeader()
  79. local data = {
  80. jsonData = jsonData,
  81. header = header
  82. }
  83. global.Facade:sendNotification(global.NoticeTable.GameWorldInfoInit, data)
  84. local LoginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  85. LoginProxy:SyncVersion()
  86. if LoginProxy and LoginProxy.SendDeviceInfo then
  87. LoginProxy:SendDeviceInfo()
  88. end
  89. if LoginProxy and LoginProxy.SendSDKInfo then
  90. LoginProxy:SendSDKInfo()
  91. end
  92. end
  93. function GameWorldController:RegisterMsgHandler()
  94. LuaRegisterMsgHandler(885, handler( self, self.handleMessage) )
  95. -----------------------------------------------------------------------------
  96. LuaRegisterMsgHandler(global.MsgType.MSG_SC_GAME_WORLD_INFO_INIT, handler(self, self.RespGameWorldInfoInit))
  97. end
  98. -----------------------------------------------------------------------------
  99. function GameWorldController:CheckHeartBeat()
  100. local dValue = math.abs(os.time() - self.mHeartBeatT)
  101. if dValue >= 60 then
  102. self.mHeartBeatT = os.time()
  103. end
  104. if os.time() - self.mHeartBeatT < 10 then
  105. return
  106. end
  107. self.mHeartBeatT = os.time()
  108. -- 发心跳
  109. LuaSendMsg( global.MsgType.MSG_CS_HEART_BEAT_KEEP, self.mHeartBeatT )
  110. end
  111. function GameWorldController:OnGameSuspend()
  112. -- 暂停前发心跳 发心跳
  113. -- 重置时间戳
  114. self.mHeartBeatT = os.time()
  115. -- 发心跳
  116. LuaSendMsg( global.MsgType.MSG_CS_HEART_BEAT_KEEP, self.mHeartBeatT )
  117. end
  118. function GameWorldController:OnGameLeaveWorld()
  119. -- issue, can not leaveWorld in handle msg
  120. self._leaveWorld = true
  121. end
  122. -----------------------
  123. function GameWorldController:GetFilterMsgTime()
  124. if not self._filterMsgTime then
  125. local envProxy = global.Facade:retrieveProxy(global.ProxyTable.GameEnvironment)
  126. self._filterMsgTime = envProxy:GetCustomDataByKey("filterMsgTime") or CHECK_GAME_ACTIVE_TIME
  127. end
  128. return self._filterMsgTime
  129. end
  130. function GameWorldController:CheckGameUnActive()
  131. local dT = os.time() - self.mActiveT
  132. if not self.mUnActiveState and dT > self:GetFilterMsgTime() then
  133. local loginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  134. loginProxy:SyncVersion(true)
  135. self.mUnActiveState = true
  136. end
  137. end
  138. function GameWorldController:OnGameActive()
  139. self.mActiveT = os.time()
  140. if self.mUnActiveState then
  141. local loginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  142. loginProxy:SyncVersion(false)
  143. self.mUnActiveState = false
  144. end
  145. end
  146. function GameWorldController:update(delta)
  147. local dI = math.floor( delta * 1000.0 )
  148. self.mElapsedT = self.mElapsedT + dI
  149. self.mLoopCount = self.mLoopCount + 1
  150. if self._restart then
  151. self._restart = false
  152. global.L_NativeBridgeManager:GN_accountLogout()
  153. global.Facade:sendNotification(global.NoticeTable.RestartGame)
  154. return
  155. end
  156. if self._leaveWorld then
  157. self._leaveWorld = false
  158. global.Facade:sendNotification(global.NoticeTable.LeaveWorld)
  159. return
  160. end
  161. -----------------------------------------------------------------------------
  162. -- 网络收发消息包
  163. global.gameEnvironment:GetNetClient():Tick()
  164. -----------------------------------------------------------------------------
  165. -- drop item
  166. global.dropItemController:Tick( delta )
  167. -----------------------------------------------------------------------------
  168. -- 技能CD
  169. global.CalcSkillCDMediator:Tick( delta )
  170. -- 连击技能CD
  171. global.CalcComboSkillCDMediator:Tick(delta)
  172. -- 角色
  173. global.actorManager:Tick( delta )
  174. -- 场景
  175. global.sceneManager:Tick(delta)
  176. --
  177. global.gameMapController:Tick( delta )
  178. -- actor refresh
  179. global.actorRefreshController:Tick(delta)
  180. -----------------------------------------------------------------------------
  181. -- 技能
  182. global.skillManager:Tick( delta )
  183. -- buff
  184. global.BuffManager:Tick(delta)
  185. -- effect
  186. global.ActorEffectManager:Tick(delta)
  187. -----------------------------------------------------------------------------
  188. -- sequence animation manager
  189. global.FrameAnimManager:Tick( delta )
  190. -- downloader
  191. global.ResDownloader:Tick( delta )
  192. -- heartBeat
  193. self:CheckHeartBeat()
  194. -- active
  195. self:CheckGameUnActive()
  196. end
  197. return GameWorldController