Bag.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. for cfgId, count in pairs(rewardsMap) do
  140. local tableValue = ConfigDataManager.getTable("cfg_item", "id", cfgId)
  141. if table.isNullOrEmpty(tableValue) then
  142. error("Bag.sendRewards cfgId error", cfgId)
  143. goto continue
  144. end
  145. local notEnterPack = tableValue[1]["notenterpack"]
  146. local overlying = tableValue[1]["overlying"]
  147. if (string.isNullOrEmpty(notEnterPack) or tonumber(notEnterPack) == 0)
  148. and (string.isNullOrEmpty(overlying) or tonumber(overlying) <= 1) then
  149. for i = 1, count do
  150. local itemId
  151. if string.isNullOrEmpty(actionDesc) then
  152. itemId = additemtobag(actor, cfgId, 1)
  153. else
  154. itemId = additemtobag(actor, cfgId, 1, 0, 9999, actionDesc)
  155. end
  156. if (itemId == false or itemId == "false") then
  157. error("添加道具失败:", actor, cfgId, count, actionDesc)
  158. end
  159. local itemType = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId)
  160. if tonumber(itemType) ~= ItemType.TRANSFER_CARD then
  161. table.insert(tipItems, {
  162. ["id"] = itemId,
  163. ["cfgId"] = cfgId,
  164. ["count"] = 1,
  165. })
  166. end
  167. end
  168. else
  169. local itemId
  170. if string.isNullOrEmpty(actionDesc) then
  171. itemId = additemtobag(actor, cfgId, count)
  172. else
  173. itemId = additemtobag(actor, cfgId, count, 0, 9999, actionDesc)
  174. end
  175. if (itemId == false or itemId == "false") then
  176. error("添加道具失败:", actor, cfgId, count, actionDesc)
  177. end
  178. local itemType = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId)
  179. if tonumber(itemType) ~= ItemType.TRANSFER_CARD then
  180. table.insert(tipItems, {
  181. ["id"] = itemId,
  182. ["cfgId"] = cfgId,
  183. ["count"] = count,
  184. })
  185. end
  186. end
  187. :: continue ::
  188. end
  189. if table.count(tipItems) > 0 then
  190. --奖励面板通知
  191. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tipItems)
  192. end
  193. end
  194. ---发送奖励导背包,并且通知面板消息
  195. ---@param actor any 玩家
  196. ---@param rewardsMap table {道具id=数量,道具id=数量}
  197. ---@param itemBing string cfg_bind -> bind
  198. function Bag.sendRewardsByBind(actor, rewardsMap, itemBing, actionDesc)
  199. -- 添加奖励到背包
  200. additemmaptobag(actor, rewardsMap, itemBing, 9999, actionDesc)
  201. --奖励面板通知
  202. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, rewardsMap)
  203. end