refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-07-31 15:14:55 +02:00
parent be200921ef
commit a0e0728298
6 changed files with 82 additions and 52 deletions
+12 -21
View File
@@ -19,17 +19,11 @@ function private.current_query()
end
function private.current_thread()
for _, thread in m.threads do
if thread.id == Aux.control.thread_id then
return thread
end
end
return Aux.util.filter(m.threads, function(thread) return thread.id == Aux.control.thread_id end)[1]
end
function public.start(params)
if m.threads[params.type] then
m.abort(m.threads[params.type].id)
end
Aux.safe(m).abort(Aux.safe(m).threads[params.type].id)
local thread_id = Aux.control.new_thread(Aux.f(m.wait_for_callback, params.on_scan_start, m.scan))
@@ -51,15 +45,13 @@ function public.abort(scan_id)
end
for _, thread in ipairs(aborted_threads) do
if thread.params.on_abort then
thread.params.on_abort()
end
Aux.safe(thread.params.on_abort)()
end
end
function private.wait_for_results(k)
if m.current_thread().params.type == 'bidder' then
return Aux.control.wait_until(function() return Aux.bids_loaded end, k)
return Aux.control.when(function() return Aux.bids_loaded end, k)
elseif m.current_thread().params.type == 'owner' then
return m.wait_for_owner_results(k)
elseif m.current_thread().params.type == 'list' then
@@ -85,7 +77,7 @@ function private.wait_for_list_results(k)
updated = true
end)
listener:start()
Aux.control.wait_until(function()
Aux.control.when(function()
-- short circuiting order important, owner_data_complete must be called iif an update has happened.
-- if no update has happened it must not be called for performance reasons, otherwise it must be called to request further missing data if there is any
local ok = updated and m.owner_data_complete() or last_update and GetTime() - last_update > 5
@@ -129,7 +121,7 @@ function private.wait_for_callback(...)
if ok then
return k()
else
return Aux.control.wait_until(function() return ok end, function() return k(unpack(ret)) end)
return Aux.control.when(function() return ok end, function() return k(unpack(ret)) end)
end
end
@@ -197,11 +189,11 @@ function private.scan_auctions_helper(i, k)
Aux.history.process_auction(auction_info)
if m.current_thread().params.auto_buy_validator and m.current_thread().params.auto_buy_validator(auction_info) then
if Aux.safe(m).current_thread().params.auto_buy_validator(auction_info)/false then
local c = Aux.control.wait_for(recurse)
Aux.place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, Aux.f(c, true))
Aux.control.new_thread(Aux.control.sleep, 10, Aux.f(c, false))
elseif not m.current_query().validator or m.current_query().validator(auction_info) then
elseif Aux.safe(m).current_query().validator(auction_info)/true then
return m.wait_for_callback(m.current_thread().params.on_auction, auction_info, function(removed)
if removed then
return recurse(true)
@@ -216,17 +208,16 @@ function private.scan_auctions_helper(i, k)
end
function private.submit_query(k)
Aux.control.wait_until(function() return m.current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function()
Aux.control.when(function() return m.current_thread().params.type ~= 'list' or CanSendAuctionQuery() end, function()
Aux.safe(m).current_thread().params.on_submit_query()
if m.current_thread().params.on_submit_query then
m.current_thread().params.on_submit_query()
end
if m.current_thread().params.type == 'bidder' then
GetBidderAuctionItems(m.current_thread().page)
elseif m.current_thread().params.type == 'owner' then
GetOwnerAuctionItems(m.current_thread().page)
else
local blizzard_query = Aux.safe(m.current_query().blizzard_query)/{}
local blizzard_query = Aux.safe(m).current_query().blizzard_query/{}
QueryAuctionItems(
blizzard_query.name,
blizzard_query.min_level,
+1 -1
View File
@@ -64,7 +64,7 @@ function private.move_item(from_slot, to_slot, amount, k)
SplitContainerItem(from_slot[1], from_slot[2], amount)
PickupContainerItem(unpack(to_slot))
return Aux.control.wait_until(function() return m.stack_size(to_slot) == expected_size end, k)
return Aux.control.when(function() return m.stack_size(to_slot) == expected_size end, k)
end
function private.process()
+6 -6
View File
@@ -84,7 +84,7 @@ function public.on_next_update(callback)
end
function public.as_soon_as(p, callback)
return m.new_thread(m.wait_until, p, callback)
return m.new_thread(m.when, p, callback)
end
function public.new_thread(k, ...)
@@ -101,7 +101,7 @@ end
function public.wait_for(k)
local ret
m.wait_until(function() return ret end, function() return k(unpack(ret)) end)
m.when(function() return ret end, function() return k(unpack(ret)) end)
return function(...)
ret = arg
end
@@ -109,21 +109,21 @@ end
function public.sleep(dt, ...)
local t0 = GetTime()
return m.wait_until(function() return GetTime() - t0 >= dt end, unpack(arg))
return m.when(function() return GetTime() - t0 >= dt end, unpack(arg))
end
function public.wait(k, ...)
if type(k) == 'number' then
m.wait_until(function() k = k - 1 return k <= 0 end, unpack(arg))
m.when(function() k = k - 1 return k <= 0 end, unpack(arg))
else
m.threads[m.thread_id].k = Aux.f(k, unpack(arg))
end
end
function public.wait_until(p, k, ...)
function public.when(p, k, ...)
if p() then
return k(unpack(arg))
else
return m.wait(m.wait_until, p, Aux.f(k, unpack(arg)))
return m.wait(m.when, p, Aux.f(k, unpack(arg)))
end
end
+53 -20
View File
@@ -57,28 +57,61 @@ function public.f(func, ...)
end
do
local safe_nil = setmetatable({}, {
__index = function(self) return self end,
__call = function(self) return self end,
__div = function(_, value) return value end
})
local data = {}
local eq = function() return true end
local proto = setmetatable({}, { __eq = eq })
local safe_nil
local function unsafe(object)
if object == proto then
return -object
else
return object
end
end
local safe_mt = {
__index = function(self, key)
key = unsafe(key)
if data[self] == nil or key == nil then
return safe_nil
else
return m.safe(data[self][key])
end
end,
__call = function(self, ...)
if data[self] == nil then
return safe_nil
end
for i=1,arg.n do
if arg[i] == proto and -arg[i] == nil then
return safe_nil
else
arg[i] = unsafe(arg[i])
end
end
return m.safe(data[self](unpack(arg)))
end,
__div = function(self, default)
if data[self] == nil then
return default
else
return data[self]
end
end,
__unm = function(self)
return data[self]
end,
__eq = eq,
}
function public.safe(object)
return (object == nil or object == safe_nil) and safe_nil or setmetatable({}, {
__index = function(_, key)
return m.safe(key) ~= safe_nil and m.safe(object[key]) or safe_nil
end,
__call = function(_, ...)
for i=1,arg.n do
if safe(arg[i]) == safe_nil then
return safe_nil
end
end
return object(unpack(arg))
end,
__div = function() return object end
})
local self = {}
data[self] = unsafe(object)
return setmetatable(self, safe_mt)
end
safe_nil = m.safe()
end
do
@@ -497,7 +530,7 @@ function public.hook(name, handler, object)
end
if orig[name] then
error('"'..name..'" is already hooked!')
error('"'..name..'" is already hooked.')
end
orig[name] = object[name]
+9 -3
View File
@@ -3,8 +3,14 @@ local m, public, private = Aux.module'util'
function public.pass()
end
function public.id(object)
return object
function public.id(value)
return value
end
function public.const(value)
return function()
return value
end
end
function public.size(table)
@@ -147,7 +153,7 @@ end
function public.filter(xs, p)
local ys = {}
for _, x in ipairs(xs) do
for _, x in xs do
if p(x) then
tinsert(ys, x)
end
+1 -1
View File
@@ -718,7 +718,7 @@ function public.validator(blizzard_filter, post_filter)
return function(record)
if blizzard_filter.exact and strlower(Aux.info.item(record.item_id).name) ~= blizzard_filter.name then
return
return false
end
if getn(post_filter) > 0 then
local stack = {}