util_user.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 moduleData = require("data.module")
  14. local root = {}
  15. -- 埋点 - 事件
  16. function root:log_event(uid, keyEvent, cnt)
  17. if is_empty(uid) or is_empty(keyEvent) or is_empty(cnt) then
  18. return
  19. end
  20. local version = moduleData:get_version(uid)
  21. local channel = moduleData:get_channel(uid)
  22. serverLogUtil.logEvent(uid, version, channel, keyEvent, cnt)
  23. end
  24. -- 埋点 - 资源变化
  25. function root:log_resource(uid, keyEvent, itemId, delta, total)
  26. if is_empty(uid) or is_empty(keyEvent) or is_empty(itemId) then
  27. return
  28. end
  29. local channel = moduleData:get_channel(uid)
  30. serverLogUtil.logResource(uid, keyEvent, itemId, delta, total, channel)
  31. end
  32. -- 通知
  33. function root:user_proto_notify(uid, protoName, msg)
  34. if uid == nil or protoName == nil then
  35. log.error("玩家消息推送失败 uid[%s] protoName[%s]", tostring(uid), tostring(protoName))
  36. return false
  37. end
  38. if is_robot(uid) then
  39. return false
  40. end
  41. local nodeInfo = self:user_get_cluster_info(uid, "gate")
  42. if nodeMgr.is_node_info_valid(nodeInfo) then
  43. nodeMgr.send_with_node_info(nodeInfo, "s2c", uid, protoName, msg)
  44. return true
  45. else
  46. log.error("玩家消息推送失败 uid[%s] nodeInfo[%s] protoName[%s]", tostring(uid), tostring(nodeInfo), tostring(protoName))
  47. end
  48. return false
  49. end
  50. -- 推送 - 客户端日志
  51. function root:user_notify_client_log(uid, msg)
  52. if uid == nil or is_empty(msg) then
  53. return
  54. end
  55. if not IS_TEST then
  56. return
  57. end
  58. -- self:user_proto_notify(uid, "on_log_client", {logStr = msg})
  59. end
  60. -- 踢玩家下线
  61. function root:user_kicked(uid, msg)
  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, "kick", uid, msg)
  65. end
  66. end
  67. -- 事件
  68. function root:user_dispatch_event(uid, eventId, evtParams)
  69. if uid == nil or eventId == nil then
  70. return false
  71. end
  72. -- log.info(
  73. -- "user_dispatch_event uid[%s] eventId[%s] evtParams[%s]",
  74. -- tostring(uid),
  75. -- tostring(eventId),
  76. -- tostring(evtParams)
  77. -- )
  78. if evtParams.times == nil then
  79. evtParams.times = 1
  80. end
  81. local nodeInfo = self:user_get_cluster_info(uid, "game")
  82. if nodeMgr.is_node_info_valid(nodeInfo) then
  83. nodeMgr.send_with_node_info(nodeInfo, "user_game_event", uid, eventId, evtParams)
  84. return true
  85. end
  86. nodeMgr.send("game1_1", ".settleSrv", "user_game_event", uid, eventId, evtParams)
  87. return false
  88. end
  89. ----------------------------------------
  90. -- 节点分配
  91. ----------------------------------------
  92. local function l_dispatch_server_node(nodeType, uid)
  93. local ok, server = nodeMgr.call("master", ".srvNodeMgr", "user_dispatch_cluster_node", uid, nodeType)
  94. if not ok then
  95. return
  96. end
  97. return server
  98. end
  99. --- 分配网关节点
  100. function root:user_dispatch_gate_node(uid)
  101. return l_dispatch_server_node("gate", uid)
  102. end
  103. --- 分配游戏节点
  104. function root:user_dispatch_game(uid)
  105. local node = l_dispatch_server_node("game", uid)
  106. if node == nil or is_empty(node.nodename) then
  107. return
  108. end
  109. local srvAgentMgr = nodeMgr.query_node_service_addr(node.nodename, ".srvAgentMgr")
  110. if not srvAgentMgr then
  111. return
  112. end
  113. local ok, gameAgent = nodeMgr.call(node.nodename, srvAgentMgr, "user_get_game_agent", uid)
  114. if not ok or gameAgent == nil then
  115. return
  116. end
  117. return {nodeName = node.nodename, agent = gameAgent}
  118. end
  119. -- 分配Business节点
  120. function root:user_dispatch_match_node(uid, conf)
  121. local ok, matchNode = nodeMgr.call("battlemaster", ".srvNodeMgr", "dispatch_match_node", uid, conf)
  122. if not ok or is_empty(matchNode) then
  123. return
  124. end
  125. local ok, matchAgent = nodeMgr.call(matchNode, ".BattleMatch", "dispatch_match_agent", conf.battleType)
  126. if not ok then
  127. return
  128. end
  129. local nodeInfo = {}
  130. nodeInfo.nodeName = matchNode
  131. nodeInfo.agent = matchAgent
  132. return nodeInfo
  133. end
  134. local MODULE_SESSION = "session"
  135. -- 玩家节点信息
  136. function root:user_get_cluster_info(uid, clusterName)
  137. if uid == nil or is_empty(clusterName) then
  138. return
  139. end
  140. local nodeInfo = moduleData:hget_json(uid, MODULE_SESSION, clusterName)
  141. if is_empty(nodeInfo) then
  142. nodeInfo = nil
  143. end
  144. return nodeInfo
  145. end
  146. -- 玩家是否登录网关
  147. function root:user_is_online_gate(uid)
  148. local nodeInfo = self:user_get_cluster_info(uid, "gate")
  149. if nodeMgr.is_node_info_valid(nodeInfo) then
  150. return true
  151. end
  152. return false
  153. end
  154. -- 玩家是否登录游戏
  155. function root:user_is_online_game(uid)
  156. local nodeInfo = self:user_get_cluster_info(uid, "game")
  157. if nodeMgr.is_node_info_valid(nodeInfo) then
  158. return true
  159. end
  160. return false
  161. end
  162. ----------------------------------------
  163. -- game
  164. ----------------------------------------
  165. function root:user_send_game_agent(uid, interface, ...)
  166. local nodeInfo = self:user_get_cluster_info(uid, "game")
  167. if nodeMgr.is_node_info_valid(nodeInfo) then
  168. nodeMgr.send_with_node_info(nodeInfo, "doCmd", uid, interface, ...)
  169. end
  170. end
  171. function root:user_call_game_agent(uid, interface, ...)
  172. local nodeInfo = self:user_get_cluster_info(uid, "game")
  173. if nodeMgr.is_node_info_valid(nodeInfo) then
  174. return nodeMgr.call_with_node_info(nodeInfo, "doCmd", uid, interface, ...)
  175. end
  176. end
  177. ----------------------------------------
  178. -- 中心服
  179. ----------------------------------------
  180. local clusterName = skynet.getenv("clusterName")
  181. local nodename = skynet.getenv("nodeName")
  182. -- 玩家登陆服务器
  183. function root:master_user_login(uid, ...)
  184. return nodeMgr.call("master", ".srvNodeMgr", "userLogin", clusterName, nodename, uid, ...)
  185. end
  186. -- 玩家登出服务器
  187. function root:master_user_logout(uid, ...)
  188. return nodeMgr.call("master", ".srvNodeMgr", "userLogout", clusterName, nodename, uid, ...)
  189. end
  190. ----------------------------------------
  191. -- 战斗节点
  192. ----------------------------------------
  193. function root:user_call_2_battle(uid, cmd, ...)
  194. if uid == nil or cmd == nil then
  195. return code.PARAMTER_ERROR
  196. end
  197. local nodeInfo = self:user_get_cluster_info(uid, "battle")
  198. if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then
  199. return code.BATTLE.NOT_EXIST
  200. end
  201. local ok, ret = nodeMgr.call_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...)
  202. if not ok or ret == nil then
  203. return code.BATTLE.NOT_EXIST
  204. end
  205. return ret.code, ret
  206. end
  207. function root:user_send_2_battle(uid, cmd, ...)
  208. if uid == nil or cmd == nil then
  209. return code.PARAMTER_ERROR
  210. end
  211. local nodeInfo = self:user_get_cluster_info(uid, "battle")
  212. if not nodeMgr.is_node_info_valid(nodeInfo) or nodeInfo.battleId == nil then
  213. return code.BATTLE.NOT_EXIST
  214. end
  215. nodeMgr.send_with_node_info(nodeInfo, "forward", nodeInfo.battleId, cmd, ...)
  216. end
  217. return root