-
Posted by Philburt on Tue, 09 Jun 2009 18:37:30
I wish to read the first 10 pages of the AH into a table using the following code:
function MainFrame_OnLoad() maxpages=10 thispage=0 CanISendQuery=nil RegisterTheEvent() end --on the screen is a frame called MainFrame, with a button called UserScanButton function UserScanButton_OnClick() for key,value in pairs(AHTable) do AHTable[key]=nil end -- make sure the table is empty CanISendQuery=nil while CanISendQuery~=1 CanISendQuery,_=CanSendAuctionQuery("list") end QueryAuctionItems("",1,80,0,0,0,thispage) end -- the program now waits for the AUCTION_ITEM_LIST_UPDATE event to fire before processing the page of data function ReadTheAuctionHouse() for ScannedItem=1,50 do name,_,count,_,_,level,_,_,buyout,_,_,_,sold=GetAuctionItemInfo("list",ScannedItem) if name~=nil then AHTable[#AHTable+1]=name.."%"..count.."%"..level.."%"..buyout.."%"..sold end end thispage=thispage+1 if thispage>maxpages then MainFrame:UnregisterEvent("AUCTION_ITEM_LIST_UPDATE") end if thispage<=maxpages then CanISendQuery=nil while CanISendQuery~=1 do CanISendQuery,_=CanSendAuctionQuery("list") end QueryAuctionItems("",1,80,0,0,0,thispage) -- get next page end end function MainFrame_OnEvent(frame,event,...) if event=="AUCTION_ITEM_LIST_UPDATE" then ReadTheAuctionHouse() end end function RegisterTheEvent() local EventFramesRegistered = {} EventFramesRegistered = {GetFramesRegisteredForEvent("AUCTION_ITEM_LIST_UPDATE")} for _, frame in pairs(EventFramesRegistered) do frame:UnregisterEvent("AUCTION_ITEM_LIST_UPDATE") end --make sure only my addon is registered for this event (whilst testing only) MainFrame:RegisterEvent("AUCTION_ITEM_LIST_UPDATE") end
The process is: 1) OnLoading, set some variables and register the event 2) Wait until the user presses the Scan Button 3) Wait until the program can scan the AH (CanSendAuctionQuery("list")) 4) Use QueryAuctionItems to read a page-load of data (50 items) 5) Wait until the AUCTIONITEMLISTUPDATE event fires 6) Read the 50 items by calling the ReadAuctionHouse function 7) Put each item (if not nil) into the AHTable 8) Increase the thispage counter 9) Unregister the event if we have done all the pages and the program would now stop otherwise, 10) Wait until the program can scan the AH (CanSendAuctionQuery("list")) 11) Use QueryAuctionItems to read this new page-load of data 12) the program now waits again for the AUCTIONITEMLISTUPDATE event to fire, and this process loops back to step 5.
My problem is that the table only holds the data from the 1st page, repeated 10 times, (ie records 1-50 are the same as 51-100 and 101-150 etc.
Any ideas would be very welcome - thanks in advance.
-
Posted by jnwhiteh on Tue, 09 Jun 2009 22:24:07
You're setting a LOT of variables that aren't local. This is a bad thing. Your variables need to either be set global with a name that will not conflict, or local so that they can be accessed everywhere. Anything there may be interfering.
Beyond that, I would add a print statement to the part where you are doing the scanning, so you can see what is coming across. Then you can decide if its the scan/logic that is failing or something else.
-
Posted by Philburt on Wed, 10 Jun 2009 18:08:00
Thanks for the reply.
I didn't show all the variable declarations intentionally so as to keep the length of code down. Also, I used simple common variable names to keep the code easy to understand.
My main concern was if the program flow/structure was correct or not, by having the Event Handler routine calling the ReadTheAuctionHouse routine, which inturn uses the QueryAuctionItems command to fire the AUCTIONITEMLIST_UPDATE event.
After adding a few AddMessages to the code, I've come to realise that the program will get one page, use the event handler to read the data and store in AHTable and then get the next page, triggering a new AUCTIONITEMLIST_UPDATE event. Control then returns to the end of the Event Handler, and I believed the event would soon fire and send control back to the ReadTheAuctionHouse function again, but instead WOW totally freezes and I have to close it down and reload.