CombatVideo.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. ----------------------------------------------
  2. -- 战斗录像 相关
  3. -- db.combatVideos [index] = videoUuid
  4. ----------------------------------------------
  5. local LuaMongo = _G.lua_mongo
  6. local Config = require("Config")
  7. local Msg = require("core.Msg")
  8. local ObjHuman = require("core.ObjHuman")
  9. local DB = require("common.DB")
  10. local Util = require("common.Util")
  11. local Lang = require("common.Lang")
  12. local CombatDefine = require("combat.CombatDefine")
  13. local CombatLogic = require("combat.CombatLogic")
  14. local Broadcast = require("broadcast.Broadcast")
  15. local RoleLogic = require("role.RoleLogic")
  16. local InnerMsg = require("core.InnerMsg")
  17. local MiddleManager = require("middle.MiddleManager")
  18. VIDEO_SINGLE_MAXCNT = 5 -- 个人记录条数上限
  19. TOWER_PER_MAXCNT = 3 -- 通天塔每层保留3条记录
  20. THRONE_RECORD_MAX_CNT = 10 -- 王者争霸记录条数
  21. BATTLE_RECORD_MAX_CNT = 1 -- 战役战斗记录条数
  22. GODSAREA_RECORD_MAX_CNT = 5 -- 诸神圣域每个排名战斗记录条数
  23. -- 录像类型
  24. VIDEOTYPE_COMMON = 1 -- 个人记录
  25. VIDEOTYPE_JJC = 2 -- 单人竞技场
  26. VIDEOTYPE_TOWER = 3 -- 通天塔
  27. VIDEOTYPE_THRONE = 4 -- 王者争霸
  28. VIDEOTYPE_BATTLE = 5 -- 战役记录
  29. VIDEOTYPE_JJCLODDER = 6 -- 天梯赛
  30. VIDEOTYPE_GODSAREA = 7 -- 诸神圣域
  31. --------------------------------------------- db ---------------------------------------------------
  32. local QueryByUuid = {_id = nil}
  33. local QueryByType = {videoType = nil, key = nil}
  34. function createCombatVideo(videoType, combatInfo, key,evolveCnt)
  35. if not combatInfo then return end
  36. if combatInfo.isVideo then return end
  37. if combatInfo.isVideoSave then return end
  38. -- 优化帧数据 降低
  39. CombatLogic.killFrames(combatInfo)
  40. local combatVideo = {}
  41. combatVideo.videoType = videoType
  42. combatVideo.key = key
  43. combatVideo.evolveCnt = evolveCnt
  44. combatVideo.combatInfo = Util.copyTable(combatInfo)
  45. LuaMongo.insert(DB.db_combat_video, combatVideo)
  46. return combatVideo
  47. end
  48. function updateCombatVideo(combatVideo)
  49. if not combatVideo._id then return end
  50. QueryByUuid._id = combatVideo._id
  51. LuaMongo.update(DB.db_combat_video, QueryByUuid, combatVideo)
  52. end
  53. function removeCombatVideo(videoUuid)
  54. if not videoUuid then return end
  55. QueryByUuid._id = videoUuid
  56. LuaMongo.remove(DB.db_combat_video, QueryByUuid)
  57. end
  58. function getCombatVideo(videoUuid)
  59. if not videoUuid then return end
  60. QueryByUuid._id = videoUuid
  61. local combatVideo = {}
  62. LuaMongo.find(DB.db_combat_video, QueryByUuid)
  63. if not LuaMongo.next(combatVideo) then
  64. return
  65. end
  66. return combatVideo
  67. end
  68. local COMBATVIDEO_LIST_CACHE = {}
  69. local FieldSimple = {["combatInfo.result"] = 0}
  70. function getCombatVideosByType(videoType, key)
  71. QueryByType.videoType = videoType
  72. if videoType == VIDEOTYPE_THRONE or videoType == VIDEOTYPE_GODSAREA then
  73. FieldSimple = {}
  74. end
  75. QueryByType.key = key
  76. local len = 0
  77. Util.cleanTable(COMBATVIDEO_LIST_CACHE)
  78. LuaMongo.find(DB.db_combat_video, QueryByType, FieldSimple)
  79. while true do
  80. local combatVideo = {}
  81. if not LuaMongo.next(combatVideo) then
  82. break
  83. end
  84. len = len + 1
  85. COMBATVIDEO_LIST_CACHE[len] = combatVideo
  86. end
  87. return COMBATVIDEO_LIST_CACHE
  88. end
  89. ----------------------------------------------- msg -------------------------------------------------------
  90. -- 个人录像数目
  91. function getCombatVideoCnt(human)
  92. if not human.db.combatVideos then
  93. return 0
  94. end
  95. return #human.db.combatVideos
  96. end
  97. -- 获取录像索引
  98. function getCombatVideoIndex(human, videoUuid)
  99. if not human.db.combatVideos then
  100. return
  101. end
  102. for index, vuuid in ipairs(human.db.combatVideos) do
  103. if vuuid == videoUuid then
  104. return index
  105. end
  106. end
  107. end
  108. -- 保存个人录像
  109. function saveCombatVideo(human, isShare)
  110. local combatInfo = human.combat
  111. if not combatInfo then return end
  112. if combatInfo.isVideo then
  113. return Broadcast.sendErr(human, Lang.COMBAT_ERR_IS_VIDEO)
  114. end
  115. if combatInfo.videoUuid then
  116. if not isShare then
  117. Broadcast.sendErr(human, Lang.SAVE_SUCCESS)
  118. end
  119. return combatInfo.videoUuid
  120. end
  121. local saveNum = getCombatVideoCnt(human)
  122. if saveNum >= VIDEO_SINGLE_MAXCNT then
  123. local videoUuid = human.db.combatVideos[1]
  124. deleteCombatVideo(human, videoUuid)
  125. end
  126. local combatVideo = createCombatVideo(VIDEOTYPE_COMMON, combatInfo, human.db._id)
  127. if not combatVideo then return end
  128. if not combatVideo._id then return end
  129. human.db.combatVideos = human.db.combatVideos or {}
  130. local newIndex = #human.db.combatVideos + 1
  131. human.db.combatVideos[newIndex] = combatVideo._id
  132. combatInfo.videoUuid = combatVideo._id
  133. if not isShare then
  134. Broadcast.sendErr(human, Lang.SAVE_SUCCESS)
  135. end
  136. return combatVideo._id
  137. end
  138. -- 查看录像
  139. function sendCombatVideoQuery(human)
  140. local msgRet = Msg.gc.GC_COMBAT_VIDEO_QUERY
  141. local videoCnt = getCombatVideoCnt(human)
  142. msgRet.list[0] = 0
  143. for i = 1, videoCnt do
  144. local videoUuid = human.db.combatVideos[i]
  145. local combatVideo = getCombatVideo(videoUuid)
  146. local combatInfo = combatVideo and combatVideo.combatInfo
  147. if combatInfo then
  148. msgRet.list[0] = msgRet.list[0] + 1
  149. local net = msgRet.list[msgRet.list[0]]
  150. net.videoUuid = combatVideo._id
  151. net.combatTime = combatInfo.time
  152. net.isWin = combatInfo.isWin and CombatDefine.RESULT_WIN or CombatDefine.RESULT_FAIL
  153. net.type = combatInfo.type
  154. RoleLogic.makeRoleBase(combatInfo.attacker, net.atkRoleBase)
  155. RoleLogic.makeRoleBase(combatInfo.defender, net.defRoleBase)
  156. end
  157. end
  158. --Msg.trace(msgRet)
  159. Msg.send(msgRet, human.fd)
  160. end
  161. -- 删除战斗记录
  162. function deleteCombatVideo(human, videoUuid)
  163. if not videoUuid or videoUuid == "" then return end
  164. local videoIndex = getCombatVideoIndex(human, videoUuid)
  165. if not videoIndex then return end
  166. local combatVideo = getCombatVideo(videoUuid)
  167. if combatVideo then
  168. removeCombatVideo(combatVideo._id)
  169. end
  170. table.remove(human.db.combatVideos, videoIndex)
  171. ObjHuman.save(human)
  172. sendCombatVideoQuery(human)
  173. end
  174. -- 战斗回放
  175. function lookCombatVideo(human, videoUuid, lookType)
  176. -- if CombatLogic.isCombating(human) then
  177. -- return Broadcast.sendErr(human, Lang.COMBAT_VIDEO_LOOK_ERR_ING)
  178. -- end
  179. if not videoUuid or videoUuid == "" then return end
  180. local combatVideo = getCombatVideo(videoUuid)
  181. if not combatVideo then
  182. -- return Broadcast.sendErr(human, Lang.COMBAT_ERR_NOT_VIDEO)
  183. local args = {
  184. playerUid = human.db._id,
  185. lookType = lookType,
  186. opType = 1,
  187. }
  188. NS_Video_Query(videoUuid, args)
  189. return
  190. end
  191. local combatInfo = combatVideo.combatInfo
  192. combatInfo.isVideo = true
  193. combatInfo.videoUuid = videoUuid
  194. combatInfo.lookType = lookType
  195. human.combat = combatInfo
  196. CombatLogic.sendCombatData(human, combatInfo)
  197. CombatLogic.sendCombatFinish(human, combatInfo)
  198. end
  199. -- 保存通天塔记录
  200. function saveTowerVideo(towerId, combatInfo)
  201. if not towerId or not combatInfo then return end
  202. local list = getCombatVideosByType(VIDEOTYPE_TOWER, towerId)
  203. if #list < TOWER_PER_MAXCNT then
  204. createCombatVideo(VIDEOTYPE_TOWER, combatInfo, towerId)
  205. else
  206. local selectVideo = nil
  207. for _, combatVideo in ipairs(list) do
  208. local attacker = combatVideo.combatInfo.attacker
  209. local sattacker = selectVideo and selectVideo.combatInfo.attacker
  210. if sattacker == nil or attacker.lv > sattacker.lv then
  211. selectVideo = combatVideo
  212. end
  213. end
  214. selectVideo.combatInfo = combatInfo
  215. updateCombatVideo(selectVideo)
  216. end
  217. end
  218. -- 竞技计数减一
  219. function delJJCVideoCnt(videoUuid)
  220. local combatVideo = getCombatVideo(videoUuid)
  221. if not combatVideo then return end
  222. if combatVideo.videoType ~= VIDEOTYPE_JJC then
  223. return
  224. end
  225. combatVideo.key = (combatVideo.key or 0) - 1
  226. if combatVideo.key > 0 then
  227. updateCombatVideo(combatVideo)
  228. else
  229. removeCombatVideo(combatVideo._id)
  230. end
  231. end
  232. -- 清空竞技场记录
  233. function cleanJJCVideo()
  234. QueryByType.videoType = VIDEOTYPE_JJC
  235. LuaMongo.remove(DB.db_combat_video, QueryByType)
  236. end
  237. -- 王者争霸记录
  238. function saveThroneVideo(id, combatInfo,evolveCnt)
  239. local list = getCombatVideosByType(VIDEOTYPE_THRONE, id)
  240. if #list < THRONE_RECORD_MAX_CNT then
  241. createCombatVideo(VIDEOTYPE_THRONE, combatInfo, id,evolveCnt)
  242. else
  243. local selectVideo = {}
  244. for _, combatVideo in ipairs(list) do
  245. local time = combatVideo.combatInfo.time
  246. local stime = selectVideo.combatInfo and selectVideo.combatInfo.time
  247. if stime == nil or time < stime then
  248. selectVideo = combatVideo
  249. end
  250. end
  251. selectVideo.combatInfo = combatInfo
  252. selectVideo.evolveCnt = evolveCnt
  253. updateCombatVideo(selectVideo)
  254. end
  255. end
  256. -- 战役记录
  257. function saveBattleVideo(uuid, combatInfo)
  258. local list = getCombatVideosByType(VIDEOTYPE_BATTLE, uuid)
  259. if #list < BATTLE_RECORD_MAX_CNT then
  260. local selectVideo = nil
  261. selectVideo = createCombatVideo(VIDEOTYPE_BATTLE, combatInfo, uuid)
  262. return selectVideo._id
  263. else
  264. local selectVideo = nil
  265. for _, combatVideo in ipairs(list) do
  266. local humanUuid = combatVideo.uuid
  267. local sUuid = selectVideo and selectVideo.uuid
  268. if sUuid == nil or sUuid == humanUuid then
  269. selectVideo = combatVideo
  270. end
  271. end
  272. selectVideo.combatInfo = combatInfo
  273. updateCombatVideo(selectVideo)
  274. return selectVideo._id
  275. end
  276. end
  277. -- 天梯赛竞技计数减一
  278. function delJJCLadderVideoCnt(videoUuid)
  279. local combatVideo = getCombatVideo(videoUuid)
  280. if not combatVideo then return end
  281. if combatVideo.videoType ~= VIDEOTYPE_JJCLODDER then
  282. return
  283. end
  284. combatVideo.key = (combatVideo.key or 0) - 1
  285. if combatVideo.key > 0 then
  286. updateCombatVideo(combatVideo)
  287. else
  288. removeCombatVideo(combatVideo._id)
  289. end
  290. end
  291. -- 清空天梯赛记录
  292. function cleanJJCLadderVideo()
  293. QueryByType.videoType = VIDEOTYPE_JJCLODDER
  294. LuaMongo.remove(DB.db_combat_video, QueryByType)
  295. end
  296. ---------------------------------------------普通服处理------------------------------------------
  297. local function lookCSCombatVideo(combatVideo, args)
  298. local combatInfo = combatVideo.combatInfo
  299. combatInfo.isVideo = true
  300. combatInfo.videoUuid = combatVideo._id
  301. combatInfo.lookType = args.lookType
  302. local human = ObjHuman.onlineUuid[args.playerUid]
  303. if not human then
  304. return
  305. end
  306. human.combat = combatInfo
  307. CombatLogic.sendCombatData(human, combatInfo)
  308. CombatLogic.sendCombatFinish(human, combatInfo)
  309. end
  310. -- 向跨服查询战斗录像数据
  311. function NS_Video_Query(videoUuid, extraArgs)
  312. if not videoUuid then
  313. return
  314. end
  315. local msgData = InnerMsg.lw.LW_COMBAT_VIDEO_QUERY
  316. msgData.sourceServerId = Config.SVR_INDEX
  317. msgData.videoUuid = videoUuid or ""
  318. msgData.extraArgs = extraArgs
  319. InnerMsg.sendMsg(0, msgData)
  320. end
  321. -- 收到跨服发送的查询结果
  322. function NS_Video_Query_Result(msg)
  323. if msg.res ~= 0 then
  324. return
  325. end
  326. local extraArgs = msg.extraArgs
  327. if extraArgs.opType == 1 then
  328. lookCSCombatVideo(msg.videoData, extraArgs)
  329. end
  330. end
  331. -- 把战斗录像数据保存到跨服
  332. function NS_Video_Save(vieoType, combatInfo, extraArgs)
  333. if not vieoType or not combatInfo then
  334. return
  335. end
  336. local msgData = InnerMsg.lw.LW_COMBAT_VIDEO_SAVE
  337. -- msgData.sourceServerId = Config.SVR_INDEX
  338. CombatLogic.killFrames(combatInfo)
  339. msgData.videoType = vieoType
  340. msgData.videoData = combatInfo
  341. msgData.extraArgs = extraArgs or {}
  342. InnerMsg.sendMsg(0, msgData)
  343. end
  344. -------------------------------------------------------------------------------------------
  345. ---------------------------------------------跨服处理---------------------------------------
  346. local function saveGodsAreaVideo(combatInfo, extraArgs)
  347. local rank = extraArgs.rank
  348. local list = getCombatVideosByType(VIDEOTYPE_GODSAREA, rank)
  349. if #list < GODSAREA_RECORD_MAX_CNT then
  350. createCombatVideo(VIDEOTYPE_GODSAREA, combatInfo, rank)
  351. else
  352. local selectVideo = {}
  353. for _, combatVideo in ipairs(list) do
  354. local time = combatVideo.combatInfo.time
  355. local stime = selectVideo.combatInfo and selectVideo.combatInfo.time
  356. if stime == nil or time < stime then
  357. selectVideo = combatVideo
  358. end
  359. end
  360. selectVideo.combatInfo = combatInfo
  361. updateCombatVideo(selectVideo)
  362. end
  363. end
  364. -- 普通服向跨服查询录像数据
  365. function CS_Video_Query(msg)
  366. if _G.is_middle ~= true then return end
  367. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  368. local msgData = InnerMsg.wl.WL_COMBAT_VIDEO_QUERY
  369. msgData.res = -1
  370. msgData.extraArgs = msg.extraArgs
  371. local combatVideo = getCombatVideo(msg.videoUuid)
  372. if combatVideo then
  373. msgData.res = 0
  374. msgData.videoData = combatVideo
  375. end
  376. InnerMsg.sendMsg(fd, msgData)
  377. end
  378. -- 普通服通知跨服保存录像数据
  379. function CS_Video_Save(msg)
  380. if _G.is_middle ~= true then return end
  381. if msg.videoType == VIDEOTYPE_GODSAREA then
  382. saveGodsAreaVideo(msg.videoData, msg.extraArgs)
  383. end
  384. end
  385. -------------------------------------------------------------------------------------------