CombatVideo.lua 13 KB

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