View Single Post
10-09-20, 11:48 PM   #5
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Opertune View Post
but when i want to print minimum buyout price (after gold conversion and after retrieve the minimum value in my table) my function return a random value.

I'm guessing it's because you're not waiting for all auction items to get cached, and skip ahead to checking the auctions

Here's another example which prints the lowest price for an item ID right after it's done scanning or when you press the button or run a script
Lua Code:
  1. local initialQuery
  2. local auctions = {}
  3. local f = CreateFrame("Frame", "SomeAddon")
  4.  
  5. -- you can also press the button or use this script when scanning is done
  6. -- /run print(GetMoneyString(SomeAddon:GetLowestPrice(152510)))
  7. local btn = CreateFrame("Button", nil, UIParent, "UIPanelButtonTemplate")
  8. btn:SetPoint("CENTER")
  9. btn:SetSize(120, 40)
  10. btn:SetText("Example")
  11. btn:SetScript("OnClick", function(self, button)
  12.     if next(auctions) then
  13.         f:SomeExample()
  14.     else
  15.         print("No data")
  16.     end
  17. end)
  18.  
  19. function f:SomeExample()
  20.     local price = self:GetLowestPrice(152510)
  21.     print("Min Price:", GetMoneyString(price))
  22. end
  23.  
  24. function f:GetLowestPrice(itemID)
  25.     local minPrice = math.huge
  26.     for _, v in pairs(auctions) do
  27.         if v[17] == itemID then
  28.             local unitPrice = v[10] / v[3] -- totalPrice / count
  29.             --print(unitPrice)
  30.             minPrice = min(minPrice, unitPrice)
  31.         end
  32.     end
  33.     if minPrice < math.huge then
  34.         return minPrice
  35.     end
  36. end
  37.  
  38. function f:ScanAuctions(callback)
  39.     local continuables = {}
  40.     wipe(auctions)
  41.     for i = 0, C_AuctionHouse.GetNumReplicateItems()-1 do
  42.         auctions[i] = {C_AuctionHouse.GetReplicateItemInfo(i)}
  43.         local item = Item:CreateFromItemID(auctions[i][17]) -- itemID
  44.         continuables[item] = true
  45.  
  46.         item:ContinueOnItemLoad(function()
  47.             auctions[i] = {C_AuctionHouse.GetReplicateItemInfo(i)}
  48.             continuables[item] = nil
  49.             if not next(continuables) then
  50.                 print("I'm done scanning")
  51.                 callback()
  52.             end
  53.         end)
  54.     end
  55. end
  56.  
  57. function f:OnEvent(event)
  58.     if event == "AUCTION_HOUSE_SHOW" then
  59.         C_AuctionHouse.ReplicateItems()
  60.         initialQuery = true
  61.     elseif event == "REPLICATE_ITEM_LIST_UPDATE" then
  62.         if initialQuery then
  63.             self:ScanAuctions(function() self:SomeExample() end)
  64.             initialQuery = false
  65.         end
  66.     end
  67. end
  68.  
  69. f:RegisterEvent("AUCTION_HOUSE_SHOW")
  70. f:RegisterEvent("REPLICATE_ITEM_LIST_UPDATE")
  71. f:SetScript("OnEvent", f.OnEvent)

Last edited by Ketho : 12-19-20 at 02:48 PM.
  Reply With Quote