From 1e6573a15eeb35af9f28e94856996c1a7cc18626 Mon Sep 17 00:00:00 2001 From: pschifferer Date: Thu, 29 Oct 2009 03:50:37 +0000 Subject: [PATCH] Fully enabled auto-buy feature. Use at your own risk. :-P --- CauldronShoppingList.lua | 45 ++++++++++++++++++++++++++++++++++++++------ CauldronShoppingListUI.lua | 5 ++++- CauldronUtil.lua | 4 ++++ Locale/Cauldron-enUS.lua | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/CauldronShoppingList.lua b/CauldronShoppingList.lua index 0e8596d..8b2b21f 100644 --- a/CauldronShoppingList.lua +++ b/CauldronShoppingList.lua @@ -165,7 +165,8 @@ function CauldronShopping:AutoBuyShoppingItems(list, requestor) end local merchant = Cauldron:GetMerchantItems(); - + + --[[ -- TODO: remove -- store merchant items in the DB if not Cauldron.db.realm.userdata[Cauldron.vars.playername].merchant then @@ -174,13 +175,18 @@ function CauldronShopping:AutoBuyShoppingItems(list, requestor) for item, info in pairs(merchant) do Cauldron.db.realm.userdata[Cauldron.vars.playername].merchant[item] = info; end + --]] -- iterate over shopping list if list[requestor] then for item, amount in pairs(list[requestor]) do -- check if the merchant has the item and it can be purchased if merchant[item] then - CauldronShopping:BuyItem(item, merchant[item], amount); + -- check if the toon has enough of the item already + local counts = Cauldron:ReagentCount(item); + if counts.total < amount then + CauldronShopping:BuyItem(item, merchant[item], amount - counts.total); + end end end end @@ -189,16 +195,34 @@ end function CauldronShopping:BuyItem(item, merchInfo, amount) - if (not merchInfo) or (not amount) or (amount < 1) then + if (not merchInfo) or (not amount) then Cauldron:warn("CauldronShopping:BuyItem: missing merchant item info or invalid amount!"); return; end + if amount < 1 then + -- just return with an error + return; + end -- figure out how many to purchase, accounting for stacks local stacks = 1; local purchases = 1; + local tempAmount = amount; + Cauldron:debug("tempAmount="..tostring(tempAmount)); + local maxStack = 1; if amount > 1 then + Cauldron:debug("item="..tostring(item)); + _,_,_,_,_,_,_,maxStack = GetItemInfo(item); + Cauldron:debug("maxStack="..tostring(maxStack)); + + if maxStack then + purchases = ceil((amount / maxStack) / merchInfo.quantity); + Cauldron:debug("purchases="..tostring(purchases)); + else + maxStack = 1; + Cauldron:debug("setting maxStack to 1"); + end --[[ i="Arcane Powder" o=40-GetItemCount(i) @@ -217,10 +241,13 @@ end end local total = purchases * merchInfo.quantity; + Cauldron:debug("total="..tostring(total)); local qtyInfo = string.format(L["(%1$d total)"], total); - if stacks > 1 then - qtyInfo = string.format(L["(%1$d stacks, %2$d total)"], stacks, total); + Cauldron:debug("qtyInfo="..tostring(qtyInfo)); + if purchases > 1 then + qtyInfo = string.format(L["(%1$d stacks, %2$d total)"], purchases, total); + Cauldron:debug("qtyInfo="..tostring(qtyInfo)); end -- tell the user, because that's the right thing to do @@ -228,7 +255,13 @@ end -- buy the items for i=1,purchases do - -- BuyMerchantItem(merchInfo.index, stacks); + stacks = tempAmount >= maxStack and maxStack / merchInfo.quantity or ceil(tempAmount / merchInfo.quantity); + Cauldron:debug("stacks="..tostring(stacks)); + + BuyMerchantItem(merchInfo.index, stacks); + + tempAmount = tempAmount - maxStack; + Cauldron:debug("tempAmount="..tostring(tempAmount)); end end diff --git a/CauldronShoppingListUI.lua b/CauldronShoppingListUI.lua index 4ccf92f..fc4cdd0 100644 --- a/CauldronShoppingListUI.lua +++ b/CauldronShoppingListUI.lua @@ -39,6 +39,8 @@ function Cauldron:ShowShoppingList() end Cauldron:UpdateShoppingList(); + + _G["CauldronShoppingListFrameAutoBuyButton"]:SetChecked(Cauldron:GetAutoBuy()); end @@ -207,7 +209,8 @@ function Cauldron:UpdateShoppingList() end function Cauldron:AutoBuyShoppingList_OnClick(button) - local checked = button:GetChecked(); + local checked = button:GetChecked() or 0; +-- checked = (checked > 0) ? true : false; Cauldron.db.realm.userdata[Cauldron.vars.playername].options.autoBuy = checked; end diff --git a/CauldronUtil.lua b/CauldronUtil.lua index d631566..b436d71 100644 --- a/CauldronUtil.lua +++ b/CauldronUtil.lua @@ -258,3 +258,7 @@ function Cauldron:GetAchievementsForSkill(skillInfo) return nil; end + +function Cauldron:GetAutoBuy() + return Cauldron.db.realm.userdata[Cauldron.vars.playername].options.autoBuy; +end diff --git a/Locale/Cauldron-enUS.lua b/Locale/Cauldron-enUS.lua index eeebd8e..2226bed 100644 --- a/Locale/Cauldron-enUS.lua +++ b/Locale/Cauldron-enUS.lua @@ -130,7 +130,7 @@ L["Available at vendor"] = true L["Click to queue the listed amount of this item"] = true L["Shift-click to queue the listed potential amount of this item"] = true -L["Purchasing %1$s from merchant %2$d..."] = true +L["Purchasing %1$s from merchant %2$s..."] = true L["(%1$d stacks, %2$d total)"] = true L["(%1$d total)"] = true -- 1.7.9.5