| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- --触发事件处理模块
- --需要保证订阅在前, 触发在后
- local TriggerDefine = require("trigger.TriggerDefine")
- --订阅列表
- local moniterTbl = {}
- --检测触发事件是否正确
- -- local function checkTrigger(arg)
- -- local errTag = nil
- -- if type(arg) == "table" then
- -- for _, tag in ipairs(arg) do
- -- if not TriggerDefine.TRIGGERADDTB[tag] and not TriggerDefine.TRIGGERUPDATETB[tag] then
- -- errTag = tag
- -- break
- -- end
- -- end
- -- else
- -- if not TriggerDefine.TRIGGERADDTB[arg] and not TriggerDefine.TRIGGERUPDATETB[arg] then
- -- errTag = arg
- -- end
- -- end
- -- if errTag then
- -- assert(false, string.format("注册事件不存在, tag = %s", errTag))
- -- end
- -- return true
- -- end
- --注册订阅
- -- function RegisterMoniter(triggerArr, cbFunc)
- -- checkTrigger(triggerArr)
- -- local handler = #moniterTbl+1
- -- moniterTbl[handler] = {
- -- triggerArr = triggerArr,
- -- cbFunc = cbFunc,
- -- }
- -- return handler
- -- end
- -- 注册事件
- -- 为每个玩家维护订阅表
- function SubscribeEvent(eventType, uuid, callback)
- if not moniterTbl[uuid] then
- moniterTbl[uuid] = {}
- end
- if not moniterTbl[uuid][eventType] then
- moniterTbl[uuid][eventType] = {}
- else
- for i, funcCallBack in ipairs(moniterTbl[uuid][eventType]) do
- if funcCallBack == callback then
- print("[SubscribeEvent] 重复订阅消息 eventType = "..eventType)
- return
- end
- end
- end
-
- table.insert(moniterTbl[uuid][eventType], callback)
- end
- -- 发布订阅
- function PublishEvent(eventType, uuid, ...)
- if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
- for i, callback in ipairs(moniterTbl[uuid][eventType]) do
- pcall(callback, eventType, uuid, ...)
- end
- end
- end
- -- 取消订阅
- function UnsubscribeEvent(eventType, uuid, callback)
- if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
- local tSubData = moniterTbl[uuid][eventType]
- for i, cb in ipairs(tSubData) do
- if cb == callback then
- table.remove(tSubData, i)
- break
- end
- end
- if nil == _G.next(tSubData) then
- moniterTbl[uuid][eventType] = nil
- end
- end
- end
- -- 移除所有订阅(玩家下线)
- function RemoveAllscribeEvent(uuid)
- moniterTbl[uuid] = nil
- end
- function onLogout(human)
- -- 这里应该先发下线事件,但是目前没有这种需要先不管
- RemoveAllscribeEvent(human.db._id)
- print("[TriggerLogic-onLogout] 玩家下线移除所有事件订阅 name = "..human.db.name)
- end
- --移除订阅
- -- function RemoveMoniter(handler)
- -- if not handler then
- -- return
- -- end
- -- table.remove(moniterTbl, handler)
- -- end
- --广播
- -- function Broadcast(human, triggerTag, val, isAdd)
- -- for tag, v in ipairs(moniterTbl) do
- -- if table.find(v.triggerArr, triggerTag) then
- -- local f = v.cbFunc
- -- local res = pcall(f, human, triggerTag, val, isAdd)
- -- if not res then
- -- print(string.format("广播失败, handler = %s", tag))
- -- end
- -- end
- -- end
- -- end
- -- --触发事件
- -- function Trigger(human, triggerTag, val)
- -- local isAdd = false
- -- if TriggerDefine.TRIGGERADDTB[triggerTag] then
- -- isAdd = true
- -- end
- -- if not isAdd then
- -- if not TriggerDefine.TRIGGERUPDATETB[triggerTag] then
- -- assert(false, string.format("触发事件不存在, tag = %s", triggerTag))
- -- end
- -- end
- -- Broadcast(human, triggerTag, val, isAdd)
- -- end
|