Timer.lua 11 KB

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