RecycleItem.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.attrs then
  52. for _,v in ipairs(cfg.attrs) 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.attrs then
  113. net[0] = #cfg.attrs
  114. for k,v in ipairs(cfg.attrs) 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. if not human then
  140. return
  141. end
  142. local recycleData = getRecycleData(human)
  143. if not recycleData then
  144. return
  145. end
  146. local currentLevel = recycleData and recycleData.level or 0
  147. if currentLevel <= 0 then
  148. return
  149. end
  150. local attrs = calcCurrentLvAttrs(currentLevel)
  151. for attrId, attrVal in pairs(attrs or {}) do
  152. RoleAttr.updateValue(attrId, attrVal, addAttrs)
  153. end
  154. end
  155. -- 查询部分信息
  156. function RecycleItem_Query(human)
  157. if not isOpen(human) then
  158. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  159. end
  160. local recycleData = getRecycleData(human)
  161. local currentLevel = recycleData and recycleData.level or 0
  162. local currentExp = recycleData and recycleData.exp or 0
  163. local maxLevel = #RecycleConfig
  164. local nextLvExp = 0
  165. if currentLevel < maxLevel then
  166. local nextLvCfg = RecycleConfig[currentLevel + 1]
  167. nextLvExp = nextLvCfg.exp
  168. end
  169. local msgRet = Msg.gc.GC_RECYCLE_QUERY
  170. msgRet.currentLevel = currentLevel
  171. msgRet.maxLevel = maxLevel
  172. msgRet.currentExp = currentExp
  173. msgRet.nextLvExp = nextLvExp
  174. populateCurrentLvAttrs(msgRet.currentLvAttrs, currentLevel)
  175. populateNextLvAttrs(msgRet.nextLvAttrs, currentLevel+1)
  176. -- populateRecycleItem(msgRet.recycleList, human)
  177. Msg.send(msgRet, human.fd)
  178. end
  179. -- 查询可回收道具列表
  180. function RecycleItem_RecycleItemListQuery(human)
  181. if not isOpen(human) then
  182. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  183. end
  184. local msgRet = Msg.gc.GC_RECYCLE_GET_RECYCLE_LIST
  185. msgRet.recycleList[0] = 0
  186. msgRet.isStart = 1
  187. msgRet.isEnd = 0
  188. local bagData = BagLogic.GetBagData(human)
  189. if not bagData then
  190. msgRet.isEnd = 1
  191. return Msg.send(msgRet, human.fd)
  192. end
  193. local itemArr = {}
  194. for itemId in pairs(bagData) do
  195. local itemCfg = ItemConfig[itemId]
  196. if itemCfg and itemCfg.val and itemCfg.val > 0 then
  197. itemArr[#itemArr+1] = {itemId, itemCfg.val}
  198. end
  199. end
  200. local itemNum = #itemArr
  201. if itemNum == 0 then
  202. msgRet.isEnd = 1
  203. return Msg.send(msgRet, human.fd)
  204. end
  205. local len = 0
  206. local onceMsgLen = 50
  207. for _, itemInfo in ipairs(itemArr) do
  208. len = len + 1
  209. msgRet.recycleList[0] = len
  210. msgRet.recycleList[len].id = itemInfo[1]
  211. msgRet.recycleList[len].recycleVal = itemInfo[2]
  212. if len >= onceMsgLen then
  213. itemNum = itemNum - len
  214. if itemNum <= 0 then
  215. msgRet.isEnd = 1
  216. return Msg.send(msgRet, human.fd)
  217. end
  218. Msg.send(msgRet, human.fd)
  219. len = 0
  220. msgRet.isStart = 0
  221. end
  222. end
  223. if len > 0 then
  224. msgRet.isEnd = 1
  225. Msg.send(msgRet, human.fd)
  226. end
  227. end
  228. -- 回收道具
  229. function RecycleItem_Recycle_Do(human, recycleItemStr)
  230. if not isOpen(human) then
  231. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  232. end
  233. local itemList = Util.parseKVString(recycleItemStr, "|", "-", Util.TONUMBER_ALL)
  234. if not next(itemList) then
  235. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  236. end
  237. local recycleData = getRecycleData(human)
  238. local currentLevel = recycleData and recycleData.level or 0
  239. local maxLevel = #RecycleConfig
  240. if currentLevel >= maxLevel then
  241. return Broadcast.sendErr(human, Lang.COMMON_MAXLEVEL)
  242. end
  243. local totalExp = 0
  244. for itemId, itemCnt in pairs(itemList) do
  245. local itemCfg = ItemConfig[itemId]
  246. if not itemCfg or not itemCfg.val or itemCfg.val <= 0 then
  247. return Broadcast.sendErr(human, Lang.ITEM_CANNOT_RECYCLE)
  248. end
  249. if BagLogic.getItemCnt(human, itemId) < itemCnt then
  250. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  251. end
  252. totalExp = totalExp + itemCfg.val * itemCnt
  253. end
  254. for itemId, itemCnt in pairs(itemList) do
  255. if BagLogic.getItemCnt(human, itemId) < itemCnt then
  256. return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
  257. end
  258. BagLogic.delItem(human, itemId, itemCnt, LOG_TAG)
  259. end
  260. -- 更新等级, 经验
  261. local newLv, newExp = calcLv(human, totalExp)
  262. updateRecycleData(human, newLv, newExp)
  263. -- 更新战力
  264. updatePower(human)
  265. -- 更新UI界面数据
  266. RecycleItem_Query(human)
  267. RecycleItem_RecycleItemListQuery(human)
  268. end