util_user.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. --[[
  2. Descripttion:玩家埋点
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-05-26 19:45:58
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-05-26 19:46:41
  8. --]]
  9. local skynet = require("skynet")
  10. local serverLogUtil = require("utils.serverLogUtil")
  11. local nodeMgr = require("nodeMgr")
  12. local code = require("code")
  13. local battleCode = require("battle.battleCode")
  14. local moduleData = require("data.module")
  15. local root = {}
  16. function root:makeItemsStr(itemss)
  17. local str = ""
  18. for _, value in ipairs(itemss or {}) do
  19. if str == "" then
  20. str = str .. value.id .. "," .. value.count
  21. else
  22. str = str .. "," .. value.id .. "," .. value.count
  23. end
  24. end
  25. return str
  26. end
  27. -- 埋点 - 事件
  28. function root:log_event(uid, keyEvent, cnt)
  29. if is_empty(uid) or is_empty(keyEvent) or is_empty(cnt) then
  30. return
  31. end
  32. local version = moduleData:get_version(uid)
  33. local channel = moduleData:get_channel(uid)
  34. serverLogUtil.logEvent(uid, version, channel, keyEvent, cnt)
  35. end
  36. -- 埋点 - 资源变化
  37. function root:log_resource(uid, keyEvent, itemId, delta, total)
  38. if is_empty(uid) or is_empty(keyEvent) or is_empty(itemId) then
  39. return
  40. end
  41. local channel = moduleData:get_channel(uid)
  42. serverLogUtil.logResource(uid, keyEvent, itemId, delta, total, channel)
  43. end
  44. -- 埋点 - 广告
  45. function root:log_ad(uid, pos, action)
  46. if is_empty(uid) or is_empty(pos) or is_empty(action) then
  47. return
  48. end
  49. local version = moduleData:get_version(uid)
  50. local channel = moduleData:get_channel(uid)
  51. serverLogUtil.logAdvertise(uid, channel, version, pos, action)
  52. end
  53. -- 通知
  54. function root:user_proto_notify(uid, protoName, msg)
  55. if uid == nil or protoName == nil then
  56. log.error("玩家消息推送失败 uid[%s] protoName[%s]", tostring(uid), tostring(protoName))
  57. return false
  58. end
  59. if is_robot(uid) then
  60. return false
  61. end
  62. local nodeInfo = self:user_get_cluster_info(uid, "gate")
  63. if nodeMgr.is_node_info_valid(nodeInfo) then
  64. nodeMgr.send_with_node_info(nodeInfo, "pushS2C", uid, protoName, msg)
  65. return true
  66. else
  67. log.error("玩家消息推送失败 uid[%s] nodeInfo[%s] protoName[%s]", tostring(uid), tostring(nodeInfo), tostring(protoName))
  68. end
  69. return false
  70. end
  71. -- 推送 - 客户端日志
  72. function root:user_notify_client_log(uid, msg)
  73. if uid == nil or is_empty(msg) then
  74. return
  75. end
  76. if not IS_TEST then
  77. return
  78. end
  79. -- self:user_proto_notify(uid, "on_log_client", {logStr = msg})
  80. end
  81. -- 踢玩家下线
  82. function root:user_kicked(uid, msg)
  83. local nodeInfo = self:user_get_cluster_info(uid, "gate")
  84. if nodeMgr.is_node_info_valid(nodeInfo) then
  85. nodeMgr.send_with_node_info(nodeInfo, "kick", uid, msg)
  86. end
  87. end
  88. -- 事件
  89. function root:user_dispatch_event(uid, eventId, evtParams)
  90. if uid == nil or eventId == nil then
  91. return false
  92. end
  93. -- log.info(
  94. -- "user_dispatch_event uid[%s] eventId[%s] evtParams[%s]",
  95. -- tostring(uid),
  96. -- tostring(eventId),
  97. -- tostring(evtParams)
  98. -- )
  99. if evtParams.times == nil then
  100. evtParams.times = 1
  101. end
  102. local nodeInfo = self:user_get_cluster_info(uid, "game")
  103. if nodeMgr.is_node_info_valid(nodeInfo) then
  104. nodeMgr.send_with_node_info(nodeInfo, "userGameEvent", uid, eventId, evtParams)
  105. return true
  106. end
  107. -- TODO: Debug 埋这么大的一个坑,做什么呢?
  108. nodeMgr.send("game1_1", ".settleSrv", "user_game_event", uid, eventId, evtParams)
  109. return false
  110. end
  111. ----------------------------------------
  112. -- 节点分配
  113. ----------------------------------------
  114. local function l_dispatch_server_node(nodeType, uid)
  115. local funcName = "dispatchServer"
  116. if IS_PARTITION then
  117. -- TODO: Debug 分服逻辑,是否分区/分服?
  118. funcName = "dispatchServerByPartition"
  119. end
  120. local ok, server = nodeMgr.call("master", ".server_mgr", funcName, nodeType, uid, PARTITION_ID)
  121. if not ok then
  122. return
  123. end
  124. return server
  125. end
  126. --- 分配网关节点
  127. function root:user_dispatch_gate_node(uid)
  128. return l_dispatch_server_node("gate", uid)
  129. end
  130. --- 分配游戏节点
  131. function root:user_dispatch_game(uid)
  132. local node = l_dispatch_server_node("game", uid)
  133. if node == nil or is_empty(node.nodename) then
  134. return
  135. end
  136. local gameAgentMgr = nodeMgr.query_node_service_addr(node.nodename, ".gameAgentMgr")
  137. if not gameAgentMgr then
  138. return
  139. end
  140. local ok, gameAgent = nodeMgr.call(node.nodename, gameAgentMgr, "newGameAgent")
  141. if not ok or gameAgent == nil then
  142. return
  143. end
  144. return {nodeName = node.nodename, agent = gameAgent}
  145. end
  146. -- 分配Business节点
  147. function root:user_dispatch_match_node(uid, conf)
  148. local ok, matchNode = nodeMgr.call("battle_master", ".server_mgr", "dispatch_match_node", uid, conf)
  149. if not ok or is_empty(matchNode) then
  150. return
  151. end
  152. local ok, matchAgent = nodeMgr.call(matchNode, ".BattleMatch", "dispatch_match_agent", conf.battleType)
  153. if not ok then
  154. return
  155. end
  156. local nodeInfo = {}
  157. nodeInfo.nodeName = matchNode
  158. nodeInfo.agent = matchAgent
  159. return nodeInfo
  160. end
  161. local MODULE_SESSION = "tb_session"
  162. -- 玩家节点信息
  163. function root:user_get_cluster_info(uid, clusterName)
  164. if uid == nil or is_empty(clusterName) then
  165. return
  166. end
  167. local nodeInfo = moduleData:hget_json(uid, MODULE_SESSION, clusterName)
  168. if is_empty(nodeInfo) then
  169. nodeInfo = nil
  170. end
  171. return nodeInfo
  172. end
  173. -- 玩家是否登录网关
  174. function root:user_is_online_gate(uid)
  175. local nodeInfo = self:user_get_cluster_info(uid, "gate")
  176. if nodeMgr.is_node_info_valid(nodeInfo) then
  177. return true
  178. end
  179. return false
  180. end
  181. -- 玩家是否登录游戏
  182. function root:user_is_online_game(uid)
  183. local nodeInfo = self:user_get_cluster_info(uid, "game")
  184. if nodeMgr.is_node_info_valid(nodeInfo) then
  185. return true
  186. end
  187. return false
  188. end
  189. ----------------------------------------
  190. -- game
  191. ----------------------------------------
  192. function root:user_send_game_agent(uid, interface, ...)
  193. local nodeInfo = self:user_get_cluster_info(uid, "game")
  194. if nodeMgr.is_node_info_valid(nodeInfo) then
  195. nodeMgr.send_with_node_info(nodeInfo, "doCmd", uid, interface, ...)
  196. end
  197. end
  198. function root:user_call_game_agent(uid, interface, ...)
  199. local nodeInfo = self:user_get_cluster_info(uid, "game")
  200. if nodeMgr.is_node_info_valid(nodeInfo) then
  201. return nodeMgr.call_with_node_info(nodeInfo, "doCmd", uid, interface, ...)
  202. end
  203. end
  204. ----------------------------------------
  205. -- 中心服
  206. ----------------------------------------
  207. local nodecluster = skynet.getenv("nodecluster")
  208. local nodename = skynet.getenv("node_name")
  209. -- 玩家登陆服务器
  210. function root:master_user_login(uid, ...)
  211. return nodeMgr.call("master", ".server_mgr", "userLogin", nodecluster, nodename, uid, ...)
  212. end
  213. -- 玩家登出服务器
  214. function root:master_user_logout(uid, ...)
  215. return nodeMgr.call("master", ".server_mgr", "userLogout", nodecluster, nodename, uid, ...)
  216. end
  217. ----------------------------------------
  218. -- 战斗节点
  219. ----------------------------------------
  220. function root:user_call_2_battle(uid, cmd, ...)
  221. if uid == nil or cmd == nil then
  222. return code.PARAMTER_ERROR
  223. end
  224. local nodeInfo = self:user_get_cluster_info(uid, "battle")
  225. if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then
  226. return battleCode.BATTLE_NOT_EXIST
  227. end
  228. local ok, ret = nodeMgr.call_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...)
  229. if not ok or ret == nil then
  230. return battleCode.BATTLE_NOT_EXIST
  231. end
  232. return ret.code, ret
  233. end
  234. function root:user_send_2_battle(uid, cmd, ...)
  235. if uid == nil or cmd == nil then
  236. return code.PARAMTER_ERROR
  237. end
  238. local nodeInfo = self:user_get_cluster_info(uid, "battle")
  239. if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then
  240. return battleCode.BATTLE_NOT_EXIST
  241. end
  242. nodeMgr.send_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...)
  243. end
  244. return root