TriggerLogic.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. --触发事件处理模块
  2. --需要保证订阅在前, 触发在后
  3. local TriggerDefine = require("trigger.TriggerDefine")
  4. --订阅列表
  5. local moniterTbl = {}
  6. --检测触发事件是否正确
  7. -- local function checkTrigger(arg)
  8. -- local errTag = nil
  9. -- if type(arg) == "table" then
  10. -- for _, tag in ipairs(arg) do
  11. -- if not TriggerDefine.TRIGGERADDTB[tag] and not TriggerDefine.TRIGGERUPDATETB[tag] then
  12. -- errTag = tag
  13. -- break
  14. -- end
  15. -- end
  16. -- else
  17. -- if not TriggerDefine.TRIGGERADDTB[arg] and not TriggerDefine.TRIGGERUPDATETB[arg] then
  18. -- errTag = arg
  19. -- end
  20. -- end
  21. -- if errTag then
  22. -- assert(false, string.format("注册事件不存在, tag = %s", errTag))
  23. -- end
  24. -- return true
  25. -- end
  26. --注册订阅
  27. -- function RegisterMoniter(triggerArr, cbFunc)
  28. -- checkTrigger(triggerArr)
  29. -- local handler = #moniterTbl+1
  30. -- moniterTbl[handler] = {
  31. -- triggerArr = triggerArr,
  32. -- cbFunc = cbFunc,
  33. -- }
  34. -- return handler
  35. -- end
  36. -- 注册事件
  37. -- 为每个玩家维护订阅表
  38. function SubscribeEvent(eventType, uuid, callback)
  39. if not moniterTbl[uuid] then
  40. moniterTbl[uuid] = {}
  41. end
  42. if not moniterTbl[uuid][eventType] then
  43. moniterTbl[uuid][eventType] = {}
  44. else
  45. for i, funcCallBack in ipairs(moniterTbl[uuid][eventType]) do
  46. if funcCallBack == callback then
  47. print("[SubscribeEvent] 重复订阅消息 eventType = "..eventType)
  48. return
  49. end
  50. end
  51. end
  52. table.insert(moniterTbl[uuid][eventType], callback)
  53. end
  54. -- 发布订阅
  55. function PublishEvent(eventType, uuid, nValue1, nValue2)
  56. if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
  57. for i, callback in ipairs(moniterTbl[uuid][eventType]) do
  58. print("[PublishEvent] 触发回调 eventType = "..eventType.." i = "..i)
  59. pcall(callback, eventType, uuid, nValue1, nValue2)
  60. end
  61. end
  62. end
  63. -- 取消订阅
  64. function UnsubscribeEvent(eventType, uuid, callback)
  65. if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
  66. local tSubData = moniterTbl[uuid][eventType]
  67. for i, cb in ipairs(tSubData) do
  68. if cb == callback then
  69. table.remove(tSubData, i)
  70. break
  71. end
  72. end
  73. if nil == _G.next(tSubData) then
  74. moniterTbl[uuid][eventType] = nil
  75. end
  76. end
  77. end
  78. -- 移除所有订阅(玩家下线)
  79. function RemoveAllscribeEvent(uuid)
  80. moniterTbl[uuid] = nil
  81. end
  82. function onLogout(human)
  83. -- 这里应该先发下线事件,但是目前没有这种需要先不管
  84. RemoveAllscribeEvent(human.db._id)
  85. print("[TriggerLogic-onLogout] 玩家下线移除所有事件订阅 name = "..human.db.name)
  86. end
  87. --移除订阅
  88. -- function RemoveMoniter(handler)
  89. -- if not handler then
  90. -- return
  91. -- end
  92. -- table.remove(moniterTbl, handler)
  93. -- end
  94. --广播
  95. -- function Broadcast(human, triggerTag, val, isAdd)
  96. -- for tag, v in ipairs(moniterTbl) do
  97. -- if table.find(v.triggerArr, triggerTag) then
  98. -- local f = v.cbFunc
  99. -- local res = pcall(f, human, triggerTag, val, isAdd)
  100. -- if not res then
  101. -- print(string.format("广播失败, handler = %s", tag))
  102. -- end
  103. -- end
  104. -- end
  105. -- end
  106. -- --触发事件
  107. -- function Trigger(human, triggerTag, val)
  108. -- local isAdd = false
  109. -- if TriggerDefine.TRIGGERADDTB[triggerTag] then
  110. -- isAdd = true
  111. -- end
  112. -- if not isAdd then
  113. -- if not TriggerDefine.TRIGGERUPDATETB[triggerTag] then
  114. -- assert(false, string.format("触发事件不存在, tag = %s", triggerTag))
  115. -- end
  116. -- end
  117. -- Broadcast(human, triggerTag, val, isAdd)
  118. -- end