Timer.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. now = now or 0 --当前时间
  2. d = d or 0 --真实时间的偏移值
  3. local Config = require("Config")
  4. local Log = require("common.Log")
  5. local Monitor = require("core.Monitor")
  6. local ObjHuman = require("core.ObjHuman")
  7. local JjcLogic = require("jjc.JjcLogic")
  8. local FundLogic = require("present.FundLogic")
  9. local LoginSignLogic = require("loginSign.LoginSignLogic")
  10. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  11. local MiddleHeartBeat = require("middle.MiddleHeartBeat")
  12. local MiddleConnect = require("middle.MiddleConnect")
  13. local DrillLogic = require("drill.DrillLogic")
  14. local UnionWarMiddleLogic = require("union.UnionWarMiddleLogic")
  15. local UnionWarLogic = require("union.UnionWarLogic")
  16. local Util = require("common.Util")
  17. local UnionDBLogic = require("union.UnionDBLogic")
  18. local UnionLogic = require("union.UnionLogic")
  19. local BattleLogic = require("battle.BattleLogic")
  20. local CommonDB = require("common.CommonDB")
  21. local UnionWarDBLogic = require("union.UnionWarDBLogic")
  22. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  23. local QQApi = require("platform.QQApi")
  24. local ReportOnline = require("platform.ReportOnline")
  25. local LianyuLogic = require("lianyu.LianyuLogic")
  26. local YunYingLogic = require("yunying.YunYingLogic")
  27. local FcmLogic = require("fcm.FcmLogic")
  28. local SharePlatformLogic = require("present.SharePlatformLogic")
  29. local JjcGodWarLogic = require("jjcGodWar.JjcGodWarLogic")
  30. local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
  31. local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
  32. local ValleyLogic = require("valley.ValleyLogic")
  33. local YjTreasureLogic = require("yjTreasure.YjTreasureLogic")
  34. local UnionWarLogic = require("union.UnionWarLogic")
  35. local WarReportMiddle = require("warReport.WarReportMiddle")
  36. local ZhanbuLogic = require("zhanbu.ZhanbuLogic")
  37. local BillboardLogic = require("billboard.BillboardLogic")
  38. local ChatRecord = require("chat.ChatRecord")
  39. local HeroGrowUp = require("absAct.HeroGrowUp")
  40. local AbsActLogic = require("absAct.AbsActLogic")
  41. local MoZhuMiddleLogic = require("mozhu.MoZhuMiddleLogic")
  42. local LostTempleLogic = require("lostTemple.lostTempleLogic")
  43. local MainDianLogic = require("MaiDian.MaiDianLogic")
  44. local VoucherShopLogic = require("voucher.VoucherShopLogic")
  45. local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle")
  46. local JjcNewLadderLogic = require("jjcnewladder.jjcNewLadderLogic")
  47. local CycleActivityLogic = require("yunying.CycleActivity")
  48. local commonOperate = require("core.commonOperate")
  49. local GodsAreaCS = require("godsArea.GodsAreaCS")
  50. local AreaBattleCS = require("areaBattle.AreaBattleCS")
  51. local AnotherWorldBattleCS = require("anotherWorldBattle.AnotherWorldBattleCS")
  52. register_timer = {
  53. monitor = 60000, --
  54. offlineCheck = 10000, -- 离线校验
  55. saveChar = 1000, -- 用户存db
  56. onZhengDian = 15000,
  57. humanRefresh = 10000, -- 角色心跳刷新
  58. middleHeartbeat = 3000,
  59. dayCheck = 100, -- 天检测
  60. unionOfflineCheck = 1000, -- 公会会长离线时间检查
  61. paomaHerartbeat = 10000, -- 跑马灯心跳
  62. tenSecond = 10000, -- 10秒1次通用
  63. oneMin = 60000, -- 60秒1次通用
  64. reportOnline = 300000, -- 在线时长上报心跳
  65. fcm = 60000, -- 防沉迷
  66. jjcGodWar = 2000, -- 众神之战
  67. jjcLadder = 2000, -- 天梯争霸
  68. valley = 2000, -- 荣耀峡谷
  69. later = 1000, -- 1秒1次 专门为了一些大数据量的处理延后处理用
  70. }
  71. LATER_DAYA = LATER_DAYA or {} -- {ts,func,param1, param2, param3, param4}
  72. function monitor()
  73. print(_G.collectgarbage("count"))
  74. local humanCnt = ObjHuman.getOnlineCnt()
  75. Log.write(Log.LOGID_MONITOR, _G.collectgarbage("count"), humanCnt)
  76. Monitor.output()
  77. end
  78. function tenSecond()
  79. QQApi.onTimer()
  80. end
  81. function oneMin()
  82. MiddleConnect.onTimer()
  83. ZhanbuLogic.checkRecover()
  84. AreaBattleCS.oneMin()
  85. AnotherWorldBattleCS.oneMin()
  86. end
  87. function offlineCheck()
  88. _G.collectgarbage("step", 100000)
  89. ObjHuman.humanOfflineCheck()
  90. end
  91. function saveChar()
  92. ObjHuman.saveCharDB()
  93. end
  94. function humanRefresh()
  95. ObjHuman.refresh()
  96. end
  97. function doZhengDian(hour)
  98. -- 整点的时候刷新逻辑用的内存较多 这里gc一把
  99. _G.collectgarbage("step", 100000)
  100. if hour == 0 then
  101. YjTreasureLogic.onZero()
  102. CycleActivityLogic.onZero()
  103. YunYingLogic.onZero()
  104. for _, human in pairs(ObjHuman.onlineUuid) do
  105. ObjHuman.updateDaily(human)
  106. YunYingLogic.onZeroHuman(human)
  107. DailyTaskLogic.onLogin(human)
  108. end
  109. UnionLogic.onHour()
  110. LianyuLogic.onZero()
  111. SharePlatformLogic.onZero()
  112. WarReportMiddle.onZero()
  113. BillboardLogic.onZero()
  114. ChatRecord.CHAT_RECORD_REPETITION = {}
  115. local expireTs = os.time() - 86400
  116. if DELIVER_CODE_HIS then
  117. for k, v in pairs(DELIVER_CODE_HIS) do
  118. if v < expireTs then DELIVER_CODE_HIS[k] = nil end
  119. end
  120. end
  121. if CHARGE_CODE_HIS then
  122. for k, v in pairs(CHARGE_CODE_HIS) do
  123. if v < expireTs then CHARGE_CODE_HIS[k] = nil end
  124. end
  125. end
  126. HeroGrowUp.actLoop()
  127. -- LostTempleLogic.onZero()
  128. VoucherShopLogic.VoucherShop_OnZero()
  129. MainDianLogic.MaiDian_OnTime()
  130. ServerCommerceMiddle.CommerceMiddle_OnZero()
  131. commonOperate.onZero()
  132. end
  133. GodsAreaCS.onHour(hour)
  134. AreaBattleCS.onHour(hour)
  135. AnotherWorldBattleCS.onHour(hour)
  136. --if hour == 21 then
  137. -- if _G.is_middle ~= true then
  138. -- JjcLogic.onHour(hour)
  139. -- end
  140. --end
  141. JjcLogic.onHour(hour)
  142. JjcNewLadderLogic.onHour(hour)
  143. if hour == 2 then
  144. local weekDay = Util.getWeekDay()
  145. if weekDay == 2 then
  146. -- 每周一凌晨2點 斷開鏈接 并且重新取一下php的跨服信息 重新連新跨服
  147. MiddleConnect.autoResetMiddle()
  148. end
  149. end
  150. YunYingLogic.onHour(hour)
  151. UnionWarDBLogic.onHour(hour)
  152. AbsActLogic.onHour(hour)
  153. MoZhuMiddleLogic.onHour(hour)
  154. -- 晚上11点检查美团2楼玩家每日固定奖励领取情况
  155. if hour == 23 then
  156. local ZhuanpanLogic = require("zhuanpan.ZhuanpanLogic")
  157. ZhuanpanLogic.checkMtFrom2floorDailyReward()
  158. end
  159. _G.collectgarbage("step", 100000)
  160. end
  161. lastUpDateTime = lastUpDateTime or nil
  162. function onZhengDian()
  163. if lastUpDateTime and (now - lastUpDateTime) < 180000 or 120000 < now % (1000 * 60 * 60) then
  164. return
  165. end
  166. lastUpDateTime = now
  167. local hour = os.date("%H", math.floor(now / 1000)) + 0
  168. print("onZhengDian", hour)
  169. Log.write(Log.LOGID_MONITOR, _G.collectgarbage("count"), 'gc befor')
  170. _G.collectgarbage("collect")
  171. Log.write(Log.LOGID_MONITOR, _G.collectgarbage("count"), 'gc after')
  172. doZhengDian(hour)
  173. end
  174. function dayCheck()
  175. local call = 1
  176. local now = os.time()
  177. local resetTime = CommonDB.getResetTime()
  178. if resetTime == 0 then
  179. CommonDB.setResetTime(now)
  180. return
  181. end
  182. if call == 1 then
  183. local sameDay = Util.isSameDay(resetTime, now)
  184. call = sameDay == nil and 0 or 1
  185. end
  186. if call == 0 then
  187. CommonDB.setResetTime(now)
  188. DrillLogic.dayCheck()
  189. end
  190. end
  191. function middleHeartbeat()
  192. MiddleHeartBeat.heartbeat()
  193. end
  194. function unionOfflineCheck()
  195. UnionLogic.onTimer()
  196. end
  197. function paomaHerartbeat()
  198. ChatPaoMaLogic.onTimer()
  199. end
  200. function reportOnline()
  201. ReportOnline.onTimer()
  202. end
  203. function fcm()
  204. FcmLogic.onTimer()
  205. end
  206. function jjcGodWar()
  207. JjcGodWarLogic.onTimer()
  208. end
  209. function jjcLadder()
  210. JjcLadderLogic.onTimer()
  211. end
  212. function valley()
  213. ValleyLogic.onTimer()
  214. end
  215. function later()
  216. if LATER_DAYA[1] == nil then
  217. return
  218. end
  219. local tempData = LATER_DAYA[1]
  220. local now = os.time()
  221. local ts = tempData.ts
  222. if ts <= now then
  223. table.remove(LATER_DAYA, 1)
  224. tempData.func(tempData.param1, tempData.param2, tempData.param3, tempData.param4)
  225. end
  226. end
  227. last_exe_time = last_exe_time or {}
  228. function handlerTime(msec)
  229. now = msec + d
  230. if not register_timer[last_exe_func] then
  231. last_exe_func = nil
  232. end
  233. local tmp = last_exe_func
  234. while true do
  235. local k, v = next(register_timer, last_exe_func)
  236. if not k then
  237. k, v = next(register_timer)
  238. end
  239. if not k then
  240. return
  241. end
  242. last_exe_func = k
  243. local last_time = last_exe_time[k]
  244. if last_time then
  245. if last_time + v <= now then
  246. last_exe_time[k] = now
  247. Monitor.handleTimer(_ENV[k], now - last_time, k)
  248. break
  249. end
  250. else
  251. last_exe_time[k] = now
  252. break
  253. end
  254. if k == tmp then
  255. return
  256. end
  257. end
  258. end
  259. local function cmpLater(a, b)
  260. return a.ts < b.ts
  261. end
  262. function addLater(laterSec, func, param1, param2, param3, param4)
  263. local tempData = {}
  264. tempData.ts = os.time() + laterSec
  265. tempData.func = func
  266. tempData.param1 = param1
  267. tempData.param2 = param2
  268. tempData.param3 = param3
  269. tempData.param4 = param4
  270. table.insert(LATER_DAYA, tempData)
  271. table.sort(LATER_DAYA, cmpLater)
  272. end