refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-04 20:03:49 +02:00
parent 19f42cc9c9
commit 94488eb75f
8 changed files with 55 additions and 63 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ function private.scan_wdb(item_id)
if item_id <= m.MAX_ITEM_ID then if item_id <= m.MAX_ITEM_ID then
local t0 = GetTime() local t0 = GetTime()
aux.control.as_soon_as(function() return GetTime() - t0 > 0.1 end, m.scan_wdb, item_id) aux.control.thread(aux.control.when, function() return GetTime() - t0 > 0.1 end, m.scan_wdb, item_id)
else else
sort(aux_auctionable_items, function(a, b) return strlen(a) < strlen(b) or (strlen(a) == strlen(b) and a < b) end) sort(aux_auctionable_items, function(a, b) return strlen(a) < strlen(b) or (strlen(a) == strlen(b) and a < b) end)
end end
+6 -4
View File
@@ -7,7 +7,8 @@ function private.process()
local stacking_complete local stacking_complete
local c = aux.control.await(function(slot) local send_signal, signal_received = aux.util.signal()
aux.control.when(signal_received, function(slot)
if slot then if slot then
return m.post_auction(slot, m.process) return m.post_auction(slot, m.process)
else else
@@ -15,7 +16,7 @@ function private.process()
end end
end) end)
return aux.stack.start(m.state.item_key, m.state.stack_size, c) return aux.stack.start(m.state.item_key, m.state.stack_size, send_signal)
end end
return m.stop() return m.stop()
@@ -34,7 +35,8 @@ function private.post_auction(slot, k)
StartAuction(max(1, aux.util.round(m.state.unit_start_price * item_info.aux_quantity)), aux.util.round(m.state.unit_buyout_price * item_info.aux_quantity), m.state.duration) StartAuction(max(1, aux.util.round(m.state.unit_start_price * item_info.aux_quantity)), aux.util.round(m.state.unit_buyout_price * item_info.aux_quantity), m.state.duration)
local c = aux.control.await(function() local send_signal, signal_received = aux.util.signal()
aux.control.when(signal_received, function()
m.state.posted = m.state.posted + 1 m.state.posted = m.state.posted + 1
return k() return k()
end) end)
@@ -42,7 +44,7 @@ function private.post_auction(slot, k)
local posted local posted
aux.control.event_listener('CHAT_MSG_SYSTEM', function(kill) aux.control.event_listener('CHAT_MSG_SYSTEM', function(kill)
if arg1 == ERR_AUCTION_STARTED then if arg1 == ERR_AUCTION_STARTED then
c() send_signal()
kill() kill()
end end
end) end)
+28 -32
View File
@@ -2,8 +2,6 @@ local m, public, private = aux.module'scan'
private.PAGE_SIZE = 50 private.PAGE_SIZE = 50
private.last_query_time = {}
do do
local scan_states = {} local scan_states = {}
@@ -50,7 +48,8 @@ private.query = aux.dynamic_table(function()
end) end)
function private.wait_for_callback(...) function private.wait_for_callback(...)
local ok = true local send_signal, signal_received = aux.util.signal()
local suspended
local ret local ret
local f = tremove(arg, 1) local f = tremove(arg, 1)
@@ -58,17 +57,16 @@ function private.wait_for_callback(...)
if f then if f then
tinsert(arg, { tinsert(arg, {
suspend = function() ok = false end, suspend = function() suspended = true end,
resume = function(...) ok = true ret = arg end, resume = send_signal,
}) })
f(unpack(arg)) f(unpack(arg))
end end
if not suspended then
if ok then send_signal()
return k()
else
return aux.control.when(function() return ok end, function() return k(unpack(ret)) end)
end end
return aux.control.when(signal_received, function() return k(unpack(signal_received())) end)
end end
function private.total_pages(total_auctions) function private.total_pages(total_auctions)
@@ -106,7 +104,7 @@ end
function private.submit_query() function private.submit_query()
aux.control.when(function() return m.state.params.type ~= 'list' or CanSendAuctionQuery() end, function() aux.control.when(function() return m.state.params.type ~= 'list' or CanSendAuctionQuery() end, function()
aux.safe_call(m.state.params.on_submit_query) aux.safe_call(m.state.params.on_submit_query)
m.last_query_time[m.state.params.type] = GetTime() m.state.last_query_time = GetTime()
if m.state.params.type == 'bidder' then if m.state.params.type == 'bidder' then
GetBidderAuctionItems(m.state.page) GetBidderAuctionItems(m.state.page)
elseif m.state.params.type == 'owner' then elseif m.state.params.type == 'owner' then
@@ -156,9 +154,10 @@ function private.scan_page(i)
aux.history.process_auction(auction_info) aux.history.process_auction(auction_info)
if aux.safe_call(m.state.params.auto_buy_validator, auction_info) then if aux.safe_call(m.state.params.auto_buy_validator, auction_info) then
local c = aux.control.await(recurse) local send_signal, signal_received = aux.util.signal()
aux.place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, aux.f(c, true)) aux.control.when(signal_received, recurse)
return aux.control.thread(aux.control.sleep, 10, aux.f(c, false)) aux.place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, aux.f(send_signal, true))
return aux.control.thread(aux.control.when, aux.util.later(GetTime(), 10), aux.f(send_signal, false))
elseif not m.query.validator or m.query.validator(auction_info) then elseif not m.query.validator or m.query.validator(auction_info) then
return m.wait_for_callback(m.state.params.on_auction, auction_info, function(removed) return m.wait_for_callback(m.state.params.on_auction, auction_info, function(removed)
if removed then if removed then
@@ -173,13 +172,11 @@ function private.scan_page(i)
return recurse() return recurse()
end end
function private.timeout(type)
return GetTime() - m.last_query_time[type] > 11
end
function private.wait_for_results() function private.wait_for_results()
local c = aux.control.await(function() local timeout = aux.util.later(m.state.last_query_time, 10)
if m.timeout(m.state.params.type) then local send_signal, signal_received = aux.util.signal()
aux.control.when(signal_received, function()
if timeout() then
return m.submit_query() return m.submit_query()
else else
_, m.state.total_auctions = GetNumAuctionItems(m.state.params.type) _, m.state.total_auctions = GetNumAuctionItems(m.state.params.type)
@@ -193,40 +190,39 @@ function private.wait_for_results()
end end
end) end)
local type = m.state.params.type aux.control.thread(aux.control.when, timeout, send_signal)
aux.control.as_soon_as(function() return -c or m.timeout(type) end, c)
if m.state.params.type == 'bidder' then if m.state.params.type == 'bidder' then
return aux.control.as_soon_as(function() return aux.bids_loaded end, c) return aux.control.thread(aux.control.when, function() return aux.bids_loaded end, send_signal)
elseif m.state.params.type == 'owner' then elseif m.state.params.type == 'owner' then
return m.wait_for_owner_results(c) return m.wait_for_owner_results(send_signal)
elseif m.state.params.type == 'list' then elseif m.state.params.type == 'list' then
return m.wait_for_list_results(c) return m.wait_for_list_results(send_signal, signal_received)
end end
end end
function private.wait_for_owner_results(c) function private.wait_for_owner_results(send_signal)
if m.state.page == aux.current_owner_page then if m.state.page == aux.current_owner_page then
return c() return send_signal()
else else
return aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', c) return aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', send_signal)
end end
end end
function private.wait_for_list_results(c) function private.wait_for_list_results(send_signal, signal_received)
local updated, last_update local updated, last_update
aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function(kill) aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function(kill)
kill(-c) kill(signal_received())
last_update = GetTime() last_update = GetTime()
updated = true updated = true
end) end)
local ignore_owner = m.state.params.ignore_owner or aux_ignore_owner local ignore_owner = m.state.params.ignore_owner or aux_ignore_owner
return aux.control.as_soon_as(function() return aux.control.thread(aux.control.when, function()
-- short circuiting order important, owner_data_complete must be called iif an update has happened. -- short circuiting order important, owner_data_complete must be called iif an update has happened.
local ok = updated and (ignore_owner or m.owner_data_complete('list')) or last_update and GetTime() - last_update > 5 local ok = updated and (ignore_owner or m.owner_data_complete('list')) or last_update and GetTime() - last_update > 5
updated = false updated = false
return ok return ok
end, c) end, send_signal)
end end
function private.owner_data_complete(type) function private.owner_data_complete(type)
+1 -1
View File
@@ -1,5 +1,5 @@
SLASH_AUX1 = '/aux' SLASH_AUX1 = '/aux'
function SlashCmdList.aux(command) function SlashCmdList.AUX(command)
if not command then return end if not command then return end
local arguments = aux.util.tokenize(command) local arguments = aux.util.tokenize(command)
if arguments[1] == 'clear' and arguments[2] == 'history' then if arguments[1] == 'clear' and arguments[2] == 'history' then
-22
View File
@@ -68,34 +68,12 @@ function public.on_next_event(event, callback)
end) end)
end end
function public.as_soon_as(p, ...)
return m.thread(m.when, p, unpack(arg))
end
function public.thread(k, ...) function public.thread(k, ...)
local thread_id = aux.unique() local thread_id = aux.unique()
m.threads[thread_id] = { k = aux.f(k, unpack(arg)) } m.threads[thread_id] = { k = aux.f(k, unpack(arg)) }
return thread_id return thread_id
end end
function public.await(k)
local ret
m.when(function() return ret end, function() return k(unpack(ret)) end)
return setmetatable({}, {
__call = function(_, ...)
ret = arg
end,
__unm = function()
return ret ~= nil
end
})
end
function public.sleep(seconds, ...)
local t0 = GetTime()
return m.when(function() return GetTime() - t0 >= seconds end, unpack(arg))
end
function public.wait(k, ...) function public.wait(k, ...)
if type(k) == 'number' then if type(k) == 'number' then
m.when(function() k = k - 1 return k <= 1 end, unpack(arg)) m.when(function() k = k - 1 return k <= 1 end, unpack(arg))
+2 -2
View File
@@ -234,7 +234,7 @@ function private.start_real_time_scan(query, search, continuation)
if search.auto_buy then if search.auto_buy then
ctrl.suspend() ctrl.suspend()
aux.place_bid('list', auction_record.index, auction_record.buyout_price, aux.f(ctrl.resume, true)) aux.place_bid('list', auction_record.index, auction_record.buyout_price, aux.f(ctrl.resume, true))
aux.control.thread(aux.control.sleep, 10, aux.f(ctrl.resume, false)) aux.control.thread(aux.control.when, aux.util.later(GetTime(), 10), aux.f(ctrl.resume, false))
else else
tinsert(new_records, auction_record) tinsert(new_records, auction_record)
end end
@@ -330,7 +330,7 @@ function private.start_search(queries, continuation)
if search.auto_buy then if search.auto_buy then
ctrl.suspend() ctrl.suspend()
aux.place_bid('list', auction_record.index, auction_record.buyout_price, aux.f(ctrl.resume, true)) aux.place_bid('list', auction_record.index, auction_record.buyout_price, aux.f(ctrl.resume, true))
aux.control.thread(aux.control.sleep, 10, aux.f(ctrl.resume, false)) aux.control.thread(aux.control.when, aux.util.later(GetTime(), 10), aux.f(ctrl.resume, false))
elseif getn(search.records) < 1000 then elseif getn(search.records) < 1000 then
tinsert(search.records, auction_record) tinsert(search.records, auction_record)
if getn(search.records) == 1000 then if getn(search.records) == 1000 then
+16
View File
@@ -231,6 +231,22 @@ function public.bag_type(bag)
end end
end end
function public.later(t0, t)
return function()
return GetTime() - t0 > t
end
end
function public.signal()
local params
return function(...)
params = arg
end,
function()
return params
end
end
function public.without_errors(f) function public.without_errors(f)
local orig = UIErrorsFrame.AddMessage local orig = UIErrorsFrame.AddMessage
UIErrorsFrame.AddMessage = m.pass UIErrorsFrame.AddMessage = m.pass
+1 -1
View File
@@ -6,7 +6,7 @@ do
local realm, faction local realm, faction
function public.LOAD() function public.LOAD()
aux.control.as_soon_as(function() faction = UnitFactionGroup('player') return faction end, aux.util.pass) aux.control.thread(aux.control.when, function() faction = UnitFactionGroup('player') return faction end, aux.util.pass)
realm = GetCVar('realmName') realm = GetCVar('realmName')
end end