RecycleItem.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. -- 收纳箱(回收道具)
  2. --db
  3. --[=[
  4. human.db.recycleData = {
  5. level = nil,
  6. exp = nil,
  7. }
  8. ]=]--
  9. local Msg = require("core.Msg")
  10. local Lang = require("common.Lang")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local Util = require("common.Util")
  13. local BagLogic = require("bag.BagLogic")
  14. local RoleAttr = require("role.RoleAttr")
  15. local RoleDefine = require("role.RoleDefine")
  16. local ObjHuman = require("core.ObjHuman")
  17. local RecycleConfig = require("excel.recycleItem").RecycleLvList
  18. local ItemConfig = require("excel.item").item
  19. -- local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  20. -- local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  21. local LOG_TAG = "RecycleItem" -- 本系统的日志标识
  22. local OPEN_COND_LV = 150 -- 开启功能需要的等级
  23. local function initRecycleData(human)
  24. human.db.recycleData = { level = 0, exp = 0 }
  25. end
  26. local function getRecycleData(human)
  27. return human.db.recycleData
  28. end
  29. local function updateRecycleData(human, newLv, newExp)
  30. local recycleData = getRecycleData(human)
  31. if not recycleData then
  32. initRecycleData(human)
  33. recycleData = getRecycleData(human)
  34. end
  35. recycleData.level = newLv
  36. recycleData.exp = newExp
  37. end
  38. -- 是否开启本系统
  39. local function isOpen(human)
  40. -- return RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_2031) -- 待修改
  41. return human.db.lv >= OPEN_COND_LV
  42. end
  43. -- 计算当前等级加成属性
  44. local function calcCurrentLvAttrs(currentLevel)
  45. if not currentLevel or currentLevel <= 0 then
  46. return
  47. end
  48. local attrs = {}
  49. for i=1, currentLevel do
  50. local cfg = RecycleConfig[i]
  51. if cfg and cfg.atttrs then
  52. for _,v in ipairs(cfg.atttrs) do
  53. local attrId = v[1]
  54. local attrVal = v[2]
  55. attrs[attrId] = (attrs[attrId] or 0) + attrVal
  56. end
  57. end
  58. end
  59. return attrs
  60. end
  61. -- 重算战力
  62. local function updatePower(human)
  63. RoleAttr.cleanHeroAttrCache(human)
  64. RoleAttr.doCalc(human)
  65. ObjHuman.sendAttr(human, RoleDefine.ZHANDOULI)
  66. end
  67. -- 计算出新的等级和经验
  68. local function calcLv(human, addExp)
  69. local recycleData = getRecycleData(human)
  70. local currentLevel = recycleData and recycleData.level or 0
  71. local currentExp = recycleData and recycleData.exp or 0
  72. currentExp = currentExp + addExp
  73. local newLv = currentLevel
  74. for i=currentLevel+1, #RecycleConfig do
  75. local cfg = RecycleConfig[i]
  76. if currentExp < cfg.exp then
  77. break
  78. end
  79. currentExp = currentExp - cfg.exp
  80. newLv = i
  81. if currentExp <= 0 then
  82. break
  83. end
  84. end
  85. return newLv, currentExp
  86. end
  87. local function populateCurrentLvAttrs(net, currentLevel)
  88. local isZero = false
  89. if currentLevel <= 0 then
  90. currentLevel = 1
  91. isZero = true
  92. end
  93. local len = 0
  94. net[0] = len
  95. local attrs = calcCurrentLvAttrs(currentLevel)
  96. for attrId, attrVal in pairs(attrs or {}) do
  97. len = len + 1
  98. net[0] = len
  99. net[len].key = attrId
  100. net[len].value = isZero and 0 or attrVal
  101. end
  102. end
  103. local function populateNextLvAttrs(net, nextLv)
  104. local isMax = false
  105. local maxLv = #RecycleConfig
  106. if maxLv <= nextLv then
  107. nextLv = maxLv
  108. isMax = true
  109. end
  110. net[0] = 0
  111. local cfg = RecycleConfig[nextLv]
  112. if cfg and cfg.atttrs then
  113. net[0] = #cfg.atttrs
  114. for k,v in ipairs(cfg.atttrs) do
  115. net[k].key = v[1]
  116. net[k].value = isMax and 0 or v[2]
  117. end
  118. end
  119. end
  120. local function populateRecycleItem(net, human)
  121. net[0] = 0
  122. local bagData = BagLogic.GetBagData(human)
  123. if not bagData then
  124. return
  125. end
  126. local len = 0
  127. for itemId in pairs(bagData) do
  128. local itemCfg = ItemConfig[itemId]
  129. if itemCfg and itemCfg.val and itemCfg.val > 0 then
  130. len = len + 1
  131. net[0] = len
  132. net[len].id = itemId
  133. net[len].recycleVal = itemCfg.val
  134. end
  135. end
  136. end
  137. -- 外部调用, 统计收纳箱加成属性
  138. function doCalcHero(human, addAttrs)
  139. local recycleData = getRecycleData(human)
  140. if not recycleData then
  141. return
  142. end
  143. local currentLevel = recycleData and recycleData.level or 0
  144. if currentLevel <= 0 then
  145. return
  146. end
  147. local attrs = calcCurrentLvAttrs(currentLevel)
  148. for attrId, attrVal in pairs(attrs or {}) do
  149. RoleAttr.updateValue(attrId, attrVal, addAttrs)
  150. end
  151. end
  152. -- 查询
  153. function RecycleItem_Query(human)
  154. if not isOpen(human) then
  155. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  156. end
  157. local recycleData = getRecycleData(human)
  158. local currentLevel = recycleData and recycleData.level or 0
  159. local currentExp = recycleData and recycleData.exp or 0
  160. local maxLevel = #RecycleConfig
  161. local nextLvExp = 0
  162. if currentLevel < maxLevel then
  163. local nextLvCfg = RecycleConfig[currentLevel + 1]
  164. nextLvExp = nextLvCfg.exp - currentExp
  165. end
  166. local msgRet = Msg.gc.GC_RECYCLE_QUERY
  167. msgRet.currentLevel = currentLevel
  168. msgRet.maxLevel = maxLevel
  169. msgRet.currentExp = currentExp
  170. msgRet.nextLvExp = nextLvExp
  171. populateCurrentLvAttrs(msgRet.currentLvAttrs, currentLevel)
  172. populateNextLvAttrs(msgRet.nextLvAttrs, currentLevel+1)
  173. populateRecycleItem(msgRet.recycleList, human)
  174. Msg.send(msgRet, human.fd)
  175. end
  176. -- 回收道具
  177. function RecycleItem_Recycle_Do(human, recycleItemStr)
  178. if not isOpen(human) then
  179. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  180. end
  181. local itemList = Util.parseKVString(recycleItemStr, "|", "-", Util.TONUMBER_ALL)
  182. if not next(itemList) then
  183. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  184. end
  185. local recycleData = getRecycleData(human)
  186. local currentLevel = recycleData and recycleData.level or 0
  187. local maxLevel = #RecycleConfig
  188. if currentLevel >= maxLevel then
  189. return Broadcast.sendErr(human, Lang.COMMON_MAXLEVEL)
  190. end
  191. local totalExp = 0
  192. for itemId, itemCnt in pairs(itemList) do
  193. local itemCfg = ItemConfig[itemId]
  194. if not itemCfg or not itemCfg.val or itemCfg.val <= 0 then
  195. return Broadcast.sendErr(human, Lang.ITEM_CANNOT_RECYCLE)
  196. end
  197. if BagLogic.getItemCnt(human, itemId) < itemCnt then
  198. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  199. end
  200. totalExp = totalExp + itemCfg.val
  201. end
  202. for itemId, itemCnt in pairs(itemList) do
  203. if BagLogic.getItemCnt(human, itemId) < itemCnt then
  204. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  205. end
  206. BagLogic.delItem(human, itemId, itemCnt, LOG_TAG)
  207. end
  208. -- 更新等级, 经验
  209. local newLv, newExp = calcLv(human, totalExp)
  210. updateRecycleData(human, newLv, newExp)
  211. -- 更新战力
  212. updatePower(human)
  213. -- 更新UI界面数据
  214. RecycleItem_Query(human)
  215. end