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() private.update_inventory_records()
refresh = true -- if selected_item then
-- public.select_item(selected_item.key)
-- end
private.update_recommendation() private.update_recommendation()
end end
@@ -704,7 +706,6 @@ function private.set_item(item)
private.update_recommendation() private.update_recommendation()
refresh = true refresh = true
end end
function private.update_inventory_records() function private.update_inventory_records()
+59 -61
View File
@@ -6,8 +6,6 @@ local PAGE_SIZE = 50
local state local state
local threads = {} 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) function private.default_next_page(page, total_pages)
local last_page = max(total_pages - 1, 0) local last_page = max(total_pages - 1, 0)
if page < last_page then if page < last_page then
@@ -15,11 +13,11 @@ function private.default_next_page(page, total_pages)
end end
end end
function current_query() function private.current_query()
return current_thread().params.queries[current_thread().query_index] return private.current_thread().params.queries[private.current_thread().query_index]
end end
function current_thread() function private.current_thread()
for _, thread in pairs(threads) do for _, thread in pairs(threads) do
if thread.id == Aux.control.thread_id then if thread.id == Aux.control.thread_id then
return thread return thread
@@ -50,11 +48,11 @@ function public.abort(type)
end end
function private.wait_for_results(k) 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) 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) 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) return private.wait_for_list_results(k)
end end
end end
@@ -69,7 +67,7 @@ end
function private.wait_for_owner_results(k) function private.wait_for_owner_results(k)
local updated local updated
if current_thread().page == Aux.current_owner_page then if private.current_thread().page == Aux.current_owner_page then
updated = true updated = true
else else
Aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', function() Aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', function()
@@ -99,12 +97,12 @@ function private.wait_for_list_results(k)
end end
function private.owner_data_complete() function private.owner_data_complete()
if current_thread().params.no_wait_owner then if private.current_thread().params.no_wait_owner then
return true return true
end end
local count, _ = GetNumAuctionItems(current_thread().params.type) local count, _ = GetNumAuctionItems(private.current_thread().params.type)
for i=1,count do 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 if auction_info and not auction_info.owner then
return false return false
end end
@@ -112,7 +110,7 @@ function private.owner_data_complete()
return true return true
end 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 ok = true
local f = tremove(args, 1) local f = tremove(args, 1)
@@ -135,18 +133,18 @@ end
function private.scan() function private.scan()
local start_query_index = current_thread().params.start_query_index or 1 local start_query_index = private.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 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 private.current_thread().query_index = private.current_thread().query_index and next_query_index(private.current_thread().query_index) or start_query_index
if current_query() then if private.current_query() then
wait_for_callback{current_thread().params.on_start_query or Aux.util.pass, current_thread().query_index, function() private.wait_for_callback{private.current_thread().params.on_start_query or Aux.util.pass, private.current_thread().query_index, function()
current_thread().page = current_query().start_page private.current_thread().page = private.current_query().start_page
return private.process_query() return private.process_query()
end } end }
else else
local on_complete = current_thread().params.on_complete local on_complete = private.current_thread().params.on_complete
threads[Aux.control.thread_id] = nil threads[private.current_thread().params.type] = nil
if on_complete then if on_complete then
return on_complete() return on_complete()
end end
@@ -155,20 +153,20 @@ end
function private.process_query() 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() private.wait_for_callback{private.current_thread().params.on_page_scanned or Aux.util.pass, function()
if current_query().next_page then if private.current_query().next_page then
current_thread().page = current_query().next_page(current_thread().page, current_thread().total_pages) private.current_thread().page = private.current_query().next_page(private.current_thread().page, private.current_thread().total_pages)
else 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 end
if current_thread().page then if private.current_thread().page then
return private.process_query() return private.process_query()
else else
return private.scan() return private.scan()
@@ -178,65 +176,65 @@ function private.process_query()
end) end)
end end
function scan_auctions(count, k) function private.scan_auctions(count, k)
return scan_auctions_helper(1, count, k) return private.scan_auctions_helper(1, count, k)
end end
function scan_auctions_helper(i, n, k) function private.scan_auctions_helper(i, n, k)
local recurse = function() local recurse = function()
if i >= n then if i >= n then
return k() return k()
else else
return scan_auctions_helper(i + 1, n, k) return private.scan_auctions_helper(i + 1, n, k)
end end
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 if auction_info then
auction_info.index = i auction_info.index = i
auction_info.page = current_thread().page auction_info.page = private.current_thread().page
auction_info.query = current_query() auction_info.query = private.current_query()
auction_info.query_type = current_thread().params.type auction_info.query_type = private.current_thread().params.type
Aux.history.process_auction(auction_info) Aux.history.process_auction(auction_info)
if not current_query().validator or current_query().validator(auction_info) then if not private.current_query().validator or private.current_query().validator(auction_info) then
return wait_for_callback{current_thread().params.on_read_auction or Aux.util.pass, auction_info, recurse } return private.wait_for_callback{private.current_thread().params.on_read_auction or Aux.util.pass, auction_info, recurse }
end end
end end
return recurse() return recurse()
end end
function submit_query(k) function private.submit_query(k)
if current_thread().page then if private.current_thread().page then
Aux.control.wait_until(function() return current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function() Aux.control.wait_until(function() return private.current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function()
if current_thread().params.on_submit_query then if private.current_thread().params.on_submit_query then
current_thread().params.on_submit_query() private.current_thread().params.on_submit_query()
end end
if current_thread().params.type == 'bidder' then if private.current_thread().params.type == 'bidder' then
GetBidderAuctionItems(current_thread().page) GetBidderAuctionItems(private.current_thread().page)
elseif current_thread().params.type == 'owner' then elseif private.current_thread().params.type == 'owner' then
GetOwnerAuctionItems(current_thread().page) GetOwnerAuctionItems(private.current_thread().page)
else else
QueryAuctionItems( QueryAuctionItems(
Aux.util.safe_index{current_query(), 'blizzard_query', 'name'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'name'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'min_level'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'min_level'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'max_level'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'max_level'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'slot'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'slot'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'class'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'class'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'subclass'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'subclass'},
current_thread().page, private.current_thread().page,
Aux.util.safe_index{current_query(), 'blizzard_query', 'usable'}, Aux.util.safe_index{private.current_query(), 'blizzard_query', 'usable'},
Aux.util.safe_index{current_query(), 'blizzard_query', 'quality'} Aux.util.safe_index{private.current_query(), 'blizzard_query', 'quality'}
) )
end end
private.wait_for_results(function() private.wait_for_results(function()
local _, total_count = GetNumAuctionItems(current_thread().params.type) local _, total_count = GetNumAuctionItems(private.current_thread().params.type)
current_thread().total_pages = math.ceil(total_count / PAGE_SIZE) private.current_thread().total_pages = math.ceil(total_count / PAGE_SIZE)
if current_thread().total_pages >= current_thread().page + 1 then if private.current_thread().total_pages >= private.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() 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() return k()
end} end}
else else
+20 -22
View File
@@ -119,27 +119,25 @@ function public.stop()
end end
function public.start(item_key, size, callback) 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() public.stop()
elseif private.item_charges(target_slot) then
local slots = private.item_slots(item_key) state.target_slot = private.find_charges_item_slot(item_key, size)
local target_slot = slots() public.stop()
end
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)
end end