MiddleCommonLogic.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. -- msg = WL_COMBAT_GETINFO
  171. function MiddleCommonLogic_CombatBegin_WL(fd, msg)
  172. local tMsgData = InnerMsg.lw.LW_COMBAT_GETINFO_SEND
  173. tMsgData.nSrcUID = msg.nSrcUID
  174. tMsgData.nSrcServerID = msg.nSrcServerID
  175. local nCombatType = msg.nCombatType or CombatDefine.COMBAT_TYPE1
  176. local nDesUID = msg.nDesUID
  177. local fakeHuman = CombatLogic.createCombatFakeHuman(nDesUID)
  178. if not fakeHuman then
  179. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE
  180. print("[MiddleCommonLogic_CombatBegin_WL] 没有获取到对应玩家")
  181. InnerMsg.sendMsg(0, tMsgData)
  182. return
  183. end
  184. -- local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
  185. if CombatDefine.COMBAT_TYPE35 ~= nCombatType then
  186. local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, nCombatType, nil, true)
  187. if not combatHero or not next(combatHero) then
  188. print("[MiddleCommonLogic_CombatBegin_WL] 没有获取到对应玩家的英雄列表, 取默认战斗列表")
  189. combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
  190. if not combatHero or not next(combatHero) then
  191. print("[MiddleCommonLogic_CombatBegin_WL] 取默认战斗列表还是没有获取到英雄列表")
  192. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_TWO
  193. InnerMsg.sendMsg(0, tMsgData)
  194. return
  195. end
  196. end
  197. end
  198. local moduleFn = CombatLogic.getModule(msg.nCombatType)
  199. if not moduleFn or not moduleFn.getCombatObjList then
  200. print("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数")
  201. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE
  202. MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数 nCombatType = "..msg.nCombatType)
  203. InnerMsg.sendMsg(0, tMsgData)
  204. return
  205. else
  206. local args = {
  207. [1] = nDesUID
  208. }
  209. local objList, helpList, rolebase, formation,jiban, elfList = moduleFn.getCombatObjList(nil, CombatDefine.DEFEND_SIDE, args, msg.nCombatType, msg.extraArgs)
  210. if not objList and msg.nCombatType == CombatDefine.COMBAT_TYPE35 then
  211. if moduleFn.getCombatMonsterOutID then
  212. args[2] = msg.extraArgs.cityId
  213. local monsterOutId = moduleFn.getCombatMonsterOutID(nil, CombatDefine.DEFEND_SIDE, args)
  214. if monsterOutId then
  215. objList, helpList, rolebase, formation, jiban, elfList = CombatLogic.getMonsterObjList(monsterOutId)
  216. end
  217. end
  218. end
  219. if objList then
  220. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_OK
  221. tMsgData.tObjList = objList
  222. tMsgData.tHelpList = helpList
  223. tMsgData.tRoleBase = rolebase
  224. tMsgData.formation = formation
  225. tMsgData.tJiBan = jiban
  226. tMsgData.tElfList = elfList or {}
  227. InnerMsg.sendMsg(0, tMsgData)
  228. else
  229. print("[MiddleCommonLogic_CombatBegin_WL] 获取对战英雄列表失败")
  230. MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象列表失败 nCombatType = "..msg.nCombatType)
  231. tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR
  232. InnerMsg.sendMsg(0, tMsgData)
  233. return
  234. end
  235. end
  236. end
  237. function MiddleCommonLogic_CombatBegin_SendWL(fd, msg)
  238. local nSrcUID = msg.nSrcUID
  239. local human = ObjHuman.onlineUuid[nSrcUID]
  240. if not human then
  241. print("[MiddleCommonLogic_CombatBegin_SendWL] 玩家不在线直接返回")
  242. return
  243. end
  244. print("[MiddleCommonLogic_CombatBegin_SendWL] 收到信息进行打印")
  245. local tCombatCache = MiddleCommonLogic_IsCache(nSrcUID)
  246. if not tCombatCache then
  247. print("[MiddleCommonLogic_CombatBegin_SendWL] 不存在对应的战斗缓存信息")
  248. return
  249. else
  250. local nNowTime = os.time()
  251. -- 已经超时
  252. if tCombatCache.nTime + MiddleDefine.MIDDLE_GET_COMBAT_TIME <= nNowTime then
  253. print("[MiddleCommonLogic_CombatBegin_SendWL] 已经超时")
  254. MiddleCommonLogic_ClearCache(nSrcUID)
  255. return
  256. end
  257. end
  258. local nResult = msg.nResult
  259. if MiddleDefine.MIDDLE_GET_COMBAT_OK ~= nResult then
  260. print("[MiddleCommonLogic_CombatBegin_SendWL] 获取的数据不正确,弹Tips并返回")
  261. MiddleCommonLogic_PopTips(human, nResult)
  262. return
  263. end
  264. local args = {
  265. defender = msg.tObjList,
  266. defHelp = msg.tHelpList,
  267. defRBase = msg.tRoleBase,
  268. defFormation = msg.formation,
  269. defJiban = msg.tJiBan,
  270. defElfList = msg.tElfList,
  271. }
  272. YunYingLogic.onCallBack(human, "friendCombat", 1)
  273. TriggerLogic.PublishEvent(TriggerDefine.FRIEND_COMBAT, human.db._id, 1)
  274. print("[MiddleCommonLogic_CombatBegin_SendWL] 开始进行战斗")
  275. CombatLogic.combatBegin(human, MIDDLE_FRIEND_FIGHT_MAP, args, tCombatCache.nCombatType, tCombatCache.nDesUID)
  276. MiddleCommonLogic_ClearCache(nSrcUID)
  277. print("[MiddleCommonLogic_CombatBegin_SendWL] 战斗结束清理数据")
  278. end
  279. ---------------------------- 战斗相关结束 --------------------------------