TriggerLogic.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, ...)
  56. if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
  57. for i, callback in ipairs(moniterTbl[uuid][eventType]) do
  58. pcall(callback, eventType, uuid, ...)
  59. end
  60. end
  61. end
  62. -- 取消订阅
  63. function UnsubscribeEvent(eventType, uuid, callback)
  64. if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
  65. local tSubData = moniterTbl[uuid][eventType]
  66. for i, cb in ipairs(tSubData) do
  67. if cb == callback then
  68. table.remove(tSubData, i)
  69. break
  70. end
  71. end
  72. if nil == _G.next(tSubData) then
  73. moniterTbl[uuid][eventType] = nil
  74. end
  75. end
  76. end
  77. -- 移除所有订阅(玩家下线)
  78. function RemoveAllscribeEvent(uuid)
  79. moniterTbl[uuid] = nil
  80. end
  81. function onLogout(human)
  82. -- 这里应该先发下线事件,但是目前没有这种需要先不管
  83. RemoveAllscribeEvent(human.db._id)
  84. print("[TriggerLogic-onLogout] 玩家下线移除所有事件订阅 name = "..human.db.name)
  85. end
  86. --移除订阅
  87. -- function RemoveMoniter(handler)
  88. -- if not handler then
  89. -- return
  90. -- end
  91. -- table.remove(moniterTbl, handler)
  92. -- end
  93. --广播
  94. -- function Broadcast(human, triggerTag, val, isAdd)
  95. -- for tag, v in ipairs(moniterTbl) do
  96. -- if table.find(v.triggerArr, triggerTag) then
  97. -- local f = v.cbFunc
  98. -- local res = pcall(f, human, triggerTag, val, isAdd)
  99. -- if not res then
  100. -- print(string.format("广播失败, handler = %s", tag))
  101. -- end
  102. -- end
  103. -- end
  104. -- end
  105. -- --触发事件
  106. -- function Trigger(human, triggerTag, val)
  107. -- local isAdd = false
  108. -- if TriggerDefine.TRIGGERADDTB[triggerTag] then
  109. -- isAdd = true
  110. -- end
  111. -- if not isAdd then
  112. -- if not TriggerDefine.TRIGGERUPDATETB[triggerTag] then
  113. -- assert(false, string.format("触发事件不存在, tag = %s", triggerTag))
  114. -- end
  115. -- end
  116. -- Broadcast(human, triggerTag, val, isAdd)
  117. -- end