123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- --[[
- 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 battleCode = require("battle.battleCode")
- local moduleData = require("data.module")
- local root = {}
- function root:makeItemsStr(itemss)
- local str = ""
- for _, value in ipairs(itemss or {}) do
- if str == "" then
- str = str .. value.id .. "," .. value.count
- else
- str = str .. "," .. value.id .. "," .. value.count
- end
- end
- return str
- end
- -- 埋点 - 事件
- 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:log_ad(uid, pos, action)
- if is_empty(uid) or is_empty(pos) or is_empty(action) then
- return
- end
- local version = moduleData:get_version(uid)
- local channel = moduleData:get_channel(uid)
- serverLogUtil.logAdvertise(uid, channel, version, pos, action)
- 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
- -- TODO: Debug 埋这么大的一个坑,做什么呢?
- nodeMgr.send("game1_1", ".settleSrv", "user_game_event", uid, eventId, evtParams)
- return false
- end
- ----------------------------------------
- -- 节点分配
- ----------------------------------------
- local function l_dispatch_server_node(nodeType, uid)
- local funcName = "dispatchServer"
- if IS_PARTITION then
- -- TODO: Debug 分服逻辑,是否分区/分服?
- funcName = "dispatchServerByPartition"
- end
- local ok, server = nodeMgr.call("master", ".server_mgr", funcName, nodeType, uid, PARTITION_ID)
- 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 gameAgentMgr = nodeMgr.query_node_service_addr(node.nodename, ".gameAgentMgr")
- if not gameAgentMgr then
- return
- end
- local ok, gameAgent = nodeMgr.call(node.nodename, gameAgentMgr, "newGameAgent")
- 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("battle_master", ".server_mgr", "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 = "tb_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 nodecluster = skynet.getenv("nodecluster")
- local nodename = skynet.getenv("node_name")
- -- 玩家登陆服务器
- function root:master_user_login(uid, ...)
- return nodeMgr.call("master", ".server_mgr", "userLogin", nodecluster, nodename, uid, ...)
- end
- -- 玩家登出服务器
- function root:master_user_logout(uid, ...)
- return nodeMgr.call("master", ".server_mgr", "userLogout", nodecluster, 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 battleCode.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 battleCode.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 battleCode.BATTLE_NOT_EXIST
- end
- nodeMgr.send_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...)
- end
- return root
|