CombatVideo.lua 14 KB

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