CombatVideo.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. VIDEO_SINGLE_MAXCNT = 5 -- 个人记录条数上限
  17. TOWER_PER_MAXCNT = 3 -- 通天塔每层保留3条记录
  18. THRONE_RECORD_MAX_CNT = 10 -- 王者争霸记录条数
  19. BATTLE_RECORD_MAX_CNT = 1 -- 战役战斗记录条数
  20. -- 录像类型
  21. VIDEOTYPE_COMMON = 1 -- 个人记录
  22. VIDEOTYPE_JJC = 2 -- 单人竞技场
  23. VIDEOTYPE_TOWER = 3 -- 通天塔
  24. VIDEOTYPE_THRONE = 4 -- 王者争霸
  25. VIDEOTYPE_BATTLE = 5 -- 战役记录
  26. VIDEOTYPE_JJCLODDER = 6 -- 天梯赛
  27. --------------------------------------------- db ---------------------------------------------------
  28. local QueryByUuid = {_id = nil}
  29. local QueryByType = {videoType = nil, key = nil}
  30. function createCombatVideo(videoType, combatInfo, key,evolveCnt)
  31. if not combatInfo then return end
  32. if combatInfo.isVideo then return end
  33. if combatInfo.isVideoSave then return end
  34. -- 优化帧数据 降低
  35. CombatLogic.killFrames(combatInfo)
  36. local combatVideo = {}
  37. combatVideo.videoType = videoType
  38. combatVideo.key = key
  39. combatVideo.evolveCnt = evolveCnt
  40. combatVideo.combatInfo = Util.copyTable(combatInfo)
  41. LuaMongo.insert(DB.db_combat_video, combatVideo)
  42. return combatVideo
  43. end
  44. function updateCombatVideo(combatVideo)
  45. if not combatVideo._id then return end
  46. QueryByUuid._id = combatVideo._id
  47. LuaMongo.update(DB.db_combat_video, QueryByUuid, combatVideo)
  48. end
  49. function removeCombatVideo(videoUuid)
  50. if not videoUuid then return end
  51. QueryByUuid._id = videoUuid
  52. LuaMongo.remove(DB.db_combat_video, QueryByUuid)
  53. end
  54. function getCombatVideo(videoUuid)
  55. if not videoUuid then return end
  56. QueryByUuid._id = videoUuid
  57. local combatVideo = {}
  58. LuaMongo.find(DB.db_combat_video, QueryByUuid)
  59. if not LuaMongo.next(combatVideo) then
  60. return
  61. end
  62. return combatVideo
  63. end
  64. local COMBATVIDEO_LIST_CACHE = {}
  65. local FieldSimple = {["combatInfo.result"] = 0}
  66. function getCombatVideosByType(videoType, key)
  67. QueryByType.videoType = videoType
  68. if videoType == VIDEOTYPE_THRONE then
  69. FieldSimple = {}
  70. end
  71. QueryByType.key = key
  72. local len = 0
  73. Util.cleanTable(COMBATVIDEO_LIST_CACHE)
  74. LuaMongo.find(DB.db_combat_video, QueryByType, FieldSimple)
  75. while true do
  76. local combatVideo = {}
  77. if not LuaMongo.next(combatVideo) then
  78. break
  79. end
  80. len = len + 1
  81. COMBATVIDEO_LIST_CACHE[len] = combatVideo
  82. end
  83. return COMBATVIDEO_LIST_CACHE
  84. end
  85. ----------------------------------------------- msg -------------------------------------------------------
  86. -- 个人录像数目
  87. function getCombatVideoCnt(human)
  88. if not human.db.combatVideos then
  89. return 0
  90. end
  91. return #human.db.combatVideos
  92. end
  93. -- 获取录像索引
  94. function getCombatVideoIndex(human, videoUuid)
  95. if not human.db.combatVideos then
  96. return
  97. end
  98. for index, vuuid in ipairs(human.db.combatVideos) do
  99. if vuuid == videoUuid then
  100. return index
  101. end
  102. end
  103. end
  104. -- 保存个人录像
  105. function saveCombatVideo(human, isShare)
  106. local combatInfo = human.combat
  107. if not combatInfo then return end
  108. if combatInfo.isVideo then
  109. return Broadcast.sendErr(human, Lang.COMBAT_ERR_IS_VIDEO)
  110. end
  111. if combatInfo.videoUuid then
  112. if not isShare then
  113. Broadcast.sendErr(human, Lang.SAVE_SUCCESS)
  114. end
  115. return combatInfo.videoUuid
  116. end
  117. local saveNum = getCombatVideoCnt(human)
  118. if saveNum >= VIDEO_SINGLE_MAXCNT then
  119. local videoUuid = human.db.combatVideos[1]
  120. deleteCombatVideo(human, videoUuid)
  121. end
  122. local combatVideo = createCombatVideo(VIDEOTYPE_COMMON, combatInfo, human.db._id)
  123. if not combatVideo then return end
  124. if not combatVideo._id then return end
  125. human.db.combatVideos = human.db.combatVideos or {}
  126. local newIndex = #human.db.combatVideos + 1
  127. human.db.combatVideos[newIndex] = combatVideo._id
  128. combatInfo.videoUuid = combatVideo._id
  129. if not isShare then
  130. Broadcast.sendErr(human, Lang.SAVE_SUCCESS)
  131. end
  132. return combatVideo._id
  133. end
  134. -- 查看录像
  135. function sendCombatVideoQuery(human)
  136. local msgRet = Msg.gc.GC_COMBAT_VIDEO_QUERY
  137. local videoCnt = getCombatVideoCnt(human)
  138. msgRet.list[0] = 0
  139. for i = 1, videoCnt do
  140. local videoUuid = human.db.combatVideos[i]
  141. local combatVideo = getCombatVideo(videoUuid)
  142. local combatInfo = combatVideo and combatVideo.combatInfo
  143. if combatInfo then
  144. msgRet.list[0] = msgRet.list[0] + 1
  145. local net = msgRet.list[msgRet.list[0]]
  146. net.videoUuid = combatVideo._id
  147. net.combatTime = combatInfo.time
  148. net.isWin = combatInfo.isWin and CombatDefine.RESULT_WIN or CombatDefine.RESULT_FAIL
  149. net.type = combatInfo.type
  150. RoleLogic.makeRoleBase(combatInfo.attacker, net.atkRoleBase)
  151. RoleLogic.makeRoleBase(combatInfo.defender, net.defRoleBase)
  152. end
  153. end
  154. --Msg.trace(msgRet)
  155. Msg.send(msgRet, human.fd)
  156. end
  157. -- 删除战斗记录
  158. function deleteCombatVideo(human, videoUuid)
  159. if not videoUuid or videoUuid == "" then return end
  160. local videoIndex = getCombatVideoIndex(human, videoUuid)
  161. if not videoIndex then return end
  162. local combatVideo = getCombatVideo(videoUuid)
  163. if combatVideo then
  164. removeCombatVideo(combatVideo._id)
  165. end
  166. table.remove(human.db.combatVideos, videoIndex)
  167. ObjHuman.save(human)
  168. sendCombatVideoQuery(human)
  169. end
  170. -- 战斗回放
  171. function lookCombatVideo(human, videoUuid, lookType)
  172. -- if CombatLogic.isCombating(human) then
  173. -- return Broadcast.sendErr(human, Lang.COMBAT_VIDEO_LOOK_ERR_ING)
  174. -- end
  175. if not videoUuid or videoUuid == "" then return end
  176. local combatVideo = getCombatVideo(videoUuid)
  177. if not combatVideo then
  178. return Broadcast.sendErr(human, Lang.COMBAT_ERR_NOT_VIDEO)
  179. end
  180. local combatInfo = combatVideo.combatInfo
  181. combatInfo.isVideo = true
  182. combatInfo.videoUuid = videoUuid
  183. combatInfo.lookType = lookType
  184. human.combat = combatInfo
  185. CombatLogic.sendCombatData(human, combatInfo)
  186. CombatLogic.sendCombatFinish(human, combatInfo)
  187. end
  188. -- 保存通天塔记录
  189. function saveTowerVideo(towerId, combatInfo)
  190. if not towerId or not combatInfo then return end
  191. local list = getCombatVideosByType(VIDEOTYPE_TOWER, towerId)
  192. if #list < TOWER_PER_MAXCNT then
  193. createCombatVideo(VIDEOTYPE_TOWER, combatInfo, towerId)
  194. else
  195. local selectVideo = nil
  196. for _, combatVideo in ipairs(list) do
  197. local attacker = combatVideo.combatInfo.attacker
  198. local sattacker = selectVideo and selectVideo.combatInfo.attacker
  199. if sattacker == nil or attacker.lv > sattacker.lv then
  200. selectVideo = combatVideo
  201. end
  202. end
  203. selectVideo.combatInfo = combatInfo
  204. updateCombatVideo(selectVideo)
  205. end
  206. end
  207. -- 竞技计数减一
  208. function delJJCVideoCnt(videoUuid)
  209. local combatVideo = getCombatVideo(videoUuid)
  210. if not combatVideo then return end
  211. if combatVideo.videoType ~= VIDEOTYPE_JJC then
  212. return
  213. end
  214. combatVideo.key = (combatVideo.key or 0) - 1
  215. if combatVideo.key > 0 then
  216. updateCombatVideo(combatVideo)
  217. else
  218. removeCombatVideo(combatVideo._id)
  219. end
  220. end
  221. -- 清空竞技场记录
  222. function cleanJJCVideo()
  223. QueryByType.videoType = VIDEOTYPE_JJC
  224. LuaMongo.remove(DB.db_combat_video, QueryByType)
  225. end
  226. -- 王者争霸记录
  227. function saveThroneVideo(id, combatInfo,evolveCnt)
  228. local list = getCombatVideosByType(VIDEOTYPE_THRONE, id)
  229. if #list < THRONE_RECORD_MAX_CNT then
  230. createCombatVideo(VIDEOTYPE_THRONE, combatInfo, id,evolveCnt)
  231. else
  232. local selectVideo = {}
  233. for _, combatVideo in ipairs(list) do
  234. local time = combatVideo.combatInfo.time
  235. local stime = selectVideo.combatInfo and selectVideo.combatInfo.time
  236. if stime == nil or time < stime then
  237. selectVideo = combatVideo
  238. end
  239. end
  240. selectVideo.combatInfo = combatInfo
  241. selectVideo.evolveCnt = evolveCnt
  242. updateCombatVideo(selectVideo)
  243. end
  244. end
  245. -- 战役记录
  246. function saveBattleVideo(uuid, combatInfo)
  247. local list = getCombatVideosByType(VIDEOTYPE_BATTLE, uuid)
  248. if #list < BATTLE_RECORD_MAX_CNT then
  249. local selectVideo = nil
  250. selectVideo = createCombatVideo(VIDEOTYPE_BATTLE, combatInfo, uuid)
  251. return selectVideo._id
  252. else
  253. local selectVideo = nil
  254. for _, combatVideo in ipairs(list) do
  255. local humanUuid = combatVideo.uuid
  256. local sUuid = selectVideo and selectVideo.uuid
  257. if sUuid == nil or sUuid == humanUuid then
  258. selectVideo = combatVideo
  259. end
  260. end
  261. selectVideo.combatInfo = combatInfo
  262. updateCombatVideo(selectVideo)
  263. return selectVideo._id
  264. end
  265. end
  266. -- 天梯赛竞技计数减一
  267. function delJJCLadderVideoCnt(videoUuid)
  268. local combatVideo = getCombatVideo(videoUuid)
  269. if not combatVideo then return end
  270. if combatVideo.videoType ~= VIDEOTYPE_JJCLODDER then
  271. return
  272. end
  273. combatVideo.key = (combatVideo.key or 0) - 1
  274. if combatVideo.key > 0 then
  275. updateCombatVideo(combatVideo)
  276. else
  277. removeCombatVideo(combatVideo._id)
  278. end
  279. end
  280. -- 清空天梯赛记录
  281. function cleanJJCLadderVideo()
  282. QueryByType.videoType = VIDEOTYPE_JJCLODDER
  283. LuaMongo.remove(DB.db_combat_video, QueryByType)
  284. end