AutoBuyPotion.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. AutoBuyPotionScript = {}
  2. -- 脚本常量
  3. local scriptConst = {
  4. -- 两个买药的组,先写死
  5. HP_GROUP = 1,
  6. MP_GROUP = 2,
  7. -- 自动买药开启
  8. AUTO_BUY_ON = 1,
  9. -- 随身商店开启
  10. WITH_STORE = 1,
  11. coinType = 1
  12. }
  13. function AutoBuyPotionScript.coinChange(actor, cfgId)
  14. local type = ConfigDataManager.getTableValue("cfg_item", "type", "id", cfgId)
  15. if tonumber(type) == scriptConst.coinType then
  16. local cfgs = ConfigDataManager.getList("cfg_Portableshop")
  17. if table.isNullOrEmpty(cfgs) then
  18. return
  19. end
  20. for _, cfg in ipairs(cfgs) do
  21. local price = cfg.price
  22. if string.contains(price, cfgId) then
  23. AutoBuyPotionScript.autoBuyPotions(actor)
  24. break
  25. end
  26. end
  27. end
  28. end
  29. -- 根据配置表自动购买等级最大的药品
  30. function AutoBuyPotionScript.autoBuyPotions(actor)
  31. local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
  32. local isOn = autoBuyPotion["on"] or 0
  33. if tonumber(isOn) == 0 then
  34. -- tipinfo(actor, "自动买药未开启")
  35. return
  36. end
  37. -- 当前有的数量
  38. local hasHpItemCount, hasMpItemCount = AutoBuyPotionScript.getPortableCount(actor)
  39. local portableShopConfigs = ConfigDataManager.getList("cfg_Portableshop")
  40. if portableShopConfigs == nil or next(portableShopConfigs) == nil then
  41. return
  42. end
  43. local hpConfig, hpCanBuyCount, mpConfig, mpCanBuyCount = AutoBuyPotionScript.getPortableShopConfig(actor,
  44. portableShopConfigs)
  45. if hpConfig == nil and mpConfig == nil then
  46. return
  47. end
  48. if hpConfig ~= nil and hpCanBuyCount > 0 then
  49. AutoBuyPotionScript.buyPotionByGroup(actor, hpConfig, hasHpItemCount, hpCanBuyCount)
  50. end
  51. if mpConfig ~= nil and mpCanBuyCount > 0 then
  52. AutoBuyPotionScript.buyPotionByGroup(actor, mpConfig, hasMpItemCount, mpCanBuyCount)
  53. end
  54. end
  55. function AutoBuyPotionScript.buyPotionByGroup(actor, config, hasItemCount, canBuyCount)
  56. local shouldBuy = true
  57. -- 判断用不用买
  58. local autobuy = config["autobuy"]
  59. local sp = string.split(autobuy, "#")
  60. local buySthreshold = sp[1]
  61. local shouldBuyNum = canBuyCount
  62. if tonumber(hasItemCount) >= tonumber(buySthreshold) then
  63. shouldBuy = false
  64. return
  65. end
  66. if shouldBuy then
  67. local priceInfo = config["price"]
  68. local info = string.split(priceInfo, "#")
  69. local costType = info[1]
  70. local unitPrice = info[2]
  71. local totalPrice = unitPrice * shouldBuyNum
  72. local hasMoney = getbagitemcountbyid(actor, tonumber(costType))
  73. if hasMoney < totalPrice then
  74. noticeTip.noticeinfo(actor, StringIdConst.TEXT334)
  75. return
  76. end
  77. local itemId = config["itemid"]
  78. -- 买药
  79. local result = additemtobag(actor, itemId, shouldBuyNum, 0, 0)
  80. if result then
  81. removeitemfrombag(actor, costType, totalPrice, 0,9999,'自动购买')
  82. end
  83. end
  84. end
  85. -- 根据分组获取当前背包中有的药品数量
  86. function AutoBuyPotionScript.getPortableCount(actor)
  87. local portableShopConfigs = ConfigDataManager.getList("cfg_Portableshop")
  88. if portableShopConfigs == nil or next(portableShopConfigs) == nil then
  89. return nil
  90. end
  91. local hpItemCount = 0
  92. local mpItemCount = 0
  93. for _, portableShopConfig in ipairs(portableShopConfigs) do
  94. local itemId = portableShopConfig["itemid"] or 0
  95. local group = portableShopConfig["group"] or 0
  96. local count = getbagitemcountbyid(actor, itemId) or 0
  97. if tonumber(group) == scriptConst.HP_GROUP then
  98. hpItemCount = hpItemCount + count
  99. elseif tonumber(group) == scriptConst.MP_GROUP then
  100. mpItemCount = mpItemCount + count
  101. end
  102. end
  103. return hpItemCount, mpItemCount
  104. end
  105. -- 根据分组获取买的药品中等级最大的那个config,钱不够了减少购买数量,再不够就买等级低的
  106. function AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
  107. local hpConfig = {}
  108. local mpConfig = {}
  109. local hpMaxLevel = 0
  110. local mpMaxLevel = 0
  111. if portableShopConfigs == nil or next(portableShopConfigs) == nil then
  112. return nil, 0, nil, 0
  113. end
  114. local level = getbaseinfo(acotr, "level")
  115. -- 先找到等级最大的两组config
  116. for _, portableShopConfig in ipairs(portableShopConfigs) do
  117. local configLevel = portableShopConfig["level"] or 0
  118. configLevel = configLevel == "" and 0 or configLevel -- 不填等级不限制
  119. if tonumber(configLevel) <= tonumber(level) then
  120. local group = portableShopConfig["group"]
  121. if tonumber(group) == scriptConst.HP_GROUP and tonumber(configLevel) >= tonumber(hpMaxLevel) then
  122. hpMaxLevel = configLevel
  123. hpConfig = portableShopConfig
  124. elseif tonumber(group) == scriptConst.MP_GROUP and tonumber(configLevel) >= tonumber(mpMaxLevel) then
  125. mpMaxLevel = configLevel
  126. mpConfig = portableShopConfig
  127. end
  128. end
  129. end
  130. -- 比较钱,判断是否降低等级
  131. if next(hpConfig) ~= nil and hpConfig ~= nil and mpConfig ~= nil and next(mpConfig) ~= nil then
  132. local hpCanBuyCount = tonumber(0)
  133. local mpCanBuyCount = tonumber(0)
  134. local hpMoneyType = tonumber(string.split(hpConfig["price"], "#")[1])
  135. local hpMoneyNum = tonumber(string.split(hpConfig["price"], "#")[2])
  136. local mpMoneyType = tonumber(string.split(mpConfig["price"], "#")[1])
  137. local mpMoneyNum = tonumber(string.split(mpConfig["price"], "#")[2])
  138. local hpShouldBuyNum = tonumber(string.split(hpConfig["autobuy"], "#")[2])
  139. local mpShouldBuyNum = tonumber(string.split(mpConfig["autobuy"], "#")[2])
  140. if hpMoneyType == mpMoneyType then
  141. -- 两个消耗是同一种货币
  142. local totalPrice = hpMoneyNum * hpShouldBuyNum + mpMoneyNum * mpShouldBuyNum
  143. local hasMoney = getbagitemcountbyid(acotr, hpMoneyType)
  144. if tonumber(hasMoney) < totalPrice then
  145. -- 钱不够,尝试随机减少购买数量(先去除)
  146. local flag = true
  147. repeat
  148. if flag then
  149. hpShouldBuyNum = hpShouldBuyNum - 1 >= 1 and hpShouldBuyNum - 1 or 1
  150. flag = false
  151. elseif not flag then
  152. mpShouldBuyNum = mpShouldBuyNum - 1 >= 1 and mpShouldBuyNum - 1 or 1
  153. flag = true
  154. end
  155. totalPrice = hpMoneyNum * hpShouldBuyNum + mpMoneyNum * mpShouldBuyNum
  156. if tonumber(hasMoney) >= totalPrice then
  157. -- 说明可以通过减少数量来满足条件,两个物品都不用降级
  158. hpCanBuyCount = hpShouldBuyNum
  159. mpCanBuyCount = mpShouldBuyNum
  160. return hpConfig, hpCanBuyCount, mpConfig, mpCanBuyCount
  161. end
  162. until hpShouldBuyNum == 1 and mpShouldBuyNum == 1
  163. -- logInfo("减少数量钱也不够,尝试买低级药")
  164. -- 随机降低某个药品的等级
  165. local random = math.random(0, 1)
  166. if random == 0 then
  167. -- portableShopConfigs中去除当前高等级Config
  168. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
  169. elseif random == 1 then
  170. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
  171. end
  172. return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
  173. end
  174. else
  175. -- 两个消耗不同的货币
  176. local hasHpMoney = getbagitemcountbyid(acotr, hpMoneyType)
  177. local hasMpMoney = getbagitemcountbyid(acotr, mpMoneyType)
  178. local hpTotalPrice = hpMoneyNum * hpShouldBuyNum
  179. local mpTotalPrice = mpMoneyNum * mpShouldBuyNum
  180. local hpCanDecreaseToBuy = false -- 是否可以通过减少购买hp数量完成购买
  181. local mpCanDecreaseToBuy = false -- 是否可以通过减少购买mp数量完成购买
  182. if tonumber(hasHpMoney) < hpTotalPrice then
  183. repeat
  184. hpShouldBuyNum = hpShouldBuyNum - 1
  185. hpTotalPrice = hpMoneyNum * hpShouldBuyNum
  186. if tonumber(hasHpMoney) >= hpTotalPrice then
  187. hpCanBuyCount = hpShouldBuyNum
  188. hpCanDecreaseToBuy = true
  189. break
  190. end
  191. until hpShouldBuyNum == 1
  192. if not hpCanDecreaseToBuy then
  193. -- hp减少失败,尝试降低hp等级
  194. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
  195. end
  196. elseif tonumber(hasMpMoney) < mpTotalPrice then
  197. repeat
  198. mpShouldBuyNum = mpShouldBuyNum - 1
  199. mpTotalPrice = mpMoneyNum * mpShouldBuyNum
  200. if tonumber(hasMpMoney) >= mpTotalPrice then
  201. mpCanBuyCount = mpShouldBuyNum
  202. mpCanDecreaseToBuy = true
  203. break
  204. end
  205. until mpShouldBuyNum == 1
  206. if not mpCanDecreaseToBuy then
  207. -- mp减少失败,尝试降低mp等级
  208. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
  209. end
  210. end
  211. if not hpCanDecreaseToBuy or not mpCanDecreaseToBuy then
  212. -- 至少有一个不能通过减少购买数量完成购买,只能降级处理
  213. return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
  214. end
  215. end
  216. -- 到了这里说明购买数量可以满足或者当前配置等级已经满足
  217. return hpConfig, hpShouldBuyNum, mpConfig, mpShouldBuyNum
  218. elseif next(hpConfig) ~= nil and hpConfig ~= nil then
  219. local hpMoneyType = tonumber(string.split(hpConfig["price"], "#")[1])
  220. local hpMoneyNum = tonumber(string.split(hpConfig["price"], "#")[2])
  221. local hpShouldBuyNum = tonumber(string.split(hpConfig["autobuy"], "#")[2])
  222. local totalPrice = hpMoneyNum * hpShouldBuyNum
  223. local hasMoney = getbagitemcountbyid(acotr, hpMoneyType)
  224. local canBuyCount = tonumber(0)
  225. local canDecreaseToBuy = false
  226. if tonumber(hasMoney) < totalPrice then
  227. -- 尝试减少购买hp数量
  228. repeat
  229. hpShouldBuyNum = hpShouldBuyNum - 1
  230. totalPrice = hpMoneyNum * hpShouldBuyNum
  231. if tonumber(hasMoney) >= totalPrice then
  232. -- 可以通过减少购买hp数量完成购买,不用降级
  233. canBuyCount = hpShouldBuyNum
  234. canDecreaseToBuy = true
  235. break
  236. end
  237. until hpShouldBuyNum == 1
  238. if not canDecreaseToBuy then
  239. -- 不能减少hp购买数量来满足购买,降级处理
  240. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, hpConfig)
  241. return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
  242. end
  243. end
  244. return hpConfig, canBuyCount, nil, 0
  245. elseif next(mpConfig) ~= nil and mpConfig ~= nil then
  246. local mpMoneyType = tonumber(string.split(mpConfig["price"], "#")[1])
  247. local mpMoneyNum = tonumber(string.split(mpConfig["price"], "#")[2])
  248. local mpShouldBuyNum = tonumber(string.split(mpConfig["autobuy"], "#")[2])
  249. local totalPrice = mpMoneyNum * mpShouldBuyNum
  250. local hasMoney = getbagitemcountbyid(acotr, mpMoneyType)
  251. local canBuyCount = tonumber(0)
  252. local canDecreaseToBuy = false
  253. if tonumber(hasMoney) < totalPrice then
  254. -- 尝试减少购买mp数量
  255. repeat
  256. mpShouldBuyNum = mpShouldBuyNum - 1
  257. totalPrice = mpMoneyNum * mpShouldBuyNum
  258. if tonumber(hasMoney) >= totalPrice then
  259. -- 可以通过减少购买mp数量完成购买,不用降级
  260. canBuyCount = mpShouldBuyNum
  261. canDecreaseToBuy = true
  262. break
  263. end
  264. until mpShouldBuyNum == 1
  265. if not canDecreaseToBuy then
  266. -- 不能减少mp购买数量来满足购买,降级处理
  267. AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, mpConfig)
  268. return AutoBuyPotionScript.getPortableShopConfig(acotr, portableShopConfigs)
  269. end
  270. end
  271. return nil, 0, mpConfig, canBuyCount
  272. end
  273. return nil, 0, nil, 0
  274. end
  275. -- 过滤掉等级高,买不起的config
  276. function AutoBuyPotionScript.filterPortableShopConfigs(portableShopConfigs, config)
  277. for i = #portableShopConfigs, 1, -1 do
  278. if portableShopConfigs[i] == config then
  279. table.remove(portableShopConfigs, i)
  280. break
  281. end
  282. end
  283. end
  284. -- 开启和关闭自动买药
  285. function AutoBuyPotionScript.openOrCloseAutoBuyPotion(actor)
  286. local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
  287. local isOn = autoBuyPotion["on"] or 0
  288. if isOn == 0 then
  289. -- 在关着,要打开需检验
  290. local result = PrivilegeCardScript.checkIsOpenCondition(actor, "cfg_free_buff", "autopotion",
  291. scriptConst.AUTO_BUY_ON)
  292. -- 检查第二项条件
  293. if not result then
  294. result = PrivilegeMonth.hasPrivilege(actor, PrivilegeMonth.PrivilegeType.AUTO_BUY_MEDICINE)
  295. end
  296. if not result then
  297. noticeTip.noticeinfo(actor, StringIdConst.TEXT335)
  298. return
  299. end
  300. end
  301. isOn = 1 - isOn
  302. autoBuyPotion["on"] = isOn
  303. -- lg("开启或关闭自动买药", autoBuyPotion)
  304. setplaydef(actor, "T$autoBuyPotion", autoBuyPotion)
  305. if isOn == 1 then
  306. -- 开启了自动买药,立即判断是否买药
  307. AutoBuyPotionScript.autoBuyPotions(actor)
  308. end
  309. sendluamsg(actor, LuaMessageIdToClient.SET_AUTO_BUY_POTION_RESULT, { isOn = isOn })
  310. end
  311. function AutoBuyPotionScript.getCurrentOpenState(actor)
  312. local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
  313. local isOn = autoBuyPotion["on"] or 0
  314. sendluamsg(actor, LuaMessageIdToClient.CURRENT_AUTO_BUY_POTION_RESULT, { isOn = isOn })
  315. end