module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+50 -49
View File
@@ -1,8 +1,9 @@
module 'aux.core.scan'
include 'T'
include 'aux'
local T = require 'T'
local info = require 'aux.util.info'
local history = require 'aux.core.history'
@@ -26,7 +27,7 @@ do
end
function M.abort(scan_id)
local aborted = T
local aborted = T.acquire()
for type, state in pairs(scan_states) do
if not scan_id or state.id == scan_id then
kill_thread(state.id)
@@ -40,26 +41,26 @@ do
end
function M.stop()
state.stopped = true
get_state().stopped = true
end
function complete()
local on_complete = state.params.on_complete
scan_states[state.params.type] = nil
local on_complete = get_state().params.on_complete
scan_states[get_state().params.type] = nil
do (on_complete or nop)() end
end
function get.state()
function get_state()
for _, state in pairs(scan_states) do
if state.id == thread_id then
if state.id == thread_id() then
return state
end
end
end
end
function get.query()
return state.params.queries[state.query_index]
function get_query()
return get_state().params.queries[get_state().query_index]
end
function total_pages(total_auctions)
@@ -68,21 +69,21 @@ end
function last_page(total_auctions)
local last_page = max(total_pages(total_auctions) - 1, 0)
local last_page_limit = query.blizzard_query.last_page or last_page
local last_page_limit = get_query().blizzard_query.last_page or last_page
return min(last_page_limit, last_page)
end
function scan()
state.query_index = state.query_index and state.query_index + 1 or 1
if query and not state.stopped then
do (state.params.on_start_query or nop)(state.query_index) end
if query.blizzard_query then
if (query.blizzard_query.first_page or 0) <= (query.blizzard_query.last_page or huge) then
state.page = query.blizzard_query.first_page or 0
get_state().query_index = get_state().query_index and get_state().query_index + 1 or 1
if get_query() and not get_state().stopped then
do (get_state().params.on_start_query or nop)(get_state().query_index) end
if get_query().blizzard_query then
if (get_query().blizzard_query.first_page or 0) <= (get_query().blizzard_query.last_page or huge) then
get_state().page = get_query().blizzard_query.first_page or 0
return submit_query()
end
else
state.page = nil
get_state().page = nil
return scan_page()
end
end
@@ -91,13 +92,13 @@ end
do
local function submit()
if state.params.type == 'bidder' then
GetBidderAuctionItems(state.page)
elseif state.params.type == 'owner' then
GetOwnerAuctionItems(state.page)
if get_state().params.type == 'bidder' then
GetBidderAuctionItems(get_state().page)
elseif get_state().params.type == 'owner' then
GetOwnerAuctionItems(get_state().page)
else
state.last_list_query = GetTime()
local blizzard_query = query.blizzard_query or T
get_state().last_list_query = GetTime()
local blizzard_query = get_query().blizzard_query or T.acquire()
QueryAuctionItems(
blizzard_query.name,
blizzard_query.min_level,
@@ -105,7 +106,7 @@ do
blizzard_query.slot,
blizzard_query.class,
blizzard_query.subclass,
state.page,
get_state().page,
blizzard_query.usable,
blizzard_query.quality
)
@@ -113,8 +114,8 @@ do
return wait_for_results()
end
function submit_query()
if state.stopped then return end
if state.params.type ~= 'list' then
if get_state().stopped then return end
if get_state().params.type ~= 'list' then
return submit()
else
return when(CanSendAuctionQuery, submit)
@@ -126,30 +127,30 @@ function scan_page(i)
i = i or 1
if i > PAGE_SIZE then
do (state.params.on_page_scanned or nop)() end
if query.blizzard_query and state.page < last_page(state.total_auctions) then
state.page = state.page + 1
do (get_state().params.on_page_scanned or nop)() end
if get_query().blizzard_query and get_state().page < last_page(get_state().total_auctions) then
get_state().page = get_state().page + 1
return submit_query()
else
return scan()
end
end
local auction_info = info.auction(i, state.params.type)
if auction_info and (auction_info.owner or state.params.ignore_owner or aux_ignore_owner) then
local auction_info = info.auction(i, get_state().params.type)
if auction_info and (auction_info.owner or get_state().params.ignore_owner or aux_ignore_owner) then
auction_info.index = i
auction_info.page = state.page
auction_info.blizzard_query = query.blizzard_query
auction_info.query_type = state.params.type
auction_info.page = get_state().page
auction_info.blizzard_query = get_query().blizzard_query
auction_info.query_type = get_state().params.type
history.process_auction(auction_info)
if (state.params.auto_buy_validator or nop)(auction_info) then
if (get_state().params.auto_buy_validator or nop)(auction_info) then
local send_signal, signal_received = signal()
when(signal_received, scan_page, i)
return place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, send_signal)
elseif not query.validator or query.validator(auction_info) then
do (state.params.on_auction or nop)(auction_info) end
elseif not get_query().validator or get_query().validator(auction_info) then
do (get_state().params.on_auction or nop)(auction_info) end
end
end
@@ -157,29 +158,29 @@ function scan_page(i)
end
function wait_for_results()
if state.params.type == 'bidder' then
return when(function() return bids_loaded end, accept_results)
elseif state.params.type == 'owner' then
if get_state().params.type == 'bidder' then
return when(function() return bids_loaded() end, accept_results)
elseif get_state().params.type == 'owner' then
return wait_for_owner_results()
elseif state.params.type == 'list' then
elseif get_state().params.type == 'list' then
return wait_for_list_results()
end
end
function accept_results()
_, state.total_auctions = GetNumAuctionItems(state.params.type)
_, get_state().total_auctions = GetNumAuctionItems(get_state().params.type)
do
(state.params.on_page_loaded or nop)(
state.page - (query.blizzard_query.first_page or 0) + 1,
last_page(state.total_auctions) - (query.blizzard_query.first_page or 0) + 1,
total_pages(state.total_auctions) - 1
(get_state().params.on_page_loaded or nop)(
get_state().page - (get_query().blizzard_query.first_page or 0) + 1,
last_page(get_state().total_auctions) - (get_query().blizzard_query.first_page or 0) + 1,
total_pages(get_state().total_auctions) - 1
)
end
return scan_page()
end
function wait_for_owner_results()
if state.page == current_owner_page then
if get_state().page == current_owner_page() then
return accept_results()
else
local updated
@@ -194,8 +195,8 @@ function wait_for_list_results()
last_update = GetTime()
updated = true
end)
local timeout = later(5, state.last_list_query)
local ignore_owner = state.params.ignore_owner or aux_ignore_owner
local timeout = later(5, get_state().last_list_query)
local ignore_owner = get_state().params.ignore_owner or aux_ignore_owner
return when(function()
if not last_update and timeout() then
return true