Bag.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. --- 背包相关处理
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by 無心道(15388152619)
  4. --- DateTime: 2024/10/28 16:39
  5. ---
  6. Bag = {}
  7. ---检查背包是否道具充足
  8. function Bag.checkItemEnough(actor, data)
  9. for i, v in ipairs(data) do
  10. local cfgId = v["cfgid"]
  11. local count = v["count"]
  12. if not Bag.checkItem(actor, cfgId, count) then
  13. return false
  14. end
  15. end
  16. return true
  17. end
  18. ---检查背包是否道具充足 道具id#数量|道具id#数量
  19. function Bag.checkCostString(actor, cost)
  20. return Bag.checkCostMap(actor, string.toIntIntMap(cost, "#", "|"))
  21. end
  22. ---检查背包是否道具充足 {道具id=数量,道具id=数量}
  23. function Bag.checkCostMap(actor, data)
  24. for cfgId, count in pairs(data) do
  25. if not Bag.checkItem(actor, cfgId, count) then
  26. return false
  27. end
  28. end
  29. return true
  30. end
  31. ---检查背包是否道具充足
  32. function Bag.checkItem(actor, cfgId, count)
  33. local num = getbagitemcountbyid(actor, cfgId) or 0
  34. -- print(actor, "cfgId", cfgId, "need", count, "bag", num)
  35. if tonumber(num) < tonumber(count) then
  36. return false
  37. end
  38. return true
  39. end
  40. ---检查背包是否道具充足 {道具id=数量,道具id=数量}
  41. function Bag.checkCostMapBind(actor, data)
  42. for cfgId, count in pairs(data) do
  43. if not Bag.checkItemWithBind(actor, cfgId, count) then
  44. return false
  45. end
  46. end
  47. return true
  48. end
  49. ---检查背包是否道具充足,绑定货币判断普通货币的可替换数量
  50. function Bag.checkItemWithBind(actor, cfgId, count)
  51. local num = getbagitemcountbyid(actor, cfgId) or 0
  52. if tonumber(num) >= tonumber(count) then
  53. return true
  54. end
  55. local replaceCount = Bag.getCoinBind(actor, cfgId)
  56. if tonumber(num) + replaceCount >= tonumber(count) then
  57. return true
  58. end
  59. return false
  60. end
  61. -- 获取可以替换绑定货币的的普通货币数量
  62. function Bag.getCoinBind(actor, itemId)
  63. local config = ConfigDataManager.getById("cfg_item", itemId)
  64. if table.isNullOrEmpty(config) then
  65. return 0
  66. end
  67. if tonumber(config.type) ~= ItemType.COIN then
  68. return 0
  69. end
  70. if ItemSubType.BIND_QJCOIN ~= tonumber(config.subtype) and ItemSubType.BIND_DIAMOND ~= tonumber(config.subtype) then
  71. return 0
  72. end
  73. local replaceId = config.useparam
  74. if string.isNullOrEmpty(replaceId) then
  75. return 0
  76. end
  77. local replaceCount = getbagitemcountbyid(actor, replaceId) or 0
  78. return replaceCount
  79. end
  80. ---扣除消耗 {道具id=数量,道具id=数量}
  81. function Bag.costMapBind(actor, costs, actionDesc)
  82. for cfgId, count in pairs(costs) do
  83. Bag.costBind(actor, cfgId, count, actionDesc)
  84. end
  85. end
  86. ---扣除消耗 {道具id=数量,道具id=数量}
  87. function Bag.costBind(actor, cfgId, count, actionDesc)
  88. if string.isNullOrEmpty(actionDesc) then
  89. removeitemfrombag(actor, cfgId, count, 1, 9999)
  90. else
  91. removeitemfrombag(actor, cfgId, count, 1, 9999, actionDesc)
  92. end
  93. end
  94. ---扣除消耗 {道具id=数量,道具id=数量}
  95. function Bag.costMap(actor, costs, actionDesc)
  96. for cfgId, count in pairs(costs) do
  97. Bag.cost(actor, cfgId, count, actionDesc)
  98. end
  99. end
  100. ---扣除消耗 {道具id=数量,道具id=数量}
  101. function Bag.cost(actor, cfgId, count, actionDesc)
  102. if string.isNullOrEmpty(actionDesc) then
  103. removeitemfrombag(actor, cfgId, count)
  104. else
  105. removeitemfrombag(actor, cfgId, count, 0, 9999, actionDesc)
  106. end
  107. end
  108. ---根据职业发送奖励导背包,并且通知面板消息
  109. ---@param actor any 玩家
  110. ---@param rewardsString string 职业#道具id#数量|职业#道具id#数量
  111. function Bag.sendRewards4CareerString(actor, rewardsString, actionDesc)
  112. --职业
  113. local career = getbaseinfo(actor, "getbasecareer")
  114. local rewardsMap = string.toIntIntMap4Career(career, rewardsString, "#", "|")
  115. Bag.sendRewards(actor, rewardsMap, actionDesc)
  116. end
  117. ---发送奖励导背包,并且通知面板消息
  118. ---@param actor any 玩家
  119. ---@param rewardsString string 道具id#数量|道具id#数量
  120. function Bag.sendRewards4String(actor, rewardsString, actionDesc)
  121. local rewardsMap = string.toIntIntMap(rewardsString, "#", "|")
  122. Bag.sendRewards(actor, rewardsMap, actionDesc)
  123. end
  124. ---发送奖励导背包,并且通知面板消息
  125. ---@param actor any 玩家
  126. ---@param rewardsString string 道具id#数量|道具id#数量
  127. ---@param bindId string cfg_bind -> id
  128. function Bag.sendRewards4StringByBind(actor, rewardsString, bindId, actionDesc)
  129. local itemBing = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", bindId)
  130. local rewardsMap = string.toIntIntMap(rewardsString, "#", "|")
  131. Bag.sendRewardsByBind(actor, rewardsMap, itemBing, actionDesc)
  132. end
  133. ---发送奖励导背包,并且通知面板消息
  134. ---@param actor any 玩家
  135. ---@param rewardsMap table {道具id=数量,道具id=数量}
  136. function Bag.sendRewards(actor, rewardsMap, actionDesc)
  137. local tipItems = {}
  138. -- 变身卡牌需要走批量
  139. local cardRewardMap = {}
  140. local otherRewardMap = {}
  141. for cfgId, count in pairs(rewardsMap) do
  142. local itemCfg = ConfigDataManager.getById("cfg_item", cfgId)
  143. if table.notNullOrEmpty(itemCfg) then
  144. local itemType = string.tonumber(itemCfg.type)
  145. if itemType == ItemType.TRANSFER_CARD then
  146. cardRewardMap[cfgId] = count
  147. else
  148. otherRewardMap[cfgId] = count
  149. end
  150. end
  151. end
  152. if table.notNullOrEmpty(cardRewardMap) then
  153. if string.isNullOrEmpty(actionDesc) then
  154. additemmaptobag(actor, cardRewardMap)
  155. else
  156. additemmaptobag(actor, cardRewardMap, 0, 9999, actionDesc)
  157. end
  158. end
  159. -- 添加奖励到背包
  160. for cfgId, count in pairs(otherRewardMap) do
  161. local tableValue = ConfigDataManager.getTable("cfg_item", "id", cfgId)
  162. if table.isNullOrEmpty(tableValue) then
  163. error("Bag.sendRewards cfgId error", cfgId)
  164. goto continue
  165. end
  166. local notEnterPack = tableValue[1]["notenterpack"]
  167. local overlying = tableValue[1]["overlying"]
  168. if (string.isNullOrEmpty(notEnterPack) or tonumber(notEnterPack) == 0)
  169. and (string.isNullOrEmpty(overlying) or tonumber(overlying) <= 1) then
  170. for i = 1, count do
  171. local itemId
  172. if string.isNullOrEmpty(actionDesc) then
  173. itemId = additemtobag(actor, cfgId, 1)
  174. else
  175. itemId = additemtobag(actor, cfgId, 1, 0, 9999, actionDesc)
  176. end
  177. if (itemId == false or itemId == "false") then
  178. error("添加道具失败:", actor, cfgId, count, actionDesc)
  179. end
  180. table.insert(tipItems, {
  181. ["id"] = itemId,
  182. ["cfgId"] = cfgId,
  183. ["count"] = 1,
  184. })
  185. end
  186. else
  187. local itemId
  188. if string.isNullOrEmpty(actionDesc) then
  189. itemId = additemtobag(actor, cfgId, count)
  190. else
  191. itemId = additemtobag(actor, cfgId, count, 0, 9999, actionDesc)
  192. end
  193. if (itemId == false or itemId == "false") then
  194. error("添加道具失败:", actor, cfgId, count, actionDesc)
  195. end
  196. table.insert(tipItems, {
  197. ["id"] = itemId,
  198. ["cfgId"] = cfgId,
  199. ["count"] = count,
  200. })
  201. end
  202. :: continue ::
  203. end
  204. if table.count(tipItems) > 0 then
  205. --奖励面板通知
  206. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tipItems)
  207. end
  208. end
  209. ---发送奖励导背包,并且通知面板消息
  210. ---@param actor any 玩家
  211. ---@param rewardsMap table {道具id=数量,道具id=数量}
  212. ---@param itemBing string cfg_bind -> bind
  213. function Bag.sendRewardsByBind(actor, rewardsMap, itemBing, actionDesc)
  214. -- 添加奖励到背包
  215. additemmaptobag(actor, rewardsMap, itemBing, 9999, actionDesc)
  216. --奖励面板通知
  217. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, rewardsMap)
  218. end