| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- --触发事件处理模块
- --需要保证订阅在前, 触发在后
- local TriggerDefine = require("trigger.TriggerDefine")
- --订阅列表
- local moniterTbl = {}
- -- 注册事件
- -- 为每个玩家维护订阅表
- 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, nValue1, nValue2, nValue3)
- if moniterTbl[uuid] and moniterTbl[uuid][eventType] then
- for i, callback in ipairs(moniterTbl[uuid][eventType]) do
- --print("[PublishEvent] 触发回调 eventType = "..eventType.." i = "..i)
- pcall(callback, eventType, uuid, nValue1, nValue2, nValue3)
- 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
|