refactoring
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
local private, public = {}, {}
|
||||
Aux.disenchant = public
|
||||
local m, private, public = Aux.module'disenchant'
|
||||
|
||||
function public.source(item_id)
|
||||
|
||||
@@ -68,7 +67,7 @@ local UNCOMMON, RARE, EPIC = 2, 3, 4
|
||||
|
||||
function public.value(slot, quality, level)
|
||||
local expectation
|
||||
for _, event in public.distribution(slot, quality, level) do
|
||||
for _, event in m.distribution(slot, quality, level) do
|
||||
local value = Aux.history.value(event.item_id..':'..0)
|
||||
if not value then
|
||||
return
|
||||
|
||||
+22
-23
@@ -1,5 +1,4 @@
|
||||
local private, public = {}, {}
|
||||
Aux.history = public
|
||||
local m, private, public = Aux.module'history'
|
||||
|
||||
local history_schema = {'record', '#', {next_push='number'}, {daily_min_buyout='number'}, {daily_max_price='number'}, {data_points={'list', ';', {'record', '@', {market_value='number'}, {time='number'}}}}}
|
||||
private.value_cache = {}
|
||||
@@ -11,7 +10,7 @@ function private.next_push()
|
||||
end
|
||||
|
||||
function private.new_record()
|
||||
return { next_push = private.next_push(), data_points = {} }
|
||||
return { next_push = m.next_push(), data_points = {} }
|
||||
end
|
||||
|
||||
function private.load_data()
|
||||
@@ -21,31 +20,31 @@ function private.load_data()
|
||||
end
|
||||
|
||||
function private.read_record(item_key)
|
||||
local data = private.load_data()
|
||||
local data = m.load_data()
|
||||
|
||||
local record
|
||||
if data[item_key] then
|
||||
record = Aux.persistence.read(history_schema, data[item_key])
|
||||
else
|
||||
record = private.new_record()
|
||||
record = m.new_record()
|
||||
end
|
||||
|
||||
if record.next_push <= time() then
|
||||
private.push_record(record)
|
||||
private.write_record(item_key, record)
|
||||
m.push_record(record)
|
||||
m.write_record(item_key, record)
|
||||
end
|
||||
|
||||
return record
|
||||
end
|
||||
|
||||
function private.write_record(item_key, record)
|
||||
private.value_cache[item_key] = nil
|
||||
local data = private.load_data()
|
||||
m.value_cache[item_key] = nil
|
||||
local data = m.load_data()
|
||||
data[item_key] = Aux.persistence.write(history_schema, record)
|
||||
end
|
||||
|
||||
function public.process_auction(auction_record)
|
||||
local item_record = private.read_record(auction_record.item_key)
|
||||
local item_record = m.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)
|
||||
@@ -56,17 +55,17 @@ function public.process_auction(auction_record)
|
||||
|
||||
item_record.daily_max_price = max(item_record.daily_max_price or 0, unit_buyout_price, unit_bid_price)
|
||||
|
||||
private.write_record(auction_record.item_key, item_record)
|
||||
m.write_record(auction_record.item_key, item_record)
|
||||
end
|
||||
|
||||
function public.price_data(item_key)
|
||||
local item_record = private.read_record(item_key)
|
||||
local item_record = m.read_record(item_key)
|
||||
return item_record.daily_min_buyout, item_record.daily_max_price, item_record.data_points
|
||||
end
|
||||
|
||||
function public.value(item_key)
|
||||
if not private.value_cache[item_key] or private.value_cache[item_key].next_push <= time() then
|
||||
local item_record = private.read_record(item_key)
|
||||
if not m.value_cache[item_key] or m.value_cache[item_key].next_push <= time() then
|
||||
local item_record = m.read_record(item_key)
|
||||
|
||||
local value
|
||||
if getn(item_record.data_points) > 0 then
|
||||
@@ -81,23 +80,23 @@ function public.value(item_key)
|
||||
weighted_value.weight = weighted_value.weight / total_weight
|
||||
end
|
||||
|
||||
value = private.weighted_median(weighted_values)
|
||||
value = m.weighted_median(weighted_values)
|
||||
else
|
||||
value = private.market_value(item_record)
|
||||
value = m.calculate_market_value(item_record)
|
||||
end
|
||||
|
||||
private.value_cache[item_key] = {value=value, next_push=item_record.next_push}
|
||||
m.value_cache[item_key] = {value=value, next_push=item_record.next_push}
|
||||
end
|
||||
|
||||
return private.value_cache[item_key].value
|
||||
return m.value_cache[item_key].value
|
||||
end
|
||||
|
||||
function public.market_value(item_key)
|
||||
local item_record = private.read_record(item_key)
|
||||
return private.market_value(item_record)
|
||||
local item_record = m.read_record(item_key)
|
||||
return m.calculate_market_value(item_record)
|
||||
end
|
||||
|
||||
function private.market_value(item_record)
|
||||
function private.calculate_market_value(item_record)
|
||||
return item_record.daily_min_buyout and min(ceil(item_record.daily_min_buyout * 1.15), item_record.daily_max_price)
|
||||
end
|
||||
|
||||
@@ -119,7 +118,7 @@ end
|
||||
|
||||
function private.push_record(item_record)
|
||||
|
||||
local market_value = private.market_value(item_record)
|
||||
local market_value = m.calculate_market_value(item_record)
|
||||
if market_value then
|
||||
tinsert(item_record.data_points, 1, { market_value = market_value, time = item_record.next_push })
|
||||
while getn(item_record.data_points) > 11 do
|
||||
@@ -129,5 +128,5 @@ function private.push_record(item_record)
|
||||
|
||||
item_record.daily_min_buyout = nil
|
||||
item_record.daily_max_price = nil
|
||||
item_record.next_push = private.next_push()
|
||||
item_record.next_push = m.next_push()
|
||||
end
|
||||
Reference in New Issue
Block a user