module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+45 -45
View File
@@ -1,12 +1,12 @@
module 'aux.util.filter'
include 'T'
include 'aux'
local T = require 'T'
local info = require 'aux.util.info'
local money = require 'aux.util.money'
local filter_util = require 'aux.util.filter'
local cache = require 'aux.core.cache'
local history = require 'aux.core.history'
local disenchant = require 'aux.core.disenchant'
@@ -51,7 +51,7 @@ M.filters = {
},
['left'] = {
input_type = A('30m', '2h', '8h', '24h'),
input_type = T.list('30m', '2h', '8h', '24h'),
validator = function(index)
return function(auction_record)
return auction_record.duration == index
@@ -60,7 +60,7 @@ M.filters = {
},
['rarity'] = {
input_type = A('poor', 'common', 'uncommon', 'rare', 'epic'),
input_type = T.list('poor', 'common', 'uncommon', 'rare', 'epic'),
validator = function(index)
return function(auction_record)
return auction_record.quality == index - 1
@@ -166,7 +166,7 @@ M.filters = {
input_type = 'money',
validator = function(amount)
return function(auction_record)
local vendor_price = cache.merchant_info(auction_record.item_id)
local vendor_price = info.merchant_info(auction_record.item_id)
return vendor_price and vendor_price * auction_record.aux_quantity - auction_record.bid_price >= amount
end
end
@@ -176,7 +176,7 @@ M.filters = {
input_type = 'money',
validator = function(amount)
return function(auction_record)
local vendor_price = cache.merchant_info(auction_record.item_id)
local vendor_price = info.merchant_info(auction_record.item_id)
return auction_record.buyout_price > 0 and vendor_price and vendor_price * auction_record.aux_quantity - auction_record.buyout_price >= amount
end
end
@@ -184,12 +184,12 @@ M.filters = {
}
function operator(str)
local operator = str == 'not' and A('operator', 'not', 1)
for name in pairs(temp-S('and', 'or')) do
local operator = str == 'not' and T.list('operator', 'not', 1)
for name in pairs(T.temp-T.set('and', 'or')) do
local arity = select(3, strfind(str, '^' .. name .. '(%d*)$'))
if arity then
arity = tonumber(arity)
operator = not (arity and arity < 2) and A('operator', name, arity)
operator = not (arity and arity < 2) and T.list('operator', name, arity)
end
end
return operator or nil
@@ -206,41 +206,41 @@ do
local number = tonumber(select(3, strfind(str, '^(%d+)$')))
if number then
if number >= 1 and number <= 60 then
for _, key in ipairs(temp-A('min_level', 'max_level')) do
for _, key in ipairs(T.temp-T.list('min_level', 'max_level')) do
if not self[key] then
self[key] = A(str, number)
return A('blizzard', key, str, number)
self[key] = T.list(str, number)
return T.list('blizzard', key, str, number)
end
end
end
end
for _, parser in pairs(temp-A(
temp-A('class', info.item_class_index),
temp-A('subclass', vararg-function(arg) return info.item_subclass_index(index(self.class, 2) or 0, unpack(arg)) end),
temp-A('slot', vararg-function(arg) return info.item_slot_index(index(self.class, 2) == 2 and 2 or 0, index(self.subclass, 2) or 0, unpack(arg)) end),
temp-A('quality', info.item_quality_index)
for _, parser in pairs(T.temp-T.list(
T.temp-T.list('class', info.item_class_index),
T.temp-T.list('subclass', T.vararg-function(arg) return info.item_subclass_index(index(self.class, 2) or 0, unpack(arg)) end),
T.temp-T.list('slot', T.vararg-function(arg) return info.item_slot_index(index(self.class, 2) == 2 and 2 or 0, index(self.subclass, 2) or 0, unpack(arg)) end),
T.temp-T.list('quality', info.item_quality_index)
)) do
if not self[parser[1]] then
tinsert(parser, str)
local index, label = parser[2](select(3, unpack(parser)))
if index then
self[parser[1]] = A(label, index)
return A('blizzard', parser[1], label, index)
self[parser[1]] = T.list(label, index)
return T.list('blizzard', parser[1], label, index)
end
end
end
if not self[str] and (str == 'usable' or str == 'exact' and self.name and size(self) == 1) then
self[str] = A(str, 1)
return A('blizzard', str, str, 1)
self[str] = T.list(str, 1)
return T.list('blizzard', str, str, 1)
elseif i == 1 and strlen(str) <= 63 then
self.name = unquote(str)
return A('blizzard', 'name', unquote(str), str)
return T.list('blizzard', 'name', unquote(str), str)
-- return nil, 'The name filter must not be longer than 63 characters' TODO
end
end,
}
function blizzard_filter_parser()
return setmetatable(T, mt)
return setmetatable(T.acquire(), mt)
end
end
@@ -259,10 +259,10 @@ function parse_parameter(input_type, str)
end
function M.parse_filter_string(str)
local filter, post_filter = T, T
local filter, post_filter = T.acquire(), T.acquire()
local blizzard_filter_parser = blizzard_filter_parser()
local parts = str and map(split(str, '/'), function(part) return strlower(trim(part)) end) or T
local parts = str and map(split(str, '/'), function(part) return strlower(trim(part)) end) or T.acquire()
local i = 1
while parts[i] do
@@ -282,10 +282,10 @@ function M.parse_filter_string(str)
return nil, 'Invalid input for ' .. parts[i] .. '. Expecting: ' .. input_type
end
end
tinsert(post_filter, A('filter', parts[i], parts[i + 1]))
tinsert(post_filter, T.list('filter', parts[i], parts[i + 1]))
i = i + 1
else
tinsert(post_filter, A('filter', parts[i]))
tinsert(post_filter, T.list('filter', parts[i]))
end
tinsert(filter, post_filter[getn(post_filter)])
else
@@ -293,7 +293,7 @@ function M.parse_filter_string(str)
if part then
tinsert(filter, part)
elseif parts[i] ~= '' then
tinsert(post_filter, A('filter', 'tooltip', parts[i]))
tinsert(post_filter, T.list('filter', 'tooltip', parts[i]))
tinsert(filter, post_filter[getn(post_filter)])
else
return nil, 'Empty modifier'
@@ -302,14 +302,14 @@ function M.parse_filter_string(str)
i = i + 1
end
return O('components', filter, 'blizzard', blizzard_filter_parser(), 'post', post_filter)
return T.map('components', filter, 'blizzard', blizzard_filter_parser(), 'post', post_filter)
end
function M.query(filter_string)
local filter, error, suggestions = parse_filter_string(filter_string)
if not filter then
return nil, suggestions or T, error
return nil, suggestions or T.acquire(), error
end
local polish_notation_counter = 0
@@ -323,7 +323,7 @@ function M.query(filter_string)
end
if polish_notation_counter > 0 then
local suggestions = T
local suggestions = T.acquire()
for key in pairs(filters) do
tinsert(suggestions, strlower(key))
end
@@ -342,7 +342,7 @@ end
function M.queries(filter_string)
local parts = split(filter_string, ';')
local queries = T
local queries = T.acquire()
for _, str in ipairs(parts) do
local query, _, error = query(str)
if not query then
@@ -355,7 +355,7 @@ function M.queries(filter_string)
end
function suggestions(filter)
local suggestions = T
local suggestions = T.acquire()
if filter.blizzard.name and size(filter.blizzard) == 1 then tinsert(suggestions, 'exact') end
@@ -365,19 +365,19 @@ function suggestions(filter)
-- classes
if not filter.blizzard.class then
for _, class in ipairs(temp-A(GetAuctionItemClasses())) do tinsert(suggestions, class) end
for _, class in ipairs(T.temp-T.list(GetAuctionItemClasses())) do tinsert(suggestions, class) end
end
-- subclasses
if not filter.blizzard.subclass then
for _, subclass in ipairs(temp-A(GetAuctionItemSubClasses(index(filter.blizzard.class, 2) or 0))) do
for _, subclass in ipairs(T.temp-T.list(GetAuctionItemSubClasses(index(filter.blizzard.class, 2) or 0))) do
tinsert(suggestions, subclass)
end
end
-- slots
if not filter.blizzard.slot then
for _, invtype in ipairs(temp-A(GetAuctionInvTypes(index(filter.blizzard.class, 2) == 2 and 2 or 0, index(filter.blizzard.subclass, 2) or 0))) do
for _, invtype in ipairs(T.temp-T.list(GetAuctionInvTypes(index(filter.blizzard.class, 2) == 2 and 2 or 0, index(filter.blizzard.subclass, 2) or 0))) do
tinsert(suggestions, _G[invtype])
end
end
@@ -427,7 +427,7 @@ function prettified_filter_string(filter)
if component[1] == 'blizzard' then
if component[2] == 'name' then
if filter.blizzard.exact then
prettified.append(info.display_name(cache.item_id(component[3])) or color.orange('[' .. component[3] .. ']'))
prettified.append(info.display_name(info.item_id(component[3])) or color.orange('[' .. component[3] .. ']'))
elseif component[3] ~= '' then
prettified.append(color.label.enabled(component[3]))
end
@@ -443,7 +443,7 @@ function prettified_filter_string(filter)
local parameter = component[3]
if parameter then
if component[2] == 'item' then
prettified.append(info.display_name(cache.item_id(parameter)) or color.label.enabled('[' .. parameter .. ']'))
prettified.append(info.display_name(info.item_id(parameter)) or color.label.enabled('[' .. parameter .. ']'))
else
if filters[component[2]].input_type == 'money' then
prettified.append(money.to_string(money.from_string(parameter), nil, true, color.label.enabled))
@@ -471,9 +471,9 @@ end
function blizzard_query(filter)
local filters = filter.blizzard
local query = O('name', filters.name)
local query = T.map('name', filters.name)
local item_info, class_index, subclass_index, slot_index
local item_id = filters.name and cache.item_id(filters.name)
local item_id = filters.name and info.item_id(filters.name)
item_info = item_id and info.item(item_id)
if filters.exact and item_info then
class_index = info.item_class_index(item_info.class)
@@ -489,7 +489,7 @@ function blizzard_query(filter)
query.slot = slot_index
query.quality = item_info.quality
else
for key in pairs(temp-S('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality')) do
for key in pairs(T.temp-T.set('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality')) do
query[key] = index(filters[key], 2)
end
end
@@ -497,7 +497,7 @@ function blizzard_query(filter)
end
function validator(filter)
local validators = T
local validators = T.acquire()
for i, component in pairs(filter.post) do
local type, name, param = unpack(component)
if type == 'filter' then
@@ -508,11 +508,11 @@ function validator(filter)
if filter.blizzard.exact and strlower(info.item(record.item_id).name) ~= filter.blizzard.name then
return false
end
local stack = temp-T
local stack = T.temp-T.acquire()
for i = getn(filter.post), 1, -1 do
local type, name, param = unpack(filter.post[i])
if type == 'operator' then
local args = temp-T
local args = T.temp-T.acquire()
while (not param or param > 0) and getn(stack) > 0 do
tinsert(args, tremove(stack))
param = param and param - 1
@@ -534,7 +534,7 @@ end
function M.query_builder()
local filter
return O(
return T.map(
'append', function(part)
filter = not filter and part or filter .. '/' .. part
end,