From 303ef7f9c3b8cc1a38d672f2f3a1af987ddd7aca Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 23 Aug 2016 16:33:58 +0200 Subject: [PATCH] refactoring --- core/history.lua | 28 +++++++++++++--------------- util/persistence.lua | 23 +++++++++-------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/core/history.lua b/core/history.lua index f7245be..b198bb9 100644 --- a/core/history.lua +++ b/core/history.lua @@ -14,9 +14,9 @@ do local data function accessor.data() if not data then - local dataset = persistence.load_dataset() - dataset.history = dataset.history or t - data = dataset.history + local dataset = persistence.dataset + data = dataset.history or t + dataset.history = data end return data end @@ -56,10 +56,14 @@ 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 + local max_unit_price = max(unit_buyout_price, unit_bid_price) + if unit_buyout_price > 0 and unit_buyout_price < (item_record.daily_min_buyout or huge) then + item_record.daily_min_buyout = unit_buyout_price + elseif max_unit_price > item_record.daily_max_price or 0 then + item_record.daily_max_price = max_unit_price + else + return 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 @@ -100,13 +104,9 @@ function calculate_market_value(item_record) end function weighted_median(list) - local sorted_list = tt - for _, e in list do - tinsert(sorted_list, e) - end - sort(sorted_list, function(a,b) return a.value < b.value end) + sort(list, function(a,b) return a.value < b.value end) local weight = 0 - for _, element in sorted_list do + for _, element in list do weight = weight + element.weight if weight >= .5 then return element.value @@ -121,7 +121,5 @@ function push_record(item_record) 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 + item_record.next_push, item_record.daily_min_buyout, item_record.daily_max_price = next_push, nil, nil end \ No newline at end of file diff --git a/util/persistence.lua b/util/persistence.lua index ebf741f..53a4fe0 100644 --- a/util/persistence.lua +++ b/util/persistence.lua @@ -3,20 +3,15 @@ module 'persistence' _g.aux_datasets = t do - local realm, faction - - function LOAD() - thread(when, function() faction = UnitFactionGroup 'player' return faction end, function() end) - realm = GetCVar 'realmName' - end - - function accessor.dataset_key() return realm..'|'..faction end -end - -function public.load_dataset() - local dataset_key = dataset_key - _g.aux_datasets[dataset_key] = _g.aux_datasets[dataset_key] or t - return _g.aux_datasets[dataset_key] + local dataset + function public.accessor.dataset() + if not dataset then + local dataset_key = format('%s|%s', GetCVar 'realmName', UnitFactionGroup 'player') + dataset = _g.aux_datasets[dataset_key] or t + _g.aux_datasets[dataset_key] = dataset + end + return dataset + end end --do TODO syntactic sugar record '#' {key='string'} {foo='number'}