123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- AutoBuyPotionScript = {}
- -- 脚本常量
- local scriptConst = {
- -- 两个买药的组,先写死
- HP_GROUP = 1,
- MP_GROUP = 2,
- -- 自动买药开启
- AUTO_BUY_ON = 1,
- -- 随身商店开启
- WITH_STORE = 1,
- coinType = 1
- }
- function AutoBuyPotionScript.coinChange(actor, cfgId)
- local type = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId)
- if tonumber(type) == scriptConst.coinType then
- local cfgs = ConfigDataManager.getList("cfg_Portableshop")
- if table.isNullOrEmpty(cfgs) then
- return
- end
- for _, cfg in ipairs(cfgs) do
- local price = cfg.price
- if string.contains(price, cfgId) then
- AutoBuyPotionScript.autoBuyPotions(actor)
- break
- end
- end
- end
- end
- -- 根据配置表自动购买等级最大的药品
- function AutoBuyPotionScript.autoBuyPotions(actor)
- local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
- local isOn = autoBuyPotion["on"] or 0
- if tonumber(isOn) == 0 then
- -- tipinfo(actor, "自动买药未开启")
- return
- end
- -- 当前有的数量
- local hasHpItemCount, hasMpItemCount = AutoBuyPotionScript.getPortableCount(actor)
- local portableShopConfigs = ConfigDataManager.getList("cfg_Portableshop")
- if portableShopConfigs == nil or next(portableShopConfigs) == nil then
- return
- end
- local hpConfig, hpCanBuyCount, mpConfig, mpCanBuyCount = AutoBuyPotionScript.getPortableShopConfig(actor,
- portableShopConfigs)
- if hpConfig == nil and mpConfig == nil then
- return
- end
- if hpConfig ~= nil and hpCanBuyCount > 0 then
- AutoBuyPotionScript.buyPotionByGroup(actor, hpConfig, hasHpItemCount, hpCanBuyCount)
- end
- if mpConfig ~= nil and mpCanBuyCount > 0 then
- AutoBuyPotionScript.buyPotionByGroup(actor, mpConfig, hasMpItemCount, mpCanBuyCount)
- end
- end
- function AutoBuyPotionScript.buyPotionByGroup(actor, config, hasItemCount, canBuyCount)
- local shouldBuy = true
- -- 判断用不用买
- local autobuy = config["autobuy"]
- local sp = string.split(autobuy, "#")
- local buySthreshold = sp[1]
- local shouldBuyNum = canBuyCount
- if tonumber(hasItemCount) >= tonumber(buySthreshold) then
- shouldBuy = false
- return
- end
- if shouldBuy then
- local priceInfo = config["price"]
- local info = string.split(priceInfo, "#")
- local costType = info[1]
- local unitPrice = info[2]
- local totalPrice = unitPrice * shouldBuyNum
- local hasMoney = getbagitemcountbyid(actor, tonumber(costType))
- if hasMoney < totalPrice then
- noticeTip.noticeinfo(actor, StringIdConst.TEXT334)
- return
- end
- local itemId = config["itemid"]
- -- 买药
- local result = additemtobag(actor, itemId, shouldBuyNum, 0, 0)
- if result then
- removeitemfrombag(actor, costType, totalPrice, 0,9999,'自动购买')
- end
- end
- end
- -- 根据分组获取当前背包中有的药品数量
- function AutoBuyPotionScript.getPortableCount(actor)
- local portableShopConfigs = ConfigDataManager.getList("cfg_Portableshop")
- if portableShopConfigs == nil or next(portableShopConfigs) == nil then
- return nil
- end
- local hpItemCount = 0
- local mpItemCount = 0
- for _, portableShopConfig in ipairs(portableShopConfigs) do
- local itemId = portableShopConfig["itemid"] or 0
- local group = portableShopConfig["group"] or 0
- local count = getbagitemcountbyid(actor, itemId) or 0
- if tonumber(group) == scriptConst.HP_GROUP then
- hpItemCount = hpItemCount + count
- elseif tonumber(group) == scriptConst.MP_GROUP then
- mpItemCount = mpItemCount + count
- end
- end
- return hpItemCount, mpItemCount
- end
- -- 根据分组获取买的药品中等级最大的那个config,钱不够了减少购买数量,再不够就买等级低的
- function AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
- local hpConfig = {}
- local mpConfig = {}
- local hpMaxLevel = 0
- local mpMaxLevel = 0
- if portableShopConfigs == nil or next(portableShopConfigs) == nil then
- return nil, 0, nil, 0
- end
- local level = getbaseinfo(acotr, "level")
- -- 先找到等级最大的两组config
- for _, portableShopConfig in ipairs(portableShopConfigs) do
- local configLevel = portableShopConfig["level"] or 0
- configLevel = configLevel == "" and 0 or configLevel -- 不填等级不限制
- if tonumber(configLevel) <= tonumber(level) then
- local group = portableShopConfig["group"]
- if tonumber(group) == scriptConst.HP_GROUP and tonumber(configLevel) >= tonumber(hpMaxLevel) then
- hpMaxLevel = configLevel
- hpConfig = portableShopConfig
- elseif tonumber(group) == scriptConst.MP_GROUP and tonumber(configLevel) >= tonumber(mpMaxLevel) then
- mpMaxLevel = configLevel
- mpConfig = portableShopConfig
- end
- end
- end
- -- 比较钱,判断是否降低等级
- if next(hpConfig) ~= nil and hpConfig ~= nil and mpConfig ~= nil and next(mpConfig) ~= nil then
- local hpCanBuyCount = tonumber(0)
- local mpCanBuyCount = tonumber(0)
- local hpMoneyType = tonumber(string.split(hpConfig["price"], "#")[1])
- local hpMoneyNum = tonumber(string.split(hpConfig["price"], "#")[2])
- local mpMoneyType = tonumber(string.split(mpConfig["price"], "#")[1])
- local mpMoneyNum = tonumber(string.split(mpConfig["price"], "#")[2])
- local hpShouldBuyNum = tonumber(string.split(hpConfig["autobuy"], "#")[2])
- local mpShouldBuyNum = tonumber(string.split(mpConfig["autobuy"], "#")[2])
- if hpMoneyType == mpMoneyType then
- -- 两个消耗是同一种货币
- local totalPrice = hpMoneyNum * hpShouldBuyNum + mpMoneyNum * mpShouldBuyNum
- local hasMoney = getbagitemcountbyid(acotr, hpMoneyType)
- if tonumber(hasMoney) < totalPrice then
- -- 钱不够,尝试随机减少购买数量(先去除)
- local flag = true
- repeat
- if flag then
- hpShouldBuyNum = hpShouldBuyNum - 1 >= 1 and hpShouldBuyNum - 1 or 1
- flag = false
- elseif not flag then
- mpShouldBuyNum = mpShouldBuyNum - 1 >= 1 and mpShouldBuyNum - 1 or 1
- flag = true
- end
- totalPrice = hpMoneyNum * hpShouldBuyNum + mpMoneyNum * mpShouldBuyNum
- if tonumber(hasMoney) >= totalPrice then
- -- 说明可以通过减少数量来满足条件,两个物品都不用降级
- hpCanBuyCount = hpShouldBuyNum
- mpCanBuyCount = mpShouldBuyNum
- return hpConfig, hpCanBuyCount, mpConfig, mpCanBuyCount
- end
- until hpShouldBuyNum == 1 and mpShouldBuyNum == 1
- -- logInfo("减少数量钱也不够,尝试买低级药")
- -- 随机降低某个药品的等级
- local random = math.random(0, 1)
- if random == 0 then
- -- portableShopConfigs中去除当前高等级Config
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
- elseif random == 1 then
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
- end
- return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
- end
- else
- -- 两个消耗不同的货币
- local hasHpMoney = getbagitemcountbyid(acotr, hpMoneyType)
- local hasMpMoney = getbagitemcountbyid(acotr, mpMoneyType)
- local hpTotalPrice = hpMoneyNum * hpShouldBuyNum
- local mpTotalPrice = mpMoneyNum * mpShouldBuyNum
- local hpCanDecreaseToBuy = false -- 是否可以通过减少购买hp数量完成购买
- local mpCanDecreaseToBuy = false -- 是否可以通过减少购买mp数量完成购买
- if tonumber(hasHpMoney) < hpTotalPrice then
- repeat
- hpShouldBuyNum = hpShouldBuyNum - 1
- hpTotalPrice = hpMoneyNum * hpShouldBuyNum
- if tonumber(hasHpMoney) >= hpTotalPrice then
- hpCanBuyCount = hpShouldBuyNum
- hpCanDecreaseToBuy = true
- break
- end
- until hpShouldBuyNum == 1
- if not hpCanDecreaseToBuy then
- -- hp减少失败,尝试降低hp等级
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
- end
- elseif tonumber(hasMpMoney) < mpTotalPrice then
- repeat
- mpShouldBuyNum = mpShouldBuyNum - 1
- mpTotalPrice = mpMoneyNum * mpShouldBuyNum
- if tonumber(hasMpMoney) >= mpTotalPrice then
- mpCanBuyCount = mpShouldBuyNum
- mpCanDecreaseToBuy = true
- break
- end
- until mpShouldBuyNum == 1
- if not mpCanDecreaseToBuy then
- -- mp减少失败,尝试降低mp等级
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
- end
- end
- if not hpCanDecreaseToBuy or not mpCanDecreaseToBuy then
- -- 至少有一个不能通过减少购买数量完成购买,只能降级处理
- return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
- end
- end
- -- 到了这里说明购买数量可以满足或者当前配置等级已经满足
- return hpConfig, hpShouldBuyNum, mpConfig, mpShouldBuyNum
- elseif next(hpConfig) ~= nil and hpConfig ~= nil then
- local hpMoneyType = tonumber(string.split(hpConfig["price"], "#")[1])
- local hpMoneyNum = tonumber(string.split(hpConfig["price"], "#")[2])
- local hpShouldBuyNum = tonumber(string.split(hpConfig["autobuy"], "#")[2])
- local totalPrice = hpMoneyNum * hpShouldBuyNum
- local hasMoney = getbagitemcountbyid(acotr, hpMoneyType)
- local canBuyCount = tonumber(0)
- local canDecreaseToBuy = false
- if tonumber(hasMoney) < totalPrice then
- -- 尝试减少购买hp数量
- repeat
- hpShouldBuyNum = hpShouldBuyNum - 1
- totalPrice = hpMoneyNum * hpShouldBuyNum
- if tonumber(hasMoney) >= totalPrice then
- -- 可以通过减少购买hp数量完成购买,不用降级
- canBuyCount = hpShouldBuyNum
- canDecreaseToBuy = true
- break
- end
- until hpShouldBuyNum == 1
- if not canDecreaseToBuy then
- -- 不能减少hp购买数量来满足购买,降级处理
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
- return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
- end
- end
- return hpConfig, canBuyCount, nil, 0
- elseif next(mpConfig) ~= nil and mpConfig ~= nil then
- local mpMoneyType = tonumber(string.split(mpConfig["price"], "#")[1])
- local mpMoneyNum = tonumber(string.split(mpConfig["price"], "#")[2])
- local mpShouldBuyNum = tonumber(string.split(mpConfig["autobuy"], "#")[2])
- local totalPrice = mpMoneyNum * mpShouldBuyNum
- local hasMoney = getbagitemcountbyid(acotr, mpMoneyType)
- local canBuyCount = tonumber(0)
- local canDecreaseToBuy = false
- if tonumber(hasMoney) < totalPrice then
- -- 尝试减少购买mp数量
- repeat
- mpShouldBuyNum = mpShouldBuyNum - 1
- totalPrice = mpMoneyNum * mpShouldBuyNum
- if tonumber(hasMoney) >= totalPrice then
- -- 可以通过减少购买mp数量完成购买,不用降级
- canBuyCount = mpShouldBuyNum
- canDecreaseToBuy = true
- break
- end
- until mpShouldBuyNum == 1
- if not canDecreaseToBuy then
- -- 不能减少mp购买数量来满足购买,降级处理
- AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
- return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
- end
- end
- return nil, 0, mpConfig, canBuyCount
- end
- return nil, 0, nil, 0
- end
- -- 过滤掉等级高,买不起的config
- function AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, config)
- for i = #portableShopConfigs, 1, -1 do
- if portableShopConfigs[i] == config then
- table.remove(portableShopConfigs, i)
- break
- end
- end
- end
- -- 开启和关闭自动买药
- function AutoBuyPotionScript.openOrCloseAutoBuyPotion(actor)
- local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
- local isOn = autoBuyPotion["on"] or 0
- if isOn == 0 then
- -- 在关着,要打开需检验
- local result = PrivilegeCardScript.checkIsOpenCondition(actor, "cfg_free_buff", "autopotion",
- scriptConst.AUTO_BUY_ON)
- -- 检查第二项条件
- if not result then
- result = PrivilegeMonth.hasPrivilege(actor, PrivilegeMonth.PrivilegeType.AUTO_BUY_MEDICINE)
- end
- if not result then
- noticeTip.noticeinfo(actor, StringIdConst.TEXT335)
- return
- end
- end
- isOn = 1 - isOn
- autoBuyPotion["on"] = isOn
- -- lg("开启或关闭自动买药", autoBuyPotion)
- setplaydef(actor, "T$autoBuyPotion", autoBuyPotion)
- if isOn == 1 then
- -- 开启了自动买药,立即判断是否买药
- AutoBuyPotionScript.autoBuyPotions(actor)
- end
- sendluamsg(actor, LuaMessageIdToClient.SET_AUTO_BUY_POTION_RESULT, { isOn = isOn })
- end
- function AutoBuyPotionScript.getCurrentOpenState(actor)
- local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
- local isOn = autoBuyPotion["on"] or 0
- sendluamsg(actor, LuaMessageIdToClient.CURRENT_AUTO_BUY_POTION_RESULT, { isOn = isOn })
- end
|