scanner refactoring and some bugfixes

This commit is contained in:
Manuel Simon Hirsig
2015-09-28 20:29:43 +02:00
parent 8f069da665
commit 7fcdf27368
4 changed files with 100 additions and 99 deletions
+32 -21
View File
@@ -32,10 +32,14 @@ function AuxBuySearchButton_OnClick()
Aux.scan.start{
query = search_query,
on_start_page = function(i)
set_message('Scanning auctions: page ' .. i .. ' ...')
set_message('Scanning auctions: page ' .. i + 1 .. ' ...')
end,
on_read_auction = function(i)
local auction_item = Aux.info.auction_item(i)
if not auction_item then
return
end
local stack_size = auction_item.charges or auction_item.count
if auction_item.name == search_query.name then
record_auction(auction_item.name, stack_size, auction_item.buyout_price, auction_item.quality, auction_item.owner, auction_item.itemlink)
@@ -98,32 +102,39 @@ function AuxBuyBuySelectedButton_OnClick()
Aux.scan.start{
query = search_query,
on_start_page = function(i)
set_message('Scanning auctions: page ' .. i .. ' ...')
set_message('Scanning auctions: page ' .. i + 1 .. ' ...')
end,
on_read_auction = function(i)
local auction_item = Aux.info.auction_item(i)
if not auction_item then
return
end
local stack_size = auction_item.charges or auction_item.count
if not auction_item.name or not stack_size or not auction_item.buyout_price then
return
end
if auction_item.name and auction_item.count and auction_item.buyout_price then
local key = auction_item.name.."_"..auction_item.count.."_"..auction_item.buyout_price
if order[key] then
local key = auction_item.name.."_"..stack_size.."_"..auction_item.buyout_price
if order[key] then
if GetMoney() >= auction_item.buyout_price then
PlaceAuctionBid("list", i, auction_item.buyout_price)
progress.auctions = progress.auctions + 1
progress.units = progress.units + stack_size
progress.expense = progress.expense + auction_item.buyout_price
end
if GetMoney() >= auction_item.buyout_price then
PlaceAuctionBid("list", i, auction_item.buyout_price)
progress.auctions = progress.auctions + 1
progress.units = progress.units + auction_item.count
progress.expense = progress.expense + auction_item.buyout_price
end
if order[key] > 1 then
order[key] = order[key] - 1
else
order[key] = nil
end
if order[key] > 1 then
order[key] = order[key] - 1
else
local stack_size = auction_item.charges or auction_item.count
if auction_item.name == search_query.name then
record_auction(auction_item.name, stack_size, auction_item.buyout_price, auction_item.quality, auction_item.owner, auction_item.itemlink)
end
order[key] = nil
end
else
if auction_item.name == search_query.name then
record_auction(auction_item.name, stack_size, auction_item.buyout_price, auction_item.quality, auction_item.owner, auction_item.itemlink)
end
end
end,
+5
View File
@@ -47,6 +47,11 @@ end
function Aux.info.auction_item(index)
local itemlink = GetAuctionItemLink("list", index)
if not itemlink then
return
end
local auction_item = itemlink_item(itemlink)
local name, texture, count, quality, usable, level, min_bid, min_increment, buyout_price, bid_amount, high_bidder, owner, sale_status, id, has_all_info = GetAuctionItemInfo("list", index)
+61 -76
View File
@@ -1,41 +1,58 @@
Aux.scan = {}
local STATE_IDLE = 0
local STATE_PREQUERY = 1
local STATE_POSTQUERY = 2
local STATE_PROCESSING = 3 -- doesn't avoid race conditions completely!
local NUM_AUCTION_ITEMS_PER_PAGE = 50
local currentJob
local currentPage
local state = STATE_IDLE
local current_job
local current_page
local page_state
local timeOfLastUpdate = GetTime()
local last_query_request = GetTime()
-- forward declaration of local functions
local submitQuery, processQueryResults
local submit_query, process_query_results
-----------------------------------------
function Aux.scan.on_event()
if event == "AUCTION_ITEM_LIST_UPDATE" then
if state == STATE_POSTQUERY then
state = STATE_PROCESSING
processQueryResults()
if event == "AUCTION_ITEM_LIST_UPDATE" and current_job and not page_state then
local count, total_count = GetNumAuctionItems("list")
if current_job.on_start_page then
current_job.on_start_page(current_page)
end
page_state = {
index = 1,
count = count,
total_count = total_count,
}
end
end
-----------------------------------------
function Aux.scan.on_update()
if state == STATE_PREQUERY and GetTime() - timeOfLastUpdate > 0.5 then
timeOfLastUpdate = GetTime()
if page_state then
if page_state.index <= page_state.count and current_job.on_read_auction then
current_job.on_read_auction(page_state.index)
end
if page_state.index == page_state.count then
if page_state.index == NUM_AUCTION_ITEMS_PER_PAGE then
page_state = nil
current_page = current_page + 1
else
Aux.scan.complete()
end
end
if page_state then
page_state.index = page_state.index + 1
end
elseif current_job and GetTime() - last_query_request > 0.5 then
last_query_request = GetTime()
if CanSendAuctionQuery() then
submitQuery()
submit_query()
end
end
end
@@ -43,47 +60,40 @@ end
-----------------------------------------
function Aux.scan.idle()
return state == STATE_IDLE
return not current_job
end
-----------------------------------------
function Aux.scan.complete()
if state ~= STATE_IDLE then
if currentJob.on_complete then
currentJob.on_complete()
end
currentJob = nil
currentPage = nil
state = STATE_IDLE
if current_job and current_job.on_complete then
current_job.on_complete()
end
current_job = nil
current_page = nil
page_state = nil
end
-----------------------------------------
function Aux.scan.abort()
if state ~= STATE_IDLE then
if currentJob and currentJob.on_abort then
currentJob.on_abort()
end
currentJob = nil
currentPage = nil
state = STATE_IDLE
if current_job and current_job.on_abort then
current_job.on_abort()
end
current_job = nil
current_page = nil
page_state = nil
end
-----------------------------------------
function Aux.scan.start(job)
if state ~= STATE_IDLE then
Aux.scan.abort()
end
Aux.scan.abort()
currentJob = job
state = STATE_PREQUERY
current_job = job
current_page = 0
end
-----------------------------------------
@@ -109,41 +119,16 @@ end
-----------------------------------------
function submitQuery()
function submit_query()
QueryAuctionItems(
currentJob.query.name,
currentJob.query.minLevel,
currentJob.query.maxLevel,
currentJob.query.invTypeIndex,
currentJob.query.classIndex,
currentJob.query.subclassIndex,
currentPage,
currentJob.query.isUsable,
currentJob.query.qualityIndex
current_job.query.name,
current_job.query.minLevel,
current_job.query.maxLevel,
current_job.query.invTypeIndex,
current_job.query.classIndex,
current_job.query.subclassIndex,
current_page,
current_job.query.isUsable,
current_job.query.qualityIndex
)
state = STATE_POSTQUERY
currentPage = currentPage and currentPage + 1 or 1
end
-----------------------------------------
function processQueryResults()
local numBatchAuctions, totalAuctions = GetNumAuctionItems("list")
if currentJob.on_start_page then
currentJob.on_start_page(currentPage)
end
for i = 1, numBatchAuctions do
if currentJob.on_read_auction then
currentJob.on_read_auction(i)
end
end
if numBatchAuctions == NUM_AUCTION_ITEMS_PER_PAGE then
state = STATE_PREQUERY
else
Aux.scan.complete()
end
end
+2 -2
View File
@@ -306,11 +306,11 @@ function Aux_RefreshEntries()
subclassIndex = subclass_index,
},
on_start_page = function(i)
set_message('Scanning auctions: page ' .. i .. ' ...')
set_message('Scanning auctions: page ' .. i + 1 .. ' ...')
end,
on_read_auction = function(i)
local auction_item = Aux.info.auction_item(i)
if auction_item.name == name then
if auction_item and auction_item.name == name then
local stack_size = auction_item.charges or auction_item.count
record_auction(auction_item.name, stack_size, auction_item.buyout_price, auction_item.duration, auction_item.owner)
end