From 44d0b46aa8a9288739627bc5cd20109410d7f4ba Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 9 Aug 2016 10:46:43 +0200 Subject: [PATCH] refactoring --- components/tooltip.lua | 10 +-- core.lua | 12 +-- gui.lua | 57 ++++++++------ tables/auction_listing.lua | 25 +++---- util/core.lua | 150 ++++++++++++++----------------------- util/money.lua | 38 ++++++++++ 6 files changed, 147 insertions(+), 145 deletions(-) diff --git a/components/tooltip.lua b/components/tooltip.lua index dd72455..4715e42 100644 --- a/components/tooltip.lua +++ b/components/tooltip.lua @@ -83,7 +83,7 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local color = {r=0.1, g=0.6, b=0.6} local disenchant_value = aux.disenchant.value(item_info.slot, item_info.quality, item_info.level) - tooltip:AddLine('Disenchant Value: '..(disenchant_value and aux.util.format_money(disenchant_value) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) + tooltip:AddLine('Disenchant Value: '..(disenchant_value and aux.money.format(disenchant_value) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end end @@ -93,7 +93,7 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local _, price, limited = aux.cache.merchant_info(item_id) if price then - tooltip:AddLine('Vendor Buy '..(limited and '(limited): ' or ': ')..aux.util.format_money(price * quantity), color.r, color.g, color.b) + tooltip:AddLine('Vendor Buy '..(limited and '(limited): ' or ': ')..aux.money.format(price * quantity), color.r, color.g, color.b) end end if aux_tooltip_vendor_sell then @@ -101,7 +101,7 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local price = aux.cache.merchant_info(item_id) if price ~= 0 then - tooltip:AddLine('Vendor Sell: '..(price and aux.util.format_money(price * quantity) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) + tooltip:AddLine('Vendor Sell: '..(price and aux.money.format(price * quantity) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end @@ -113,11 +113,11 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local value = aux.history.value(item_key) if auctionable then if aux_tooltip_value then - tooltip:AddLine('Value: '..(value and aux.util.format_money(value * quantity) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) + tooltip:AddLine('Value: '..(value and aux.money.format(value * quantity) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end if aux_tooltip_daily then local market_value = aux.history.market_value(item_key) - tooltip:AddLine('Today: '..(market_value and aux.util.format_money(market_value * quantity)..' ('..aux.auction_listing.percentage_historical(aux.util.round(market_value / value * 100))..')' or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) + tooltip:AddLine('Today: '..(market_value and aux.money.format(market_value * quantity)..' ('..aux.auction_listing.percentage_historical(aux.util.round(market_value / value * 100))..')' or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end diff --git a/core.lua b/core.lua index 98ca4c8..fd9096d 100644 --- a/core.lua +++ b/core.lua @@ -7,17 +7,17 @@ public.last_owner_page_requested = nil do local mt = { __newindex = function(self, key, value) - self._f()[key] = value + self._getter()[key] = value end, __index = function(self, key) - return self._f()[key] + return self._getter()[key] end, __call = function(self) - return self._f() + return self._getter() end, } - function public.dynamic_table(f) - return setmetatable({_f=f}, mt) + function public.dynamic_table(getter) + return setmetatable({_getter=getter}, mt) end end @@ -177,7 +177,7 @@ end do local function cost_label(cost) local label = LIGHTYELLOW_FONT_COLOR_CODE..'(Total Cost: '..FONT_COLOR_CODE_CLOSE - label = label..(cost and m.util.format_money(cost, nil, LIGHTYELLOW_FONT_COLOR_CODE) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE) + label = label..(cost and m.money.format(cost, nil, LIGHTYELLOW_FONT_COLOR_CODE) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE) label = label..LIGHTYELLOW_FONT_COLOR_CODE..')'..FONT_COLOR_CODE_CLOSE return label end diff --git a/gui.lua b/gui.lua index 3f22e81..dbda078 100644 --- a/gui.lua +++ b/gui.lua @@ -21,28 +21,37 @@ public.config = { huge_font_size = 23, } -public.color = 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 - end - local color = aux.util.copy(t) - for i=1,3 do - color[i] = color[i]/255 - end - return color - end)[key] +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 + else + return callback(aux.util.copy(t)) + end + end)[key] + end) +end + +public.color = m.color_accessor(function(color) + for i=1,3 do + color[i] = color[i]/255 + end + return color end) -public.inline_color = aux.index_function(function(self, key) - self._t = (rawget(self, '_t') or m.config.color)[key] - if getn(self._t) == 0 then - return self - end - local color = aux.util.copy(self._t) - self._t = nil +public.inline_color = m.color_accessor(function(color) tinsert(color, 1, tremove(color)) return format('|c%02X%02X%02X%02X', unpack(color)) end) @@ -69,11 +78,11 @@ do local orig = menu:GetScript('OnShow') menu:SetScript('OnShow', function() UIMenu_Initialize() - for _, element in ipairs(structure) do + for i=1,getn(structure) do UIMenu_AddButton( - element[1], - element[2], - type(element[3]) == 'string' and structure[element[3]] or element[3] + structure[i][1], + structure[i], + type(structure[i]) == 'string' and structure[element[3]] or element[3] ) end return orig() diff --git a/tables/auction_listing.lua b/tables/auction_listing.lua index 1e56ab8..ec2388d 100644 --- a/tables/auction_listing.lua +++ b/tables/auction_listing.lua @@ -10,6 +10,7 @@ public.COLORS = { YELLOW = '|cffffff00', ORANGE = '|cffff9218', RED = '|cffff0000', + GRAY = '|cffbbbbbb', } local AUCTION_PCT_COLORS = { @@ -535,25 +536,19 @@ function private.record_percentage(record) end end -function public.percentage_historical(pct, based_on_bid) - local pctColor = '|cffffffff' - - if based_on_bid then - pctColor = '|cffbbbbbb' - else - for i=1, getn(AUCTION_PCT_COLORS) do - if pct < AUCTION_PCT_COLORS[i].value then - pctColor = AUCTION_PCT_COLORS[i].color - break - end - end - end +function private.percentage_color(pct) + for i=1,getn(AUCTION_PCT_COLORS) do + if pct < AUCTION_PCT_COLORS[i].value then + return AUCTION_PCT_COLORS[i].color + end + end +end +function public.percentage_historical(pct, bid) if pct > 10000 then pct = '>10000' end - - return format('%s%s%%|r', pctColor, pct) + return (bid and m.COLORS.GRAY or m.percentage_color(pct))..pct..'%'..FONT_COLOR_CODE_CLOSE end function public.time_left(code) diff --git a/util/core.lua b/util/core.lua index a43a010..e0dbb63 100644 --- a/util/core.lua +++ b/util/core.lua @@ -1,15 +1,42 @@ local m, public, private = aux.module'util' -function public.bound(lower_bound, upper_bound, number) - return max(lower_bound, min(upper_bound, number)) +function public.pack(table, ...) + local array = {} + for i=1,arg.n do + tinsert(array, table[arg[i]]) + end +end + +function public.unpack(array, ...) + local table, index = {}, {} + 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) +end + +function public.select(i, ...) + for _=1,i-1 do + tremove(arg, 1) + end + return unpack(arg) end function public.size(t) - local x = 0 + local size = 0 for _ in t do - x = x + 1 + size = size + 1 end - return x + return size end function public.key(value, t) @@ -36,13 +63,6 @@ function public.values(t) return vs end -function public.select(i, ...) - for _=1,i-1 do - tremove(arg, 1) - end - return unpack(arg) -end - function public.eq(t1, t2) if not t1 or not t2 then return false @@ -63,6 +83,23 @@ function public.eq(t1, t2) return true end +function public.wipe(t) + while getn(t) > 0 do + tremove(t) + end + for k, _ in t do + t[k] = nil + end +end + +function public.copy(t) + local copy = {} + for k, v in t do + copy[k] = v + end + return copy +end + function public.any(xs, p) local holds = false for _, x in xs do @@ -112,32 +149,6 @@ do end, } - -- local methods = {} - -- - -- function methods:add(key) - -- self[key] = true - -- end - -- - -- function methods:remove(key) - -- self[key] = nil - -- end - -- - -- function methods:size() - -- local size = 0 - -- for _,_ in self do - -- size = size + 1 - -- end - -- return size - -- end - -- - -- function methods:elements() - -- local elements = {} - -- for element, _ in self do - -- tinsert(elements, element) - -- end - -- return elements - -- end - function public.set(...) local self = {} for i=1,arg.n do @@ -147,6 +158,10 @@ do end end +function public.trim(str) + return gsub(str, '^%s*(.-)%s*$', '%1') +end + function public.join(parts, separator) local str = parts[1] or '' for i=2,getn(parts) do @@ -181,31 +196,14 @@ function public.tokenize(str) return tokens end -function public.wipe(t) - while getn(t) > 0 do - tremove(t) - end - for k, _ in t do - t[k] = nil - end -end - -function public.copy(t) - local copy = {} - for k, v in t do - copy[k] = v - end - return copy +function public.bound(lower_bound, upper_bound, number) + return max(lower_bound, min(upper_bound, number)) end function public.round(x) return floor(x + 0.5) end -function public.trim(str) - return gsub(str, '^%s*(.-)%s*$', '%1') -end - function public.inventory() local bag, slot = 0, 0 @@ -250,42 +248,4 @@ function public.signal() function() return params end -end - -function public.format_money(money, exact, color) - color = color or FONT_COLOR_CODE_CLOSE - - local TEXT_NONE = '0' - - local GSC_GOLD = 'ffd100' - local GSC_SILVER = 'e6e6e6' - local GSC_COPPER = 'c8602c' - local GSC_START = '|cff%s%d|r' - local GSC_PART = color..'.|cff%s%02d|r' - local GSC_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 gsc = '' - - local fmt = GSC_START - if g > 0 then - gsc = gsc..format(fmt, GSC_GOLD, g) - fmt = GSC_PART - end - if s > 0 or c > 0 then - gsc = gsc..format(fmt, GSC_SILVER, s) - fmt = GSC_PART - end - if c > 0 then - gsc = gsc..format(fmt, GSC_COPPER, c) - end - if gsc == '' then - gsc = GSC_NONE - end - return gsc end \ No newline at end of file diff --git a/util/money.lua b/util/money.lua index 0c213bf..b0cb233 100644 --- a/util/money.lua +++ b/util/money.lua @@ -20,6 +20,44 @@ do end end +function public.format(money, exact, color) + color = color or FONT_COLOR_CODE_CLOSE + + 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 + + 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 gsc = '' + + local fmt = GSC_START + if g > 0 then + gsc = gsc..format(fmt, GSC_GOLD, g) + fmt = GSC_PART + end + if s > 0 or c > 0 then + gsc = gsc..format(fmt, GSC_SILVER, s) + fmt = GSC_PART + end + if c > 0 then + gsc = gsc..format(fmt, GSC_COPPER, c) + end + if gsc == '' then + gsc = GSC_NONE + end + return gsc +end + function public.to_string(money, pad, trim, decimal_points, color, no_color) local is_negative = money < 0 money = abs(money)