Originally Posted by Opertune
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:
local initialQuery
local auctions = {}
local f = CreateFrame("Frame", "SomeAddon")
-- you can also press the button or use this script when scanning is done
-- /run print(GetMoneyString(SomeAddon:GetLowestPrice(152510)))
local btn = CreateFrame("Button", nil, UIParent, "UIPanelButtonTemplate")
btn:SetPoint("CENTER")
btn:SetSize(120, 40)
btn:SetText("Example")
btn:SetScript("OnClick", function(self, button)
if next(auctions) then
f:SomeExample()
else
print("No data")
end
end)
function f:SomeExample()
local price = self:GetLowestPrice(152510)
print("Min Price:", GetMoneyString(price))
end
function f:GetLowestPrice(itemID)
local minPrice = math.huge
for _, v in pairs(auctions) do
if v[17] == itemID then
local unitPrice = v[10] / v[3] -- totalPrice / count
--print(unitPrice)
minPrice = min(minPrice, unitPrice)
end
end
if minPrice < math.huge then
return minPrice
end
end
function f:ScanAuctions(callback)
local continuables = {}
wipe(auctions)
for i = 0, C_AuctionHouse.GetNumReplicateItems()-1 do
auctions[i] = {C_AuctionHouse.GetReplicateItemInfo(i)}
local item = Item:CreateFromItemID(auctions[i][17]) -- itemID
continuables[item] = true
item:ContinueOnItemLoad(function()
auctions[i] = {C_AuctionHouse.GetReplicateItemInfo(i)}
continuables[item] = nil
if not next(continuables) then
print("I'm done scanning")
callback()
end
end)
end
end
function f:OnEvent(event)
if event == "AUCTION_HOUSE_SHOW" then
C_AuctionHouse.ReplicateItems()
initialQuery = true
elseif event == "REPLICATE_ITEM_LIST_UPDATE" then
if initialQuery then
self:ScanAuctions(function() self:SomeExample() end)
initialQuery = false
end
end
end
f:RegisterEvent("AUCTION_HOUSE_SHOW")
f:RegisterEvent("REPLICATE_ITEM_LIST_UPDATE")
f:SetScript("OnEvent", f.OnEvent)