--- 背包相关处理 --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by 無心道(15388152619) --- DateTime: 2024/10/28 16:39 --- Bag = {} ---检查背包是否道具充足 function Bag.checkItemEnough(actor, data) for i, v in ipairs(data) do local cfgId = v["cfgid"] local count = v["count"] if not Bag.checkItem(actor, cfgId, count) then return false end end return true end ---检查背包是否道具充足 道具id#数量|道具id#数量 function Bag.checkCostString(actor, cost) return Bag.checkCostMap(actor, string.toIntIntMap(cost, "#", "|")) end ---检查背包是否道具充足 {道具id=数量,道具id=数量} function Bag.checkCostMap(actor, data) for cfgId, count in pairs(data) do if not Bag.checkItem(actor, cfgId, count) then return false end end return true end ---检查背包是否道具充足 function Bag.checkItem(actor, cfgId, count) local num = getbagitemcountbyid(actor, cfgId) or 0 -- print(actor, "cfgId", cfgId, "need", count, "bag", num) if tonumber(num) < tonumber(count) then return false end return true end ---检查背包是否道具充足 {道具id=数量,道具id=数量} function Bag.checkCostMapBind(actor, data) for cfgId, count in pairs(data) do if not Bag.checkItemWithBind(actor, cfgId, count) then return false end end return true end ---检查背包是否道具充足,绑定货币判断普通货币的可替换数量 function Bag.checkItemWithBind(actor, cfgId, count) local num = getbagitemcountbyid(actor, cfgId) or 0 if tonumber(num) >= tonumber(count) then return true end local replaceCount = Bag.getCoinBind(actor, cfgId) if tonumber(num) + replaceCount >= tonumber(count) then return true end return false end -- 获取可以替换绑定货币的的普通货币数量 function Bag.getCoinBind(actor, itemId) local config = ConfigDataManager.getById("cfg_item", itemId) if table.isNullOrEmpty(config) then return 0 end if tonumber(config.type) ~= ItemType.COIN then return 0 end if ItemSubType.BIND_QJCOIN ~= tonumber(config.subtype) and ItemSubType.BIND_DIAMOND ~= tonumber(config.subtype) then return 0 end local replaceId = config.useparam if string.isNullOrEmpty(replaceId) then return 0 end local replaceCount = getbagitemcountbyid(actor, replaceId) or 0 return replaceCount end ---扣除消耗 {道具id=数量,道具id=数量} function Bag.costMapBind(actor, costs, actionDesc) for cfgId, count in pairs(costs) do Bag.costBind(actor, cfgId, count, actionDesc) end end ---扣除消耗 {道具id=数量,道具id=数量} function Bag.costBind(actor, cfgId, count, actionDesc) if string.isNullOrEmpty(actionDesc) then removeitemfrombag(actor, cfgId, count, 1, 9999) else removeitemfrombag(actor, cfgId, count, 1, 9999, actionDesc) end end ---扣除消耗 {道具id=数量,道具id=数量} function Bag.costMap(actor, costs, actionDesc) for cfgId, count in pairs(costs) do Bag.cost(actor, cfgId, count, actionDesc) end end ---扣除消耗 {道具id=数量,道具id=数量} function Bag.cost(actor, cfgId, count, actionDesc) if string.isNullOrEmpty(actionDesc) then removeitemfrombag(actor, cfgId, count) else removeitemfrombag(actor, cfgId, count, 0, 9999, actionDesc) end end ---根据职业发送奖励导背包,并且通知面板消息 ---@param actor any 玩家 ---@param rewardsString string 职业#道具id#数量|职业#道具id#数量 function Bag.sendRewards4CareerString(actor, rewardsString, actionDesc) --职业 local career = getbaseinfo(actor, "getbasecareer") local rewardsMap = string.toIntIntMap4Career(career, rewardsString, "#", "|") Bag.sendRewards(actor, rewardsMap, actionDesc) end ---发送奖励导背包,并且通知面板消息 ---@param actor any 玩家 ---@param rewardsString string 道具id#数量|道具id#数量 function Bag.sendRewards4String(actor, rewardsString, actionDesc) local rewardsMap = string.toIntIntMap(rewardsString, "#", "|") Bag.sendRewards(actor, rewardsMap, actionDesc) end ---发送奖励导背包,并且通知面板消息 ---@param actor any 玩家 ---@param rewardsString string 道具id#数量|道具id#数量 ---@param bindId string cfg_bind -> id function Bag.sendRewards4StringByBind(actor, rewardsString, bindId, actionDesc) local itemBing = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", bindId) local rewardsMap = string.toIntIntMap(rewardsString, "#", "|") Bag.sendRewardsByBind(actor, rewardsMap, itemBing, actionDesc) end ---发送奖励导背包,并且通知面板消息 ---@param actor any 玩家 ---@param rewardsMap table {道具id=数量,道具id=数量} function Bag.sendRewards(actor, rewardsMap, actionDesc) local tipItems = {} -- 添加奖励到背包 for cfgId, count in pairs(rewardsMap) do local tableValue = ConfigDataManager.getTable("cfg_item", "id", cfgId) if table.isNullOrEmpty(tableValue) then error("Bag.sendRewards cfgId error", cfgId) goto continue end local notEnterPack = tableValue[1]["notenterpack"] local overlying = tableValue[1]["overlying"] if (string.isNullOrEmpty(notEnterPack) or tonumber(notEnterPack) == 0) and (string.isNullOrEmpty(overlying) or tonumber(overlying) <= 1) then for i = 1, count do local itemId if string.isNullOrEmpty(actionDesc) then itemId = additemtobag(actor, cfgId, 1) else itemId = additemtobag(actor, cfgId, 1, 0, 9999, actionDesc) end if (itemId == false or itemId == "false") then error("添加道具失败:", actor, cfgId, count, actionDesc) end local itemType = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId) if tonumber(itemType) ~= ItemType.TRANSFER_CARD then table.insert(tipItems, { ["id"] = itemId, ["cfgId"] = cfgId, ["count"] = 1, }) end end else local itemId if string.isNullOrEmpty(actionDesc) then itemId = additemtobag(actor, cfgId, count) else itemId = additemtobag(actor, cfgId, count, 0, 9999, actionDesc) end if (itemId == false or itemId == "false") then error("添加道具失败:", actor, cfgId, count, actionDesc) end local itemType = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId) if tonumber(itemType) ~= ItemType.TRANSFER_CARD then table.insert(tipItems, { ["id"] = itemId, ["cfgId"] = cfgId, ["count"] = count, }) end end :: continue :: end if table.count(tipItems) > 0 then --奖励面板通知 sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tipItems) end end ---发送奖励导背包,并且通知面板消息 ---@param actor any 玩家 ---@param rewardsMap table {道具id=数量,道具id=数量} ---@param itemBing string cfg_bind -> bind function Bag.sendRewardsByBind(actor, rewardsMap, itemBing, actionDesc) -- 添加奖励到背包 additemmaptobag(actor, rewardsMap, itemBing, 9999, actionDesc) --奖励面板通知 sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, rewardsMap) end