Timer.lua 8.1 KB

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