123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- --- 背包相关处理
- --- 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 = {}
- -- 变身卡牌需要走批量
- local cardRewardMap = {}
- local otherRewardMap = {}
- for cfgId, count in pairs(rewardsMap) do
- local itemCfg = ConfigDataManager.getById("cfg_item", cfgId)
- if table.notNullOrEmpty(itemCfg) then
- local itemType = string.tonumber(itemCfg.type)
- if itemType == ItemType.TRANSFER_CARD then
- cardRewardMap[cfgId] = count
- else
- otherRewardMap[cfgId] = count
- end
- end
- end
- if table.notNullOrEmpty(cardRewardMap) then
- if string.isNullOrEmpty(actionDesc) then
- additemmaptobag(actor, cardRewardMap)
- else
- additemmaptobag(actor, cardRewardMap, 0, 9999, actionDesc)
- end
- end
- -- 添加奖励到背包
- for cfgId, count in pairs(otherRewardMap) 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
- table.insert(tipItems, {
- ["id"] = itemId,
- ["cfgId"] = cfgId,
- ["count"] = 1,
- })
- 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
- table.insert(tipItems, {
- ["id"] = itemId,
- ["cfgId"] = cfgId,
- ["count"] = count,
- })
- 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
|