refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-09 14:53:05 +02:00
parent 44d0b46aa8
commit a1edc53bfe
6 changed files with 64 additions and 79 deletions
+3 -3
View File
@@ -37,13 +37,13 @@ do
aux.call(on_complete) aux.call(on_complete)
end end
private.state = aux.dynamic_table(function() private.state = aux.dynamic_table(scan_states, function(self)
local _, state = next(aux.util.filter(scan_states, function(state) return state.id == aux.control.thread_id end)) local _, state = next(aux.util.filter(self.private, function(state) return state.id == aux.control.thread_id end))
return state return state
end) end)
end end
private.query = aux.dynamic_table(function() private.query = aux.dynamic_table(nil, function()
return m.state.params.queries[m.state.query_index] return m.state.params.queries[m.state.query_index]
end) end)
+15 -12
View File
@@ -5,33 +5,36 @@ public.current_owner_page = nil
public.last_owner_page_requested = nil public.last_owner_page_requested = nil
do do
local data = {}
local mt = { local mt = {
__newindex = function(self, key, value) __newindex = function(self, key, value)
self._getter()[key] = value data[self].getter({public=self, private=data[self].state})[key] = value
end, end,
__index = function(self, key) __index = function(self, key)
return self._getter()[key] return data[self].getter({public=self, private=data[self].state})[key]
end, end,
__call = function(self) __call = function(self)
return self._getter() return data[self].getter({public=self, private=data[self].state})
end, end,
} }
function public.dynamic_table(getter) function public.dynamic_table(state, getter)
return setmetatable({_getter=getter}, mt) local self = {}
data[self] = {getter=getter, state=state}
return setmetatable(self, mt)
end end
end end
do do
local data = {}
local mt = { local mt = {
__index = function(self, key) __index = function(self, key)
return self:_cb(key) return data[self].handler({public=self, private=data[self].state}, key)
end,
__call = function(self)
return self:_cb()
end, end,
} }
function public.index_function(cb) function public.index_function(state, handler)
return setmetatable({_cb = cb}, mt) local self = {}
data[self] = {handler=handler, state=state}
return setmetatable(self, mt)
end end
end end
@@ -49,7 +52,7 @@ function public.tab(index, title, name)
end end
do do
local active_tab_index local active_tab_index
private.active_tab = m.dynamic_table(function() private.active_tab = m.dynamic_table(nil, function()
return m.tabs[active_tab_index] return m.tabs[active_tab_index]
end) end)
function private.on_tab_click(index) function private.on_tab_click(index)
+7 -16
View File
@@ -21,24 +21,15 @@ public.config = {
huge_font_size = 23, huge_font_size = 23,
} }
do
local self = aux_module()
function private.color2(r, g, b, a)
return aux.index_function(function(_, key)
end)
end
end
function private.color_accessor(callback) function private.color_accessor(callback)
return aux.index_function(function(_, key) return aux.index_function({callback=callback}, function(self, key)
local t = m.config.color self.private.table = m.config.color
return aux.index_function(function(self, key) return aux.index_function(self.private, function(self, key)
t = t[key] self.private.table = self.private.table[key]
if getn(t) == 0 then if getn(self.private.table) == 0 then
return self return self.public
else else
return callback(aux.util.copy(t)) return callback(aux.util.copy(self.private.table))
end end
end)[key] end)[key]
end) end)
+16 -16
View File
@@ -1,12 +1,12 @@
local __ = {} local state = {}
local public_interface_mt = { local public_interface_mt = {
__newindex = function() __newindex = function()
error('Unsupported operation.', 2) error('Unsupported operation.', 2)
end, end,
__index = function(self, key) __index = function(self, key)
if __[self].public[key] then if state[self].public[key] then
return __[self].attributes[key] return state[self].data[key]
else else
error('Read of undeclared "'..key..'".', 2) error('Read of undeclared "'..key..'".', 2)
end end
@@ -14,26 +14,26 @@ local public_interface_mt = {
} }
local private_interface_mt = { local private_interface_mt = {
__newindex = function(self, key, value) __newindex = function(self, key, value)
if not __[self].declared[key] then if not state[self].declared[key] then
error('Write of undeclared "'..key..'".', 2) error('Write of undeclared "'..key..'".', 2)
end end
__[self].attributes[key] = value state[self].data[key] = value
end, end,
__index = function(self, key) __index = function(self, key)
if not __[self].declared[key] then if not state[self].declared[key] then
error('Read of undeclared "'..key..'".', 2) error('Read of undeclared "'..key..'".', 2)
end end
return __[self].attributes[key] return state[self].data[key]
end, end,
} }
local public_declarator_mt = { local public_declarator_mt = {
__newindex = function(self, key, value) __newindex = function(self, key, value)
if __[self].declared[key] then if state[self].declared[key] then
error('Multiple declarations of "'..key..'".', 2) error('Multiple declarations of "'..key..'".', 2)
end end
__[self].attributes[key] = value state[self].data[key] = value
__[self].public[key] = true state[self].public[key] = true
__[self].declared[key] = true state[self].declared[key] = true
end, end,
__index = function() __index = function()
error('Unsupported operation.', 2) error('Unsupported operation.', 2)
@@ -41,11 +41,11 @@ local public_declarator_mt = {
} }
local private_declarator_mt = { local private_declarator_mt = {
__newindex = function(self, key, value) __newindex = function(self, key, value)
if __[self].declared[key] then if state[self].declared[key] then
error('Multiple declarations of "'..key..'".', 2) error('Multiple declarations of "'..key..'".', 2)
end end
__[self].attributes[key] = value state[self].data[key] = value
__[self].declared[key] = true state[self].declared[key] = true
end, end,
__index = function() __index = function()
error('Unsupported operation.', 2) error('Unsupported operation.', 2)
@@ -53,10 +53,10 @@ local private_declarator_mt = {
} }
function aux_module() function aux_module()
local data = {attributes={}, public={}, declared={}} local new_state = {data={}, public={}, declared={}}
local module = {setmetatable({}, public_interface_mt), setmetatable({}, private_interface_mt), setmetatable({}, public_declarator_mt), setmetatable({}, private_declarator_mt)} local module = {setmetatable({}, public_interface_mt), setmetatable({}, private_interface_mt), setmetatable({}, public_declarator_mt), setmetatable({}, private_declarator_mt)}
for _, component in module do for _, component in module do
__[component] = data state[component] = new_state
end end
return module return module
end end
+2 -11
View File
@@ -8,20 +8,11 @@ function public.pack(table, ...)
end end
function public.unpack(array, ...) function public.unpack(array, ...)
local table, index = {}, {} local table = {}
for i=1,arg.n do for i=1,arg.n do
table[arg[i]] = array[i] table[arg[i]] = array[i]
tinsert(index, arg[i])
end end
return table, index return table
end
function public.pick(indices, ...)
local t = {}
for _, index in indices do
tinsert(t, arg[index])
end
return unpack(t)
end end
function public.select(i, ...) function public.select(i, ...)
+21 -21
View File
@@ -8,14 +8,14 @@ do
local COPPER_PER_SILVER = 100 local COPPER_PER_SILVER = 100
local COPPER_PER_GOLD = 10000 local COPPER_PER_GOLD = 10000
function public.to_GSC(money) function public.gsc(money)
local gold = floor(money / COPPER_PER_GOLD) local gold = floor(money / COPPER_PER_GOLD)
local silver = floor(mod(money, COPPER_PER_GOLD) / COPPER_PER_SILVER) local silver = floor(mod(money, COPPER_PER_GOLD) / COPPER_PER_SILVER)
local copper = mod(money, COPPER_PER_SILVER) local copper = mod(money, COPPER_PER_SILVER)
return gold, silver, copper return gold, silver, copper
end end
function public.from_GSC(gold, silver, copper) function public.copper(gold, silver, copper)
return gold * COPPER_PER_GOLD + silver * COPPER_PER_SILVER + copper return gold * COPPER_PER_GOLD + silver * COPPER_PER_SILVER + copper
end end
end end
@@ -25,43 +25,43 @@ function public.format(money, exact, color)
local TEXT_NONE = '0' local TEXT_NONE = '0'
local GSC_GOLD = 'ffd100' local GOLD = 'ffd100'
local GSC_SILVER = 'e6e6e6' local SILVER = 'e6e6e6'
local GSC_COPPER = 'c8602c' local COPPER = 'c8602c'
local GSC_START = '|cff%s%d'..FONT_COLOR_CODE_CLOSE local START = '|cff%s%d'..FONT_COLOR_CODE_CLOSE
local GSC_PART = color..'.|cff%s%02d'..FONT_COLOR_CODE_CLOSE local PART = color..'.|cff%s%02d'..FONT_COLOR_CODE_CLOSE
local GSC_NONE = '|cffa0a0a0'..TEXT_NONE..FONT_COLOR_CODE_CLOSE local NONE = '|cffa0a0a0'..TEXT_NONE..FONT_COLOR_CODE_CLOSE
if not exact and money >= 10000 then if not exact and money >= 10000 then
-- Round to nearest silver -- Round to nearest silver
money = floor(money / 100 + 0.5) * 100 money = floor(money / 100 + 0.5) * 100
end end
local g, s, c = aux.money.to_GSC(money) local g, s, c = aux.money.gsc(money)
local gsc = '' local str = ''
local fmt = GSC_START local fmt = START
if g > 0 then if g > 0 then
gsc = gsc..format(fmt, GSC_GOLD, g) str = str..format(fmt, GOLD, g)
fmt = GSC_PART fmt = PART
end end
if s > 0 or c > 0 then if s > 0 or c > 0 then
gsc = gsc..format(fmt, GSC_SILVER, s) str = str..format(fmt, SILVER, s)
fmt = GSC_PART fmt = PART
end end
if c > 0 then if c > 0 then
gsc = gsc..format(fmt, GSC_COPPER, c) str = str..format(fmt, COPPER, c)
end end
if gsc == '' then if str == '' then
gsc = GSC_NONE str = NONE
end end
return gsc return str
end end
function public.to_string(money, pad, trim, decimal_points, color, no_color) function public.to_string(money, pad, trim, decimal_points, color, no_color)
local is_negative = money < 0 local is_negative = money < 0
money = abs(money) money = abs(money)
local gold, silver, copper = m.to_GSC(money) local gold, silver, copper = m.gsc(money)
-- rounding -- rounding
if decimal_points then if decimal_points then
@@ -129,7 +129,7 @@ function public.from_string(value)
value = gsub(value, '%d*%.?%d+c', '', 1) value = gsub(value, '%d*%.?%d+c', '', 1)
if strfind(value, '%S') then return end if strfind(value, '%S') then return end
return m.from_GSC(gold or 0, silver or 0, copper or 0) return m.copper(gold or 0, silver or 0, copper or 0)
end end
function public.format_number(num, pad, decimal_padding, color) function public.format_number(num, pad, decimal_padding, color)