--[[ Descripttion:玩家埋点 version: Author: Neo,Huang Date: 2022-05-26 19:45:58 LastEditors: Neo,Huang LastEditTime: 2022-05-26 19:46:41 --]] local skynet = require("skynet") local serverLogUtil = require("utils.serverLogUtil") local nodeMgr = require("nodeMgr") local code = require("code") local moduleData = require("data.module") local root = {} -- 埋点 - 事件 function root:log_event(uid, keyEvent, cnt) if is_empty(uid) or is_empty(keyEvent) or is_empty(cnt) then return end local version = moduleData:get_version(uid) local channel = moduleData:get_channel(uid) serverLogUtil.logEvent(uid, version, channel, keyEvent, cnt) end -- 埋点 - 资源变化 function root:log_resource(uid, keyEvent, itemId, delta, total) if is_empty(uid) or is_empty(keyEvent) or is_empty(itemId) then return end local channel = moduleData:get_channel(uid) serverLogUtil.logResource(uid, keyEvent, itemId, delta, total, channel) end -- 通知 function root:user_proto_notify(uid, protoName, msg) if uid == nil or protoName == nil then log.error("玩家消息推送失败 uid[%s] protoName[%s]", tostring(uid), tostring(protoName)) return false end if is_robot(uid) then return false end local nodeInfo = self:user_get_cluster_info(uid, "gate") if nodeMgr.is_node_info_valid(nodeInfo) then nodeMgr.send_with_node_info(nodeInfo, "pushS2C", uid, protoName, msg) return true else log.error("玩家消息推送失败 uid[%s] nodeInfo[%s] protoName[%s]", tostring(uid), tostring(nodeInfo), tostring(protoName)) end return false end -- 推送 - 客户端日志 function root:user_notify_client_log(uid, msg) if uid == nil or is_empty(msg) then return end if not IS_TEST then return end -- self:user_proto_notify(uid, "on_log_client", {logStr = msg}) end -- 踢玩家下线 function root:user_kicked(uid, msg) local nodeInfo = self:user_get_cluster_info(uid, "gate") if nodeMgr.is_node_info_valid(nodeInfo) then nodeMgr.send_with_node_info(nodeInfo, "kick", uid, msg) end end -- 事件 function root:user_dispatch_event(uid, eventId, evtParams) if uid == nil or eventId == nil then return false end -- log.info( -- "user_dispatch_event uid[%s] eventId[%s] evtParams[%s]", -- tostring(uid), -- tostring(eventId), -- tostring(evtParams) -- ) if evtParams.times == nil then evtParams.times = 1 end local nodeInfo = self:user_get_cluster_info(uid, "game") if nodeMgr.is_node_info_valid(nodeInfo) then nodeMgr.send_with_node_info(nodeInfo, "userGameEvent", uid, eventId, evtParams) return true end nodeMgr.send("game1_1", ".settleSrv", "user_game_event", uid, eventId, evtParams) return false end ---------------------------------------- -- 节点分配 ---------------------------------------- local function l_dispatch_server_node(nodeType, uid) local ok, server = nodeMgr.call("master", ".srvNodeMgr", "user_dispatch_cluster_node", uid, nodeType) if not ok then return end return server end --- 分配网关节点 function root:user_dispatch_gate_node(uid) return l_dispatch_server_node("gate", uid) end --- 分配游戏节点 function root:user_dispatch_game(uid) local node = l_dispatch_server_node("game", uid) if node == nil or is_empty(node.nodename) then return end local srvAgentMgr = nodeMgr.query_node_service_addr(node.nodename, ".srvAgentMgr") if not srvAgentMgr then return end local ok, gameAgent = nodeMgr.call(node.nodename, srvAgentMgr, "user_get_game_agent", uid) if not ok or gameAgent == nil then return end return {nodeName = node.nodename, agent = gameAgent} end -- 分配Business节点 function root:user_dispatch_match_node(uid, conf) local ok, matchNode = nodeMgr.call("battlemaster", ".srvNodeMgr", "dispatch_match_node", uid, conf) if not ok or is_empty(matchNode) then return end local ok, matchAgent = nodeMgr.call(matchNode, ".BattleMatch", "dispatch_match_agent", conf.battleType) if not ok then return end local nodeInfo = {} nodeInfo.nodeName = matchNode nodeInfo.agent = matchAgent return nodeInfo end local MODULE_SESSION = "session" -- 玩家节点信息 function root:user_get_cluster_info(uid, clusterName) if uid == nil or is_empty(clusterName) then return end local nodeInfo = moduleData:hget_json(uid, MODULE_SESSION, clusterName) if is_empty(nodeInfo) then nodeInfo = nil end return nodeInfo end -- 玩家是否登录网关 function root:user_is_online_gate(uid) local nodeInfo = self:user_get_cluster_info(uid, "gate") if nodeMgr.is_node_info_valid(nodeInfo) then return true end return false end -- 玩家是否登录游戏 function root:user_is_online_game(uid) local nodeInfo = self:user_get_cluster_info(uid, "game") if nodeMgr.is_node_info_valid(nodeInfo) then return true end return false end ---------------------------------------- -- game ---------------------------------------- function root:user_send_game_agent(uid, interface, ...) local nodeInfo = self:user_get_cluster_info(uid, "game") if nodeMgr.is_node_info_valid(nodeInfo) then nodeMgr.send_with_node_info(nodeInfo, "doCmd", uid, interface, ...) end end function root:user_call_game_agent(uid, interface, ...) local nodeInfo = self:user_get_cluster_info(uid, "game") if nodeMgr.is_node_info_valid(nodeInfo) then return nodeMgr.call_with_node_info(nodeInfo, "doCmd", uid, interface, ...) end end ---------------------------------------- -- 中心服 ---------------------------------------- local clusterName = skynet.getenv("clusterName") local nodename = skynet.getenv("nodeName") -- 玩家登陆服务器 function root:master_user_login(uid, ...) return nodeMgr.call("master", ".srvNodeMgr", "userLogin", clusterName, nodename, uid, ...) end -- 玩家登出服务器 function root:master_user_logout(uid, ...) return nodeMgr.call("master", ".srvNodeMgr", "userLogout", clusterName, nodename, uid, ...) end ---------------------------------------- -- 战斗节点 ---------------------------------------- function root:user_call_2_battle(uid, cmd, ...) if uid == nil or cmd == nil then return code.PARAMTER_ERROR end local nodeInfo = self:user_get_cluster_info(uid, "battle") if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then return code.BATTLE.NOT_EXIST end local ok, ret = nodeMgr.call_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...) if not ok or ret == nil then return code.BATTLE.NOT_EXIST end return ret.code, ret end function root:user_send_2_battle(uid, cmd, ...) if uid == nil or cmd == nil then return code.PARAMTER_ERROR end local nodeInfo = self:user_get_cluster_info(uid, "battle") if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then return code.BATTLE.NOT_EXIST end nodeMgr.send_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...) end return root