MiddleCommonLogic.lua 12 KB

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