continuation based scan completed
This commit is contained in:
@@ -1,140 +1,128 @@
|
||||
Aux.scan = {}
|
||||
|
||||
Aux.scan.MAX_AUCTIONS_PER_PAGE = 50
|
||||
local AUCTIONS_PER_PAGE = 50
|
||||
|
||||
local ready
|
||||
local continuation
|
||||
|
||||
local current_job
|
||||
local current_page
|
||||
|
||||
local wait, wait_for_results, wait_after, wait_for_function, wait_until_can_query, scan, scan_page, ready -- TODO
|
||||
local state, abort, new_job
|
||||
|
||||
-- forward declaration of local functions
|
||||
local submit_query, start_processing_page
|
||||
local wait, wait_after, wait_for_function, wait_until, wait_for_event
|
||||
local scan, scan_auctions, scan_auctions_helper, submit_query
|
||||
|
||||
|
||||
|
||||
function Aux.scan.on_event()
|
||||
if current_job and ready and ready(event) then
|
||||
resume = nil
|
||||
continuation()
|
||||
if state and state.ready and state.ready(event) then
|
||||
state.ready = nil
|
||||
state.continuation()
|
||||
end
|
||||
end
|
||||
|
||||
function Aux.scan.on_update()
|
||||
if current_job and ready and ready() then
|
||||
resume = nil
|
||||
continuation()
|
||||
if abort then
|
||||
if state and state.job and state.job.on_abort then
|
||||
state.job.on_abort()
|
||||
end
|
||||
state = nil
|
||||
abort = false
|
||||
elseif new_job then
|
||||
state = {
|
||||
job = new_job,
|
||||
page = new_job.page
|
||||
}
|
||||
new_job = nil
|
||||
scan()
|
||||
elseif state and state.ready and state.ready() then
|
||||
state.ready = nil
|
||||
state.continuation()
|
||||
end
|
||||
end
|
||||
|
||||
function Aux.scan.start(job)
|
||||
Aux.scan.abort()
|
||||
|
||||
scan(job)
|
||||
abort = true
|
||||
new_job = job
|
||||
end
|
||||
|
||||
function wait(timeout)
|
||||
function Aux.scan.abort()
|
||||
abort = true
|
||||
end
|
||||
|
||||
|
||||
|
||||
function wait(timeout, k)
|
||||
state.continuation = k
|
||||
local start_time = GetTime()
|
||||
resume = function()
|
||||
state.ready = function()
|
||||
return GetTime() - start_time >= timeout
|
||||
end
|
||||
end
|
||||
|
||||
function wait_after()
|
||||
local last
|
||||
return function wait(timeout)
|
||||
resume = function()
|
||||
return function()
|
||||
last = GetTime()
|
||||
end,
|
||||
function(timeout, k)
|
||||
state.continuation = k
|
||||
state.ready = function()
|
||||
return GetTime() - last >= timeout
|
||||
end
|
||||
coroutine:yield()
|
||||
end,
|
||||
function()
|
||||
last = GetTime()
|
||||
end
|
||||
end
|
||||
|
||||
function wait_for_results(k)
|
||||
continuation = k
|
||||
local start_time = GetTime()
|
||||
resume = function(event)
|
||||
return event == "AUCTION_ITEM_LIST_UPDATE"
|
||||
function wait_for_event(e, k)
|
||||
state.continuation = k
|
||||
state.ready = function(event)
|
||||
return event == e
|
||||
end
|
||||
end
|
||||
|
||||
function wait_for_function(f, args, k)
|
||||
if not f then k() end
|
||||
local done
|
||||
tinsert(args, function() done = true end, 1)
|
||||
f(unpack(args))
|
||||
resume = function(event)
|
||||
return done
|
||||
state.continuation = k
|
||||
local complete
|
||||
state.ready = function()
|
||||
return complete
|
||||
end
|
||||
|
||||
if not f then
|
||||
complete = true
|
||||
else
|
||||
tinsert(args, 1, function() complete = true end)
|
||||
f(unpack(args))
|
||||
end
|
||||
end
|
||||
|
||||
function wait_until(p)
|
||||
resume = function()
|
||||
function wait_until(p, k)
|
||||
state.continuation = k
|
||||
state.ready = function()
|
||||
return p()
|
||||
end
|
||||
end
|
||||
|
||||
function start_processing_page()
|
||||
page_state = {}
|
||||
local count, total_count = GetNumAuctionItems("list")
|
||||
page_state.index = 1
|
||||
page_state.count = count
|
||||
page_state.total_count = total_count
|
||||
|
||||
if current_job.on_start_page then
|
||||
wait_for_function(current_job.on_start_page, {current_page})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Aux.scan.complete()
|
||||
if current_job and current_job.on_complete then
|
||||
current_job.on_complete()
|
||||
end
|
||||
|
||||
current_job = nil
|
||||
current_page = nil
|
||||
end
|
||||
|
||||
|
||||
function Aux.scan.abort()
|
||||
if current_job and current_job.on_abort then
|
||||
current_job.on_abort()
|
||||
end
|
||||
|
||||
current_job = nil
|
||||
current_page = nil
|
||||
end
|
||||
|
||||
|
||||
function scan()
|
||||
|
||||
scan_page(current_page, function()
|
||||
|
||||
current_page = current_job.next_page and current_job.next_page(current_page)
|
||||
if current_page then
|
||||
return Aux.scan.complete()
|
||||
else
|
||||
return scan()
|
||||
end
|
||||
|
||||
end)
|
||||
end
|
||||
|
||||
function scan_page(page, k)
|
||||
wait_for_function(current_job.on_start_page, {current_page}, function()
|
||||
|
||||
submit_query(function()
|
||||
|
||||
wait_for_results(function()
|
||||
wait_for_function(state.job.on_start_page, {state.page}, function() --;
|
||||
|
||||
submit_query(function() --;
|
||||
|
||||
local count, total_count = GetNumAuctionItems("list")
|
||||
|
||||
scan_auctions(count, k)
|
||||
scan_auctions(count, function() --;
|
||||
|
||||
local total_pages = math.ceil(total_count / AUCTIONS_PER_PAGE)
|
||||
|
||||
state.page = state.job.next_page and state.job.next_page(state.page, total_pages)
|
||||
|
||||
if state.page then
|
||||
return scan()
|
||||
else
|
||||
if state.job.on_complete then
|
||||
state.job.on_complete()
|
||||
end
|
||||
end
|
||||
|
||||
end)end)end)
|
||||
end
|
||||
@@ -144,9 +132,9 @@ function scan_auctions(count, k)
|
||||
end
|
||||
|
||||
function scan_auctions_helper(i, n, k)
|
||||
wait_for_function(current_job.on_read_auction, {current_page}, function()
|
||||
wait_for_function(state.job.on_read_auction, {i}, function() --;
|
||||
|
||||
if i == n then
|
||||
if i >= n then
|
||||
return k()
|
||||
else
|
||||
return scan_auctions_helper(i + 1, n, k)
|
||||
@@ -156,19 +144,23 @@ function scan_auctions_helper(i, n, k)
|
||||
end
|
||||
|
||||
function submit_query(k)
|
||||
wait_until(CanSendAuctionQuery, function()
|
||||
QueryAuctionItems(
|
||||
current_job.query.name,
|
||||
current_job.query.min_level,
|
||||
current_job.query.max_level,
|
||||
current_job.query.slot,
|
||||
current_job.query.class,
|
||||
current_job.query.subclass,
|
||||
current_page,
|
||||
current_job.query.usable,
|
||||
current_job.query.quality
|
||||
)
|
||||
|
||||
return k()
|
||||
end)
|
||||
end
|
||||
if state.page then
|
||||
wait_until(CanSendAuctionQuery, function() --;
|
||||
QueryAuctionItems(
|
||||
state.job.query.name,
|
||||
state.job.query.min_level,
|
||||
state.job.query.max_level,
|
||||
state.job.query.slot,
|
||||
state.job.query.class,
|
||||
state.job.query.subclass,
|
||||
state.page,
|
||||
state.job.query.usable,
|
||||
state.job.query.quality
|
||||
)
|
||||
wait_for_event('AUCTION_ITEM_LIST_UPDATE', function() --;
|
||||
return k()
|
||||
end)end)
|
||||
else
|
||||
k()
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user