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