MiddleCommonLogic.lua 9.8 KB

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