MiddleCommonLogic.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. --------------------------------
  2. -- 文件名 : MiddleCommonLogic
  3. -- 文件说明 : 跨服相关-通用管理(用于处理比较杂项的数据)
  4. -- 创建时间 : 2025/02/26
  5. -- 创建人 : FC
  6. --------------------------------
  7. local Config = require("Config")
  8. local Log = require("common.Log")
  9. local CommonDB = require("common.CommonDB")
  10. local InnerMsg = require("core.InnerMsg")
  11. local RoleLogic = require("role.RoleLogic")
  12. local Msg = require("core.Msg")
  13. local ObjHuman = require("core.ObjHuman")
  14. local CombatDefine = require("combat.CombatDefine")
  15. local Util = require("common.Util")
  16. local Broadcast = require("broadcast.Broadcast")
  17. local Lang = require("common.Lang")
  18. local CombatLogic = require("combat.CombatLogic")
  19. local CombatPosLogic = require("combat.CombatPosLogic")
  20. local MiddleDefine = require("middle.MiddleDefine")
  21. local YunYingLogic = require("yunying.YunYingLogic")
  22. local TriggerDefine = require("trigger.TriggerDefine")
  23. local TriggerLogic = require("trigger.TriggerLogic")
  24. local MIDDLE_FRIEND_FIGHT_MAP = 1001
  25. ---- 战斗相关缓存
  26. local MIDDLECOMMONCOMBAT =
  27. {
  28. --[[
  29. -- 请求者UID
  30. [nSrcUID] =
  31. {
  32. nDesUID, -- 请求对战角色UID
  33. nCombatType, -- 战斗类型
  34. nTime, -- 请求的时间
  35. }
  36. ]]
  37. }
  38. -- 内部写日志说明
  39. local function MiddleCommonLogic_WRITELOG(szText)
  40. Log.write(Log.LOGID_OSS_COMMON, "跨服通用管理"..szText)
  41. end
  42. -- 是否可以进行战斗
  43. local function MiddleCommonLogic_CanCombatType(nCombatType)
  44. if not MiddleDefine.MIDDLE_CAN_COMBAT[nCombatType] then
  45. return false
  46. end
  47. return true
  48. end
  49. -- 是否有缓存
  50. local function MiddleCommonLogic_IsCache(nSrcUID)
  51. return MIDDLECOMMONCOMBAT[nSrcUID]
  52. end
  53. -- 清理战斗缓存
  54. local function MiddleCommonLogic_ClearCache(nSrcUID)
  55. MIDDLECOMMONCOMBAT[nSrcUID] = nil
  56. end
  57. -- 设置战斗缓存
  58. local function MiddleCommonLogic_SetCache(nSrcUID, nDesUID, nCombatType)
  59. MIDDLECOMMONCOMBAT[nSrcUID] =
  60. {
  61. nDesUID = nDesUID,
  62. nCombatType = nCombatType,
  63. nTime = os.time()
  64. }
  65. end
  66. -- 弹失败提示
  67. local function MiddleCommonLogic_PopTips(human, nResult)
  68. if MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE == nResult then
  69. Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
  70. elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_TWO == nResult then
  71. Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOFANSHOU)
  72. elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE == nResult then
  73. Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
  74. elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR == nResult then
  75. Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
  76. end
  77. end
  78. ---------------------------- 点击玩家头像处理开始 ------------------------
  79. ---
  80. -- 跨服获取玩家信息(源->中心服)
  81. function MiddleCommonLogic_GetChatPlayInfo_LW(human, msg)
  82. local tMsgData = InnerMsg.lw.LW_CHAT_PLAYER_INFO
  83. tMsgData.nSrcUID = human.db._id
  84. tMsgData.nDesUID = msg.uuid
  85. tMsgData.nSrcServerID = Config.SVR_INDEX
  86. tMsgData.nDesServerID = msg.nServerIndex
  87. print("[MiddleCommonLogic_GetChatPlayInfo_LW] ", tMsgData.nSrcUID, tMsgData.nDesUID, tMsgData.nSrcServerID, tMsgData.nDesServerID)
  88. InnerMsg.sendMsg(0, tMsgData)
  89. end
  90. -- 跨服获取玩家信息(中心服->目标)
  91. function MiddleCommonLogic_GetChatPlayInfo_WL(fd, msg)
  92. local msgRet = Msg.gc.GC_CHAT_PLAYER_INFO
  93. if not RoleLogic.makePlayInfo(msgRet.data, msg.nDesUID) then
  94. MiddleCommonLogic_WRITELOG("跨服获取玩家数据失败 nDesUID = "..msg.nDesUID.." nSrcUID = "..msg.nSrcUID.." nSrcServerID = "..msg.nSrcServerID)
  95. return
  96. end
  97. -- 获取数据发回中心服
  98. local tMsgData = InnerMsg.lw.LW_CHAT_PLAYER_INFO_SEND
  99. tMsgData.nSrcUID = msg.nSrcUID
  100. tMsgData.nSrcServerID = msg.nSrcServerID
  101. tMsgData.tData = msgRet
  102. InnerMsg.sendMsg(0, tMsgData)
  103. print("[MiddleCommonLogic_GetChatPlayInfo_WL] 跨服获取玩家信息(中心服->目标)")
  104. end
  105. -- 获取到数据发送给请求的玩家
  106. function MiddleCommonLogic_SendChatPlayInfo_WL(fd, msg)
  107. local nSrcUID = msg.nSrcUID
  108. local human = ObjHuman.onlineUuid[nSrcUID]
  109. if not human then
  110. print("[MiddleCommonLogic_SendChatPlayInfo_WL] 玩家不在线直接返回")
  111. return
  112. end
  113. local msgRet = Msg.gc.GC_CHAT_PLAYER_INFO
  114. msgRet.data = msg.tData.data
  115. Msg.send(msgRet, human.fd)
  116. print("[MiddleCommonLogic_SendChatPlayInfo_WL] 获取到数据发送给请求的玩家")
  117. end
  118. ---------------------------- 点击玩家头像处理结束 ------------------------
  119. ---------------------------- 战斗相关开始 --------------------------------
  120. function MiddleCommonLogic_CombatBegin_LW(human, msg)
  121. if false == MiddleCommonLogic_CanCombatType(msg.combatType) then
  122. print("[MiddleCommonLogic_CombatBegin] 不用处理的对战类型直接返回 combatType = "..msg.combatType)
  123. return
  124. end
  125. local tCombatCache = MiddleCommonLogic_IsCache(human)
  126. if tCombatCache then
  127. local nNowTime = os.time()
  128. if tCombatCache.nTime + MiddleDefine.MIDDLE_GET_COMBAT_TIME <= nNowTime then
  129. MiddleCommonLogic_ClearCache(human.db._id)
  130. else
  131. return Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_WAIT)
  132. end
  133. end
  134. local args = Util.split(msg.param, "|")
  135. MiddleCommonLogic_SetCache(human.db._id, args[1], msg.combatType)
  136. local tMsgData = InnerMsg.lw.LW_COMBAT_GETINFO
  137. tMsgData.nSrcUID = human.db._id
  138. tMsgData.nDesUID = args[1]
  139. tMsgData.nSrcServerID = Config.SVR_INDEX
  140. tMsgData.nDesServerID = msg.nServerIndex
  141. tMsgData.nCombatType = msg.combatType
  142. InnerMsg.sendMsg(0, tMsgData)
  143. print("[MiddleCommonLogic_CombatBegin_LW] 发送给中心服 = ")
  144. end
  145. -- msg = WL_COMBAT_GETINFO
  146. function MiddleCommonLogic_CombatBegin_WL(fd, msg)
  147. local tMsgData = InnerMsg.lw.LW_COMBAT_GETINFO_SEND
  148. tMsgData.nSrcUID = msg.nSrcUID
  149. tMsgData.nSrcServerID = msg.nSrcServerID
  150. local nDesUID = msg.nDesUID
  151. local fakeHuman = CombatLogic.createCombatFakeHuman(nDesUID)
  152. if not fakeHuman then
  153. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE
  154. print("[MiddleCommonLogic_CombatBegin_WL] 没有获取到对应玩家")
  155. InnerMsg.sendMsg(0, tMsgData)
  156. return
  157. end
  158. local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
  159. if not combatHero or not next(combatHero) then
  160. print("[MiddleCommonLogic_CombatBegin_WL] 没有获取到对应玩家的英雄列表")
  161. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_TWO
  162. InnerMsg.sendMsg(0, tMsgData)
  163. return
  164. end
  165. local moduleFn = CombatLogic.getModule(msg.nCombatType)
  166. if not moduleFn or not moduleFn.getCombatObjList then
  167. print("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数")
  168. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE
  169. MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数 nCombatType = "..msg.nCombatType)
  170. InnerMsg.sendMsg(0, tMsgData)
  171. return
  172. else
  173. local args = {
  174. [1] = nDesUID
  175. }
  176. local objList, helpList, rolebase, formation,jiban = moduleFn.getCombatObjList(nil, CombatDefine.DEFEND_SIDE, args, msg.nCombatType)
  177. if objList then
  178. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_OK
  179. tMsgData.tObjList = objList
  180. tMsgData.tHelpList = helpList
  181. tMsgData.tRoleBase = rolebase
  182. tMsgData.formation = formation
  183. tMsgData.tJiBan = jiban
  184. InnerMsg.sendMsg(0, tMsgData)
  185. else
  186. print("[MiddleCommonLogic_CombatBegin_WL] 获取对战英雄列表失败")
  187. MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象列表失败 nCombatType = "..msg.nCombatType)
  188. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR
  189. InnerMsg.sendMsg(0, tMsgData)
  190. return
  191. end
  192. end
  193. end
  194. function MiddleCommonLogic_CombatBegin_SendWL(fd, msg)
  195. local nSrcUID = msg.nSrcUID
  196. local human = ObjHuman.onlineUuid[nSrcUID]
  197. if not human then
  198. print("[MiddleCommonLogic_CombatBegin_SendWL] 玩家不在线直接返回")
  199. return
  200. end
  201. print("[MiddleCommonLogic_CombatBegin_SendWL] 收到信息进行打印")
  202. local tCombatCache = MiddleCommonLogic_IsCache(nSrcUID)
  203. if not tCombatCache then
  204. print("[MiddleCommonLogic_CombatBegin_SendWL] 不存在对应的战斗缓存信息")
  205. return
  206. else
  207. local nNowTime = os.time()
  208. -- 已经超时
  209. if tCombatCache.nTime + MiddleDefine.MIDDLE_GET_COMBAT_TIME <= nNowTime then
  210. print("[MiddleCommonLogic_CombatBegin_SendWL] 已经超时")
  211. MiddleCommonLogic_ClearCache(nSrcUID)
  212. return
  213. end
  214. end
  215. local nResult = msg.nResult
  216. if MiddleDefine.MIDDLE_GET_COMBAT_OK ~= nResult then
  217. print("[MiddleCommonLogic_CombatBegin_SendWL] 获取的数据不正确,弹Tips并返回")
  218. MiddleCommonLogic_PopTips(human, nResult)
  219. return
  220. end
  221. local args = {
  222. defender = msg.tObjList,
  223. defHelp = msg.tHelpList,
  224. defRBase = msg.tRoleBase,
  225. defFormation = msg.formation,
  226. defJiban = msg.tJiBan,
  227. }
  228. YunYingLogic.onCallBack(human, "friendCombat", 1)
  229. TriggerLogic.PublishEvent(TriggerDefine.FRIEND_COMBAT, human.db._id, 1)
  230. print("[MiddleCommonLogic_CombatBegin_SendWL] 开始进行战斗")
  231. CombatLogic.combatBegin(human, MIDDLE_FRIEND_FIGHT_MAP, args, tCombatCache.nCombatType, tCombatCache.nDesUID)
  232. MiddleCommonLogic_ClearCache(nSrcUID)
  233. print("[MiddleCommonLogic_CombatBegin_SendWL] 战斗结束清理数据")
  234. end
  235. ---------------------------- 战斗相关结束 --------------------------------