bayuMIR/Mirserver/Mir200/Envir/QuestDiary/net/Message.lua

107 lines
2.8 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Message = {}
Message.dispatch_handler = {}
--- 发送消息
---@param actor any 消息接收者
---@param msgID number 消息ID
---@param arg1 any 参数1
---@param arg2 any 参数2
---@param arg3 any 参数3
---@param data table 消息数据
function Message.sendmsg(actor, msgID, arg1, arg2, arg3, data)
local str = data and tbl2json(data) or nil
--LOGPrint("sendmsg msgID=", msgID, arg1, arg2, arg3, str)
-- LOGPrint("sendmsg msgID=", msgID, arg1, arg2, arg3, str)
sendluamsg(actor, msgID, arg1, arg2, arg3, str)
end
---* 统一发送网络消息处理
function Message:SubLink(actor, Objcfg_info, data)
local sync = { sync_obj = Objcfg_info, sync_cfg = data }
sendluamsg(actor, ssrNetMsgCfg.npc_sync, 0, 0, 0, tbl2json(sync))
end
--- 分发消息
---@param actor any 角色对象
---@param msgID number 消息ID
---@param arg1 any 参数1
---@param arg2 any 参数2
---@param arg3 any 参数3
---@param str string 字符串参数
Message._cdCache = {}
Message._cdTime = 200 -- 同一操作最小间隔(毫秒)
function Message.dispatch(actor, msgID, arg1, arg2, arg3, str)
local data = str and json2tbl(str) or nil
if not data then return end
local msgName = data.sync_obj
if not msgName then return end
local module, method = msgName:match("([^.]*)_(.*)")
if not module or not method then return end
local obj = Message.dispatch_handler[module]
if not obj then return end
-- 白名单校验:只允许 allowFunc 中注册的方法被前端调用
if obj.allowFunc then
local allowed = false
for _, v in ipairs(obj.allowFunc) do
if v == method then
allowed = true
break
end
end
if not allowed then return end
end
-- 操作频率限制:防止前端高频刷包
local now = gettcount64()
if Message._cdCache[actor] and (now - Message._cdCache[actor]) < Message._cdTime then
return
end
Message._cdCache[actor] = now
obj[method](obj, actor, arg1, arg2, arg3, data.sync_cfg)
end
-- 所有发送给服务端的网络消息触发
function handlerequest(actor, msgid, arg1, arg2, arg3, sMsg)
--* --同步数据
if msgid == ssrNetMsgCfg.sync then
Player.syncCfg(actor)
return
end
--* 解锁仓库格子
if msgid == ssrNetMsgCfg.open_storagecount then
Bag.changestorage(actor)
return
end
local result, errinfo = pcall(Message.dispatch, actor, msgid, arg1, arg2, arg3, sMsg)
if not result then
local name = getbaseinfo(actor, ConstCfg.gbase.name)
local err = "网络消息派发错误消息ID=" .. msgid
end
end
-- 气泡表回调处理
function bubblefunc(actor, ...)
LOGPrint("气泡表进来了")
local data = { ... }
LOGDump(data)
end
---* 下线清理CD缓存
GameEvent.add(EventCfg.onExitGame, function(actor)
Message._cdCache[actor] = nil
end,Message)
return Message