MiddleCommonLogic.lua 9.9 KB

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