CombatVideo.lua 8.9 KB

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