MiddleCommonLogic.lua 12 KB

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