refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-02 14:24:59 +02:00
parent 82faab92e7
commit 54a2846f62
3 changed files with 77 additions and 53 deletions
+15 -15
View File
@@ -5,26 +5,26 @@ local PAGE_SIZE = 50
private.threads = {}
private.last_query_time = {}
private.th = Aux.dynamic_table(function()
return Aux.util.filter(m.threads, function(thread) return thread.id == Aux.control.thread_id end)[1]
end)
private.q = Aux.dynamic_table(function()
return m.th.params.queries[m.th.query_index]
end)
function private.total_pages(total_auctions)
return math.ceil(total_auctions / PAGE_SIZE)
end
function private.last_page(total_auctions)
local last_page = max(m.total_pages(total_auctions) - 1, 0)
local last_page_limit = Aux.safe(m.q.blizzard_query).last_page/last_page
local last_page_limit = Aux.index(m.q.blizzard_query).last_page/last_page
return min(last_page_limit, last_page)
end
private.th = Aux.t(function()
return Aux.util.filter(m.threads, function(thread) return thread.id == Aux.control.thread_id end)[1]
end)
private.q = Aux.t(function()
return m.th.params.queries[m.th.query_index]
end)
function public.start(params)
m.abort(Aux.safe(m.threads[params.type]).id/0)
m.abort(Aux.index(m.threads[params.type]).id/0)
local thread_id = Aux.control.new_thread(Aux.f(m.wait_for_callback, params.on_scan_start, m.scan))
@@ -46,7 +46,7 @@ function public.abort(scan_id)
end
for _, thread in ipairs(aborted_threads) do
Aux.safe(thread.params.on_abort)()
Aux.call(thread.params.on_abort)
end
end
@@ -194,11 +194,11 @@ function private.scan_page_helper(i)
Aux.history.process_auction(auction_info)
if Aux.safe(m.th.params.auto_buy_validator)(auction_info)/false then
if Aux.call(m.th.params.auto_buy_validator, auction_info)/false then
local c = Aux.control.await(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 Aux.safe(m.q.validator)(auction_info)/true then
elseif Aux.call(m.q.validator, auction_info)/true then
return m.wait_for_callback(m.th.params.on_auction, auction_info, function(removed)
if removed then
return recurse(true)
@@ -214,14 +214,14 @@ end
function private.submit_query()
Aux.control.when(function() return m.th.params.type ~= 'list' or CanSendAuctionQuery() end, function()
Aux.safe(m.th.params.on_submit_query)()
Aux.call(m.th.params.on_submit_query)
m.last_query_time[m.th.params.type] = GetTime()
if m.th.params.type == 'bidder' then
GetBidderAuctionItems(m.th.page)
elseif m.th.params.type == 'owner' then
GetOwnerAuctionItems(m.th.page)
else
local blizzard_query = Aux.safe(m.q.blizzard_query)/{}
local blizzard_query = Aux.option(m.q.blizzard_query)/{}
QueryAuctionItems(
blizzard_query.name,
blizzard_query.min_level,
+59 -35
View File
@@ -46,58 +46,82 @@ function public.set(table, key, value)
table[key] = value
end
function public.t(func)
return setmetatable({}, {
__newindex = function(_, key, value)
func()[key] = value
end,
__index = function(_, key)
if func() == nil then error('', 2) end
return func()[key]
end,
__call = function()
return func()
end,
})
end
function public.f(func, ...)
function public.f(f, ...)
local params = arg
return function(...)
for i=1,arg.n do
tinsert(params, arg[i])
end
return func(unpack(params))
return f(unpack(params))
end
end
function public.safe(value)
return setmetatable({},{
do
local dynamic_table_mt = {
__newindex = function(self, key, value)
rawget(self, '_f')()[key] = value
end,
__index = function(self, key)
if value ~= nil and key ~= nil then
return m.safe(value[key])
else
return m.safe(nil)
return rawget(self, '_f')()[key]
end,
__call = function(self)
return rawget(self, '_f')()
end,
}
function public.dynamic_table(f)
return setmetatable({ _f=f }, dynamic_table_mt)
end
end
function public.call(f, ...)
return m.option(f)..function()
return m.option(f(unpack(arg)))
end
end
do
local index_mt = {
__index = function(self, key)
return m.option(rawget(self, '_value'))..function()
return m.option(rawget(self, '_value')[key])
end
end,
__call = function(self, ...)
if value ~= nil then
if type(value) ~= 'function' then
error('', 2)
end
return m.safe(value(unpack(arg)))
}
function public.index(value)
return setmetatable({ _value=value }, index_mt)
end
end
do
local option_mt = {
__call = function(self, f)
if rawget(self, '_value') ~= nil then
return m.option(f(rawget(self, '_value')))
else
return m.safe(nil)
return self
end
end,
__div = function(self, alt)
if value ~= nil then
return value
if rawget(self, '_value') ~= nil then
return rawget(self, '_value')
else
return alt
end
end,
})
__unm = function(self, alt)
return rawget(self, '_value') ~= nil
end,
__concat = function(self, f)
if rawget(self, '_value') ~= nil then
return f(rawget(self, '_value'))
else
return self
end
end,
}
function public.option(value)
return setmetatable({ _value=value }, option_mt)
end
end
do
@@ -172,7 +196,7 @@ function public.on_event()
if event == 'VARIABLES_LOADED' then
m.on_load()
elseif event == 'ADDON_LOADED' then
m.safe(m.on_addon_load[arg1])()
m.call(m.on_addon_load[arg1])
elseif event == 'AUCTION_HOUSE_SHOW' then
m.on_auction_house_show()
elseif event == 'AUCTION_HOUSE_CLOSED' then
@@ -464,7 +488,7 @@ end
function public.is_player(name, current)
local realm = GetCVar('realmName')
return (not current and m.safe(aux_characters)[realm][name]/false) or UnitName('player') == name
return (not current and m.index(aux_characters[realm])[name]/false) or UnitName('player') == name
end
function public.unmodified()
+3 -3
View File
@@ -475,7 +475,7 @@ do
search.table:RemoveAuctionRecord(record)
end,
function(index)
if Aux.safe(search.table:GetSelection()).record/nil ~= record then
if Aux.index(search.table:GetSelection()).record/nil ~= record then
return
end
@@ -731,11 +731,11 @@ function private.initialize_filter_dropdown()
local function on_click()
UIDropDownMenu_SetSelectedValue(m.filter_dropdown, this.value)
m.filter_button:SetText(this.value)
if Aux.safe(Aux.filter.filters[this.value]).input_type/'' == '' and this.value ~= 'and' and this.value ~= 'or' then
if Aux.index(Aux.filter.filters[this.value]).input_type/'' == '' and this.value ~= 'and' and this.value ~= 'or' then
m.filter_input:Hide()
else
local _, _, suggestions = Aux.filter.parse_query_string(UIDropDownMenu_GetSelectedValue(m.filter_dropdown)..'/')
m.filter_input:SetNumeric(Aux.safe(Aux.filter.filters[this.value]).input_type/'number' == 'number')
m.filter_input:SetNumeric(Aux.index(Aux.filter.filters[this.value]).input_type/'number' == 'number')
m.filter_input.complete = Aux.completion.complete(function() return suggestions or {} end)
m.filter_input:Show()
m.filter_input:SetFocus()