From f26f966d5e3ae9466364bdf3039cd8721554cf7a Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 23 Aug 2016 15:47:52 +0200 Subject: [PATCH] refactoring --- core/history.lua | 76 ++++++++++++++++++----------------------- core/tooltip.lua | 80 ++++++++++++++------------------------------ gui/item_listing.lua | 2 +- tabs/post/core.lua | 12 +++---- 4 files changed, 66 insertions(+), 104 deletions(-) diff --git a/core/history.lua b/core/history.lua index 7755e23..f7245be 100644 --- a/core/history.lua +++ b/core/history.lua @@ -6,61 +6,60 @@ module 'history' import 'persistence' -- :daily_max_price 'number' -- :data_points (list(';') (tuple('@') :market_value 'number' :time 'number')) -local history_schema = {'record', '#', {next_push='number'}, {daily_min_buyout='number'}, {daily_max_price='number'}, {data_points={'list', ';', {'record', '@', {market_value='number'}, {time='number'}}}}} +local history_schema = {'record', '#', {next_push='number'}, {daily_min_buyout='number'}, {daily_max_price='number'}, {data_points={'list', ';', {'record', '@', {market_value='number'}, {time='number'}}}} } + value_cache = t -function accessor.next_push() - local date = date '*t' - date.hour, date.min, date.sec = 24, 0, 0 - return time(date) +do + local data + function accessor.data() + if not data then + local dataset = persistence.load_dataset() + dataset.history = dataset.history or t + data = dataset.history + end + return data + end end -function new_record() +do + local next_push = 0 + function accessor.next_push() + if time() > next_push then + local date = date '*t' + date.hour, date.min, date.sec = 24, 0, 0 + next_push = time(date) + end + return next_push + end +end + +function accessor.new_record() return -object :next_push(next_push) :data_points(t) end -function load_data() - local dataset = persistence.load_dataset() - dataset.history = dataset.history or t - return dataset.history -end - function read_record(item_key) - local data = load_data() - - local record - if data[item_key] then - record = persistence.read(history_schema, data[item_key]) - else - record = new_record() - end - + local record = data[item_key] and persistence.read(history_schema, data[item_key]) or new_record if record.next_push <= time() then push_record(record) write_record(item_key, record) end - return record end function write_record(item_key, record) value_cache[item_key] = nil - local data = load_data() data[item_key] = persistence.write(history_schema, record) end function public.process_auction(auction_record) local item_record = read_record(auction_record.item_key) - local unit_bid_price = ceil(auction_record.bid_price / auction_record.aux_quantity) local unit_buyout_price = ceil(auction_record.buyout_price / auction_record.aux_quantity) - if auction_record.buyout_price > 0 then item_record.daily_min_buyout = item_record.daily_min_buyout and min(item_record.daily_min_buyout, unit_buyout_price) or unit_buyout_price end - item_record.daily_max_price = max(item_record.daily_max_price or 0, unit_buyout_price, unit_bid_price) - write_record(auction_record.item_key, item_record) end @@ -71,14 +70,12 @@ end function public.value(item_key) if not value_cache[item_key] or value_cache[item_key].next_push <= time() then - local item_record = read_record(item_key) - - local value + local item_record, value + item_record = read_record(item_key) if getn(item_record.data_points) > 0 then - local weighted_values = tt - local total_weight = 0 + local total_weight, weighted_values = 0, tt for _, data_point in item_record.data_points do - local weight = 0.99^round((item_record.data_points[1].time - data_point.time) / (60 * 60 * 24)) + local weight = .99 ^ round((item_record.data_points[1].time - data_point.time) / (60 * 60 * 24)) total_weight = total_weight + weight tinsert(weighted_values, -object :value(data_point.market_value) :weight(weight)) end @@ -89,16 +86,13 @@ function public.value(item_key) else value = calculate_market_value(item_record) end - value_cache[item_key] = -object :value(value) :next_push(item_record.next_push) end - return value_cache[item_key].value end function public.market_value(item_key) - local item_record = read_record(item_key) - return calculate_market_value(item_record) + return calculate_market_value(read_record(item_key)) end function calculate_market_value(item_record) @@ -111,26 +105,22 @@ function weighted_median(list) tinsert(sorted_list, e) end sort(sorted_list, function(a,b) return a.value < b.value end) - local weight = 0 for _, element in sorted_list do weight = weight + element.weight - if weight >= 0.5 then + if weight >= .5 then return element.value end end end function push_record(item_record) - - local market_value = calculate_market_value(item_record) - if market_value then + for market_value in present(calculate_market_value(item_record)) do tinsert(item_record.data_points, 1, -object :market_value(market_value) :time(item_record.next_push)) while getn(item_record.data_points) > 11 do tremove(item_record.data_points) end end - item_record.daily_min_buyout = nil item_record.daily_max_price = nil item_record.next_push = next_push diff --git a/core/tooltip.lua b/core/tooltip.lua index 78ff73d..dc31006 100644 --- a/core/tooltip.lua +++ b/core/tooltip.lua @@ -1,10 +1,10 @@ -module 'tooltip' import 'info' +module 'tooltip' import 'info' 'disenchant' 'cache' 'money' 'history' _g.aux_tooltip_value = true -game_tooltip_hooks = {} -hooked_setter = nil -game_tooltip_money = nil +game_tooltip_hooks = t +mutable.hooked_setter = nil +mutable.game_tooltip_money = nil function LOAD() for name, f in game_tooltip_hooks do @@ -29,7 +29,7 @@ function LOAD() end return result end) - local orig = GameTooltip:GetScript('OnTooltipAddMoney') + local orig = GameTooltip:GetScript 'OnTooltipAddMoney' GameTooltip:SetScript('OnTooltipAddMoney', function(...) temp=arg if hooked_setter then game_tooltip_money = arg1 @@ -42,12 +42,9 @@ end function extend_tooltip(tooltip, link, quantity) local item_id, suffix_id = info.parse_link(link) quantity = IsShiftKeyDown() and quantity or 1 - if _g.aux_tooltip_disenchant_source then - local color = {r=0.7, g=0.7, b=0.7} - + local color = {r=.7, g=.7, b=.7} local type, range = disenchant.source(item_id) - if type == 'CRYSTAL' then tooltip:AddLine(format('Can disenchant from level %s |cffa335eeEpic|r and |cff0070ddRare|r items.', range), color.r, color.g, color.b, true) elseif type == 'SHARD' then @@ -58,15 +55,11 @@ function extend_tooltip(tooltip, link, quantity) tooltip:AddLine(format('Can disenchant from level %s |cff1eff00Uncommon|r items.', range), color.r, color.g, color.b, true) end end - - local item_info = info.item(item_id) - if item_info then + for item_info in present(info.item(item_id)) do local distribution = disenchant.distribution(item_info.slot, item_info.quality, item_info.level) - if getn(distribution) > 0 then - if _g.aux_tooltip_disenchant_distribution then - local color = {r=0.8, g=0.8, b=0.2} + local color = {r=.8, g=.8, b=.2} tooltip:AddLine('Disenchants into:', color.r, color.g, color.b) sort(distribution, function(a,b) return a.probability > b.probability end) @@ -74,38 +67,31 @@ function extend_tooltip(tooltip, link, quantity) tooltip:AddLine(format(' %s%% %s (%s-%s)', event.probability * 100, info.display_name(event.item_id, true) or 'item:'..event.item_id, event.min_quantity, event.max_quantity), color.r, color.g, color.b) end end - if _g.aux_tooltip_disenchant_value then - local color = {r=0.1, g=0.6, b=0.6} + local color = {r=.1, g=.6, b=.6} local disenchant_value = disenchant.value(item_info.slot, item_info.quality, item_info.level) tooltip:AddLine('Disenchant Value: '..(disenchant_value and money.to_string2(disenchant_value) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end end - if _g.aux_tooltip_vendor_buy then - local color = {r=0.8, g=0.5, b=0.1} - + local color = {r=.8, g=.5, b=.1} local _, price, limited = cache.merchant_info(item_id) if price then tooltip:AddLine('Vendor Buy '..(limited and '(limited): ' or ': ')..money.to_string2(price * quantity), color.r, color.g, color.b) end end if _g.aux_tooltip_vendor_sell then - local color = {r=0.8, g=0.5, b=0.1} - + local color = {r=.8, g=.5, b=.1} local price = cache.merchant_info(item_id) if price ~= 0 then tooltip:AddLine('Vendor Sell: '..(price and money.to_string2(price * quantity) or GRAY_FONT_COLOR_CODE..'---'..FONT_COLOR_CODE_CLOSE), color.r, color.g, color.b) end end - - local color = {r=1, g=1, b=0.6} - + local color = {r=1, g=1, b=.6} local auctionable = not item_info or info.auctionable(info.tooltip(function(tt) tt:SetHyperlink(item_info.itemstring) end), item_info.quality) local item_key = (item_id or 0)..':'..(suffix_id or 0) - local value = history.value(item_key) if auctionable then if _g.aux_tooltip_value then @@ -133,50 +119,38 @@ function game_tooltip_hooks:SetHyperlink(itemstring) end function game_tooltip_hooks:SetAuctionItem(type, index) - local link = GetAuctionItemLink(type, index) - if link then - local _, _, quantity = GetAuctionItemInfo(type, index) - extend_tooltip(GameTooltip, link, quantity) + for link in present(GetAuctionItemLink(type, index)) do + extend_tooltip(GameTooltip, link, select(3, GetAuctionItemInfo(type, index))) end end function game_tooltip_hooks:SetLootItem(slot) - local link = GetLootSlotLink(slot) - if link then - local _, _, quantity = GetLootSlotInfo(slot) - extend_tooltip(GameTooltip, link, quantity) + for link in present(GetLootSlotLink(slot)) do + extend_tooltip(GameTooltip, link, select(3, GetLootSlotInfo(slot))) end end function game_tooltip_hooks:SetQuestItem(qtype, slot) - local link = GetQuestItemLink(qtype, slot) - if link then - local _, _, quantity = GetQuestItemInfo(qtype, slot) - extend_tooltip(GameTooltip, link, quantity) + for link in present(GetQuestItemLink(qtype, slot)) do + extend_tooltip(GameTooltip, link, select(3, GetQuestItemInfo(qtype, slot))) end end function game_tooltip_hooks:SetQuestLogItem(qtype, slot) - local link = GetQuestLogItemLink(qtype, slot) - if link then - local _, _, quantity = GetQuestLogRewardInfo(slot) - extend_tooltip(GameTooltip, link, quantity) + for link in present(GetQuestLogItemLink(qtype, slot)) do + extend_tooltip(GameTooltip, link, select(3, GetQuestLogRewardInfo(slot))) end end function game_tooltip_hooks:SetBagItem(bag, slot) - local link = GetContainerItemLink(bag, slot) - if link then - local _, quantity = GetContainerItemInfo(bag, slot) - extend_tooltip(GameTooltip, link, quantity) + for link in present(GetContainerItemLink(bag, slot)) do + extend_tooltip(GameTooltip, link, select(2, GetContainerItemInfo(bag, slot))) end end function game_tooltip_hooks:SetInboxItem(index) local name, _, quantity = GetInboxItem(index) - - local id = name and cache.item_id(name) - if id then + for id in present(name and cache.item_id(name)) do local _, itemstring, quality = GetItemInfo(id) local hex = select(4, GetItemQualityColor(tonumber(quality))) local link = hex.. '|H'..itemstring..'|h['..name..']|h'..FONT_COLOR_CODE_CLOSE @@ -185,15 +159,13 @@ function game_tooltip_hooks:SetInboxItem(index) end function game_tooltip_hooks:SetInventoryItem(unit, slot) - local link = GetInventoryItemLink(unit, slot) - if link then + for link in present(GetInventoryItemLink(unit, slot)) do extend_tooltip(GameTooltip, link, 1) end end function game_tooltip_hooks:SetMerchantItem(slot) - local link = GetMerchantItemLink(slot) - if link then + for link in present(GetMerchantItemLink(slot)) do local quantity = select(4, GetMerchantItemInfo(slot)) extend_tooltip(GameTooltip, link, quantity) end @@ -232,7 +204,7 @@ end function game_tooltip_hooks:SetAuctionSellItem() local name, _, quantity = GetAuctionSellItemInfo() if name then - for slot in info.inventory do + for slot in info.inventory do temp=slot local link = GetContainerItemLink(unpack(slot)) if link and select(5, info.parse_link(link)) == name then extend_tooltip(GameTooltip, link, quantity) diff --git a/gui/item_listing.lua b/gui/item_listing.lua index 9412404..d636f6e 100644 --- a/gui/item_listing.lua +++ b/gui/item_listing.lua @@ -1,4 +1,4 @@ -module 'item_listing' import 'gui' +module 'item_listing' import 'gui' 'info' local ROW_HEIGHT = 39 diff --git a/tabs/post/core.lua b/tabs/post/core.lua index b3e36ab..71a2b2b 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -1,4 +1,4 @@ -module 'post_tab' import 'scan' 'scan_util' 'post' 'history' 'info' 'persistence' 'item_listing' +module 'post_tab' import 'scan' 'scan_util' 'post' 'history' 'info' 'persistence' 'item_listing' :al 'auction_listing' 'sorting' local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440 local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}} @@ -135,12 +135,12 @@ function update_auction_listing() tinsert(auction_rows, { cols = { {value=auction_record.own and color.yellow(auction_record.count) or auction_record.count}, - {value=auction_listing.time_left(auction_record.duration)}, + {value=al.time_left(auction_record.duration)}, {value=auction_record.stack_size == stack_size and color.yellow(auction_record.stack_size) or auction_record.stack_size}, {value=money.to_string(auction_record.unit_blizzard_bid, true, nil, 3, bid_color) }, - {value=historical_value and auction_listing.percentage_historical(round(auction_record.unit_blizzard_bid / historical_value * 100)) or '---'}, + {value=historical_value and al.percentage_historical(round(auction_record.unit_blizzard_bid / historical_value * 100)) or '---'}, {value=auction_record.unit_buyout_price > 0 and money.to_string(auction_record.unit_buyout_price, true, nil, 3, buyout_color) or '---'}, - {value=auction_record.unit_buyout_price > 0 and historical_value and auction_listing.percentage_historical(round(auction_record.unit_buyout_price / historical_value * 100)) or '---'}, + {value=auction_record.unit_buyout_price > 0 and historical_value and al.percentage_historical(round(auction_record.unit_buyout_price / historical_value * 100)) or '---'}, }, record = auction_record, }) @@ -183,12 +183,12 @@ function price_update() local start_price_input = get_unit_start_price() settings.start_price = start_price_input local historical_value = history.value(selected_item.key) - start_price_percentage:SetText(historical_value and auction_listing.percentage_historical(round(start_price_input / historical_value * 100)) or '---') + start_price_percentage:SetText(historical_value and al.percentage_historical(round(start_price_input / historical_value * 100)) or '---') local buyout_price_input = get_unit_buyout_price() settings.buyout_price = buyout_price_input local historical_value = history.value(selected_item.key) - buyout_price_percentage:SetText(historical_value and auction_listing.percentage_historical(round(buyout_price_input / historical_value * 100)) or '---') + buyout_price_percentage:SetText(historical_value and al.percentage_historical(round(buyout_price_input / historical_value * 100)) or '---') write_settings(settings) end