working on big scan/control refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-02-25 17:25:46 +01:00
parent 1a0b7597bd
commit 0d9b37f4d5
3 changed files with 82 additions and 85 deletions
+3 -2
View File
@@ -394,7 +394,9 @@ function public.on_open()
private.update_inventory_records()
refresh = true
-- if selected_item then
-- public.select_item(selected_item.key)
-- end
private.update_recommendation()
end
@@ -704,7 +706,6 @@ function private.set_item(item)
private.update_recommendation()
refresh = true
end
function private.update_inventory_records()
+59 -61
View File
@@ -6,8 +6,6 @@ local PAGE_SIZE = 50
local state
local threads = {}
local scan_auctions, scan_auctions_helper, submit_query, wait_for_callback, wait_for_owner_data, abort, current_query, current_thread
function private.default_next_page(page, total_pages)
local last_page = max(total_pages - 1, 0)
if page < last_page then
@@ -15,11 +13,11 @@ function private.default_next_page(page, total_pages)
end
end
function current_query()
return current_thread().params.queries[current_thread().query_index]
function private.current_query()
return private.current_thread().params.queries[private.current_thread().query_index]
end
function current_thread()
function private.current_thread()
for _, thread in pairs(threads) do
if thread.id == Aux.control.thread_id then
return thread
@@ -50,11 +48,11 @@ function public.abort(type)
end
function private.wait_for_results(k)
if current_thread().params.type == 'bidder' then
if private.current_thread().params.type == 'bidder' then
return private.wait_for_bidder_results(k)
elseif current_thread().params.type == 'owner' then
elseif private.current_thread().params.type == 'owner' then
return private.wait_for_owner_results(k)
elseif current_thread().params.type == 'list' then
elseif private.current_thread().params.type == 'list' then
return private.wait_for_list_results(k)
end
end
@@ -69,7 +67,7 @@ end
function private.wait_for_owner_results(k)
local updated
if current_thread().page == Aux.current_owner_page then
if private.current_thread().page == Aux.current_owner_page then
updated = true
else
Aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', function()
@@ -99,12 +97,12 @@ function private.wait_for_list_results(k)
end
function private.owner_data_complete()
if current_thread().params.no_wait_owner then
if private.current_thread().params.no_wait_owner then
return true
end
local count, _ = GetNumAuctionItems(current_thread().params.type)
local count, _ = GetNumAuctionItems(private.current_thread().params.type)
for i=1,count do
local auction_info = Aux.info.auction(i, current_thread().params.type)
local auction_info = Aux.info.auction(i, private.current_thread().params.type)
if auction_info and not auction_info.owner then
return false
end
@@ -112,7 +110,7 @@ function private.owner_data_complete()
return true
end
function wait_for_callback(args) -- the arguments must not be nil!
function private.wait_for_callback(args) -- the arguments must not be nil!
local ok = true
local f = tremove(args, 1)
@@ -135,18 +133,18 @@ end
function private.scan()
local start_query_index = current_thread().params.start_query_index or 1
local next_query_index = current_thread().params.next_query_index or function(query_index) return query_index + 1 end
local start_query_index = private.current_thread().params.start_query_index or 1
local next_query_index = private.current_thread().params.next_query_index or function(query_index) return query_index + 1 end
current_thread().query_index = current_thread().query_index and next_query_index(current_thread().query_index) or start_query_index
if current_query() then
wait_for_callback{current_thread().params.on_start_query or Aux.util.pass, current_thread().query_index, function()
current_thread().page = current_query().start_page
private.current_thread().query_index = private.current_thread().query_index and next_query_index(private.current_thread().query_index) or start_query_index
if private.current_query() then
private.wait_for_callback{private.current_thread().params.on_start_query or Aux.util.pass, private.current_thread().query_index, function()
private.current_thread().page = private.current_query().start_page
return private.process_query()
end }
else
local on_complete = current_thread().params.on_complete
threads[Aux.control.thread_id] = nil
local on_complete = private.current_thread().params.on_complete
threads[private.current_thread().params.type] = nil
if on_complete then
return on_complete()
end
@@ -155,20 +153,20 @@ end
function private.process_query()
submit_query(function()
private.submit_query(function()
local count, _ = GetNumAuctionItems(current_thread().params.type)
local count, _ = GetNumAuctionItems(private.current_thread().params.type)
scan_auctions(count, function()
private.scan_auctions(count, function()
wait_for_callback{current_thread().params.on_page_scanned or Aux.util.pass, function()
if current_query().next_page then
current_thread().page = current_query().next_page(current_thread().page, current_thread().total_pages)
private.wait_for_callback{private.current_thread().params.on_page_scanned or Aux.util.pass, function()
if private.current_query().next_page then
private.current_thread().page = private.current_query().next_page(private.current_thread().page, private.current_thread().total_pages)
else
current_thread().page = private.default_next_page(current_thread().page, current_thread().total_pages)
private.current_thread().page = private.default_next_page(private.current_thread().page, private.current_thread().total_pages)
end
if current_thread().page then
if private.current_thread().page then
return private.process_query()
else
return private.scan()
@@ -178,65 +176,65 @@ function private.process_query()
end)
end
function scan_auctions(count, k)
return scan_auctions_helper(1, count, k)
function private.scan_auctions(count, k)
return private.scan_auctions_helper(1, count, k)
end
function scan_auctions_helper(i, n, k)
function private.scan_auctions_helper(i, n, k)
local recurse = function()
if i >= n then
return k()
else
return scan_auctions_helper(i + 1, n, k)
return private.scan_auctions_helper(i + 1, n, k)
end
end
local auction_info = Aux.info.auction(i, current_thread().params.type)
local auction_info = Aux.info.auction(i, private.current_thread().params.type)
if auction_info then
auction_info.index = i
auction_info.page = current_thread().page
auction_info.query = current_query()
auction_info.query_type = current_thread().params.type
auction_info.page = private.current_thread().page
auction_info.query = private.current_query()
auction_info.query_type = private.current_thread().params.type
Aux.history.process_auction(auction_info)
if not current_query().validator or current_query().validator(auction_info) then
return wait_for_callback{current_thread().params.on_read_auction or Aux.util.pass, auction_info, recurse }
if not private.current_query().validator or private.current_query().validator(auction_info) then
return private.wait_for_callback{private.current_thread().params.on_read_auction or Aux.util.pass, auction_info, recurse }
end
end
return recurse()
end
function submit_query(k)
if current_thread().page then
Aux.control.wait_until(function() return current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function()
function private.submit_query(k)
if private.current_thread().page then
Aux.control.wait_until(function() return private.current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function()
if current_thread().params.on_submit_query then
current_thread().params.on_submit_query()
if private.current_thread().params.on_submit_query then
private.current_thread().params.on_submit_query()
end
if current_thread().params.type == 'bidder' then
GetBidderAuctionItems(current_thread().page)
elseif current_thread().params.type == 'owner' then
GetOwnerAuctionItems(current_thread().page)
if private.current_thread().params.type == 'bidder' then
GetBidderAuctionItems(private.current_thread().page)
elseif private.current_thread().params.type == 'owner' then
GetOwnerAuctionItems(private.current_thread().page)
else
QueryAuctionItems(
Aux.util.safe_index{current_query(), 'blizzard_query', 'name'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'min_level'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'max_level'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'slot'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'class'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'subclass'},
current_thread().page,
Aux.util.safe_index{current_query(), 'blizzard_query', 'usable'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'quality'}
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'name'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'min_level'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'max_level'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'slot'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'class'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'subclass'},
private.current_thread().page,
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'usable'},
Aux.util.safe_index{private.current_query(), 'blizzard_query', 'quality'}
)
end
private.wait_for_results(function()
local _, total_count = GetNumAuctionItems(current_thread().params.type)
current_thread().total_pages = math.ceil(total_count / PAGE_SIZE)
if current_thread().total_pages >= current_thread().page + 1 then
wait_for_callback{current_thread().params.on_page_loaded or Aux.util.pass, current_thread().page, current_thread().total_pages, function()
local _, total_count = GetNumAuctionItems(private.current_thread().params.type)
private.current_thread().total_pages = math.ceil(total_count / PAGE_SIZE)
if private.current_thread().total_pages >= private.current_thread().page + 1 then
private.wait_for_callback{private.current_thread().params.on_page_loaded or Aux.util.pass, private.current_thread().page, private.current_thread().total_pages, function()
return k()
end}
else
+20 -22
View File
@@ -119,27 +119,25 @@ function public.stop()
end
function public.start(item_key, size, callback)
Aux.control.wait_until(function()
public.stop()
local slots = private.item_slots(item_key)
local target_slot = slots()
local thread_id = Aux.control.new_thread(private.process)
state = {
thread_id = thread_id,
target_size = size,
target_slot = target_slot,
other_slots = slots,
callback = callback,
}
if not target_slot then
public.stop()
local slots = private.item_slots(item_key)
local target_slot = slots()
local thread_id = Aux.control.new_thread(private.process)
state = {
thread_id = thread_id,
target_size = size,
target_slot = target_slot,
other_slots = slots,
callback = callback,
}
if not target_slot then
public.stop()
elseif private.item_charges(target_slot) then
state.target_slot = private.find_charges_item_slot(item_key, size)
public.stop()
end
end)
elseif private.item_charges(target_slot) then
state.target_slot = private.find_charges_item_slot(item_key, size)
public.stop()
end
end