BossDBLogic.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ----------------------------------------
  2. -- boss 挑战模块DB
  3. ----------------------------------------
  4. local LuaMongo = _G.lua_mongo
  5. local DB = require("common.DB")
  6. local CombatDefine = require("combat.CombatDefine")
  7. local CombatLogic = require("combat.CombatLogic")
  8. local CombatObj = require("combat.CombatObj")
  9. local RoleDefine = require("role.RoleDefine")
  10. local QueryByUUID = {_id = nil}
  11. local QueryByOwnUuid = {ownUuid = nil, bossType = nil}
  12. BOSS_TYPE_UNION = 1 --公会副本BOSS
  13. DEFAULT_HP_MAX = 100
  14. function createBossData(bossType, ownUuid, monsterOutID, mapID, cbParam)
  15. local objList = CombatLogic.getMonsterObjList(monsterOutID, CombatDefine.COMBAT_OBJ_TYPE3)
  16. if not objList then return end
  17. local bossData = {}
  18. bossData.bossType = bossType -- boss类型
  19. bossData.ownUuid = ownUuid -- 归属Uuid,可能是玩家Uuid,也可能是公会
  20. bossData.monsterOutID = monsterOutID -- 怪物组Id
  21. bossData.mapID = mapID -- 地图
  22. bossData.cbParam = cbParam -- boss额外信息
  23. bossData.record = nil -- 伤害记录[uuid] = hurt
  24. bossData.objList = {} -- 怪物列表 剩余血量等 [index] = obj
  25. LuaMongo.insert(DB.db_boss, bossData)
  26. return bossData
  27. end
  28. function updateBossData(bossData)
  29. if not bossData._id then return end
  30. QueryByUUID._id = bossData._id
  31. LuaMongo.update(DB.db_boss, QueryByUUID, bossData)
  32. end
  33. local BOSS_DATA_LIST = {}
  34. function getBossDatas(bossType, ownUuid)
  35. QueryByOwnUuid.ownUuid = ownUuid
  36. QueryByOwnUuid.bossType = bossType
  37. for k in pairs(BOSS_DATA_LIST) do
  38. BOSS_DATA_LIST[k] = nil
  39. end
  40. LuaMongo.find(DB.db_boss, QueryByOwnUuid)
  41. while true do
  42. local bossData = {}
  43. if not LuaMongo.next(bossData) then
  44. break
  45. end
  46. BOSS_DATA_LIST[#BOSS_DATA_LIST + 1] = bossData
  47. end
  48. return BOSS_DATA_LIST
  49. end
  50. function getBossData(uuid)
  51. if not uuid then return end
  52. QueryByUUID._id = uuid
  53. LuaMongo.find(DB.db_boss, QueryByUUID)
  54. local bossData = {}
  55. if not LuaMongo.next(bossData) then return end
  56. return bossData
  57. end
  58. function removeBossData(uuid)
  59. if not uuid then return end
  60. QueryByUUID._id = uuid
  61. LuaMongo.remove(DB.db_boss, QueryByUUID)
  62. end
  63. --------------------------------------------------------------------
  64. -- boss是否死亡
  65. function isBossDie(bossData)
  66. if not bossData then return true end
  67. if not bossData.objList then return true end
  68. if not next(bossData.objList) then return end
  69. for _, obj in pairs(bossData.objList) do
  70. if obj.hp > 0 then
  71. return
  72. end
  73. end
  74. return true
  75. end
  76. -- 获取boss hp
  77. function getBossHp(bossData, index)
  78. local obj = bossData.objList[index]
  79. if not obj then return DEFAULT_HP_MAX end
  80. return obj.hp or DEFAULT_HP_MAX
  81. end
  82. function getBossHpMax(bossData, index)
  83. local obj = bossData.objList[index]
  84. if not obj then return DEFAULT_HP_MAX end
  85. if obj.hp and not obj.hpMax then return 0 end
  86. return obj.hpMax or DEFAULT_HP_MAX
  87. end
  88. function getBossHpSum(bossData)
  89. local sum = 0
  90. local cnt = 0
  91. for index, obj in pairs(bossData.objList) do
  92. sum = sum + getBossHp(bossData, index)
  93. cnt = cnt + 1
  94. end
  95. return (cnt > 0) and sum or DEFAULT_HP_MAX
  96. end
  97. function getBossHpMaxSum(bossData)
  98. local sum = 0
  99. local cnt = 0
  100. for index, obj in pairs(bossData.objList) do
  101. sum = sum + getBossHpMax(bossData, index)
  102. cnt = cnt + 1
  103. end
  104. return (cnt > 0) and sum or DEFAULT_HP_MAX
  105. end
  106. -- 刷新血量
  107. function updateBossHp(bossData, index, combatObj)
  108. local obj = bossData.objList[index]
  109. if not obj then
  110. obj = {}
  111. obj.hpMax = CombatObj.getHpMax(combatObj)
  112. obj.hp = obj.hpMax
  113. bossData.objList[index] = obj
  114. end
  115. if not combatObj then
  116. obj.hp = 0
  117. return 0
  118. end
  119. local hurt = math.max(combatObj.initHp - combatObj.hp, 0)
  120. obj.hp = math.max(obj.hp - hurt, 0)
  121. return hurt
  122. end
  123. function updateBossHpByHurt(bossData, index, hurt, combatObj)
  124. local obj = bossData.objList[index]
  125. if not obj then
  126. obj = {}
  127. obj.hpMax = CombatObj.getHpMax(combatObj)
  128. obj.hp = obj.hpMax
  129. bossData.objList[index] = obj
  130. end
  131. obj = bossData.objList[index]
  132. if obj then
  133. local realHurt = math.min(obj.hp, hurt)
  134. obj.hp = math.max(obj.hp - hurt, 0)
  135. return realHurt
  136. end
  137. return 0
  138. end
  139. function updateBossHpSaoDang(bossData, index, hurt)
  140. local obj = bossData.objList[index]
  141. if obj then
  142. local realHurt = math.min(obj.hp, hurt)
  143. obj.hp = math.max(obj.hp - hurt, 0)
  144. return realHurt
  145. end
  146. return 0
  147. end
  148. -- 伤害记录
  149. function updateBossRecord(bossData, uuid, hurt)
  150. bossData.record = bossData.record or {}
  151. bossData.record[uuid] = bossData.record[uuid] or {}
  152. local record = bossData.record[uuid]
  153. record.hurt = (record.hurt or 0) + hurt
  154. record.time = os.time()
  155. record.uuid = uuid
  156. end
  157. -- 点赞记录
  158. function updateBossRecordLike(bossData, target)
  159. bossData.record = bossData.record or {}
  160. bossData.record[target] = bossData.record[target] or {}
  161. local record = bossData.record[target]
  162. record.like = record.like or 0
  163. record.like = record.like + 1
  164. end
  165. local function cmpHurt(a, b)
  166. if a.hurt ~= b.hurt then
  167. return a.hurt > b.hurt
  168. end
  169. return a.time < b.time
  170. end
  171. local HURT_BOARD_LIST = {}
  172. function getHurtBoard(bossData)
  173. if not bossData then return end
  174. if not bossData.record then return end
  175. for k in pairs(HURT_BOARD_LIST) do
  176. HURT_BOARD_LIST[k] = nil
  177. end
  178. local cnt = 0
  179. for uuid, record in pairs(bossData.record) do
  180. cnt = cnt + 1
  181. HURT_BOARD_LIST[cnt] = record
  182. end
  183. if cnt > 1 then
  184. table.sort(HURT_BOARD_LIST, cmpHurt)
  185. end
  186. return HURT_BOARD_LIST
  187. end