greatly simplified history
This commit is contained in:
+102
-139
@@ -1,99 +1,72 @@
|
||||
local private, public = {}, {}
|
||||
Aux.history = public
|
||||
|
||||
aux_conservative_value = false
|
||||
|
||||
private.PUSH_INTERVAL = 57600
|
||||
|
||||
aux_market_value_type = 'buyout'
|
||||
|
||||
local data
|
||||
private.NEW_RECORD = '####'
|
||||
|
||||
function private.load_data()
|
||||
local dataset = Aux.persistence.load_dataset()
|
||||
dataset.history = dataset.history or {
|
||||
item_records = {},
|
||||
next_push = time() + private.PUSH_INTERVAL,
|
||||
}
|
||||
dataset.history = dataset.history or { next_push = time() + private.PUSH_INTERVAL, item_data = {} }
|
||||
return dataset.history
|
||||
end
|
||||
|
||||
function public.on_login()
|
||||
data = private.load_data()
|
||||
for item_key, _ in pairs(data.item_records) do
|
||||
data.item_records[item_key] = private.deserialize_item_record(data.item_records[item_key])
|
||||
end
|
||||
end
|
||||
|
||||
function public.on_logout()
|
||||
for item_key, _ in pairs(data.item_records) do
|
||||
data.item_records[item_key] = private.serialize_item_record(data.item_records[item_key])
|
||||
end
|
||||
end
|
||||
|
||||
function private.serialize_item_record(item_record)
|
||||
return Aux.persistence.serialize({
|
||||
Aux.persistence.serialize(item_record.daily_bid_values, ';'),
|
||||
Aux.persistence.serialize(item_record.daily_buyout_values, ';'),
|
||||
Aux.persistence.serialize(item_record.bids_of_today:values(), ';'),
|
||||
Aux.persistence.serialize(item_record.buyouts_of_today:values(), ';'),
|
||||
}, '#')
|
||||
end
|
||||
|
||||
function private.deserialize_item_record(data_string)
|
||||
local fields = Aux.persistence.deserialize(data_string, '#')
|
||||
|
||||
local bids_of_today = Aux.util.set()
|
||||
bids_of_today:add_all(Aux.util.map(Aux.persistence.deserialize(fields[3], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end))
|
||||
|
||||
local buyouts_of_today = Aux.util.set()
|
||||
buyouts_of_today:add_all(Aux.util.map(Aux.persistence.deserialize(fields[4], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end))
|
||||
|
||||
return {
|
||||
daily_bid_values = Aux.util.map(Aux.persistence.deserialize(fields[1], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end),
|
||||
daily_buyout_values = Aux.util.map(Aux.persistence.deserialize(fields[2], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end),
|
||||
bids_of_today = bids_of_today,
|
||||
buyouts_of_today = buyouts_of_today,
|
||||
}
|
||||
end
|
||||
|
||||
function private.new_item_record()
|
||||
return { daily_bid_values = {}, daily_buyout_values = {}, bids_of_today = Aux.util.set(), buyouts_of_today = Aux.util.set() }
|
||||
end
|
||||
|
||||
--function private.price_class(price)
|
||||
-- return Aux.round(math.log(price) / math.log(1.1))
|
||||
--end
|
||||
--
|
||||
--function private.price_class_price(class)
|
||||
-- return 1.1^class
|
||||
--end
|
||||
|
||||
function public.process_auction(auction_info)
|
||||
function private.read_record(item_key)
|
||||
local data = private.load_data()
|
||||
|
||||
if data.next_push < time() then
|
||||
data.next_push = time() + private.PUSH_INTERVAL
|
||||
private.push_data()
|
||||
end
|
||||
|
||||
local fields = Aux.persistence.deserialize(data.item_data[item_key] or private.NEW_RECORD, '#')
|
||||
return {
|
||||
-- auction_count = tonumber(record[1]),
|
||||
-- day_count = tonumber(record[2]),
|
||||
daily_max_bid = tonumber(fields[1]),
|
||||
daily_min_buyout = tonumber(fields[2]),
|
||||
daily_max_buyout = tonumber(fields[3]),
|
||||
market_values = Aux.util.map(Aux.persistence.deserialize(fields[4], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end),
|
||||
conservative_market_values = Aux.util.map(Aux.persistence.deserialize(fields[5], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end),
|
||||
}
|
||||
end
|
||||
|
||||
function private.write_record(item_key, record)
|
||||
local data = private.load_data()
|
||||
data.item_data[item_key] = Aux.persistence.serialize({
|
||||
record.daily_max_bid or '',
|
||||
record.daily_min_buyout or '',
|
||||
record.daily_max_buyout or '',
|
||||
Aux.persistence.serialize(record.market_values, ';'),
|
||||
Aux.persistence.serialize(record.conservative_market_values, ';', 'x'),
|
||||
},'#')
|
||||
end
|
||||
|
||||
function public.process_auction(auction_info)
|
||||
|
||||
-- item_record.auction_count = item_record.auction_count + 1
|
||||
|
||||
local data = private.load_data()
|
||||
|
||||
local item_record = private.read_record(auction_info.item_key)
|
||||
|
||||
if auction_info.high_bid > 0 then
|
||||
data.item_records[auction_info.item_key] = data.item_records[auction_info.item_key] or private.new_item_record()
|
||||
local bid = Aux.round(auction_info.high_bid / auction_info.aux_quantity)
|
||||
data.item_records[auction_info.item_key].bids_of_today:add(bid)
|
||||
-- data.item_records[auction_info.item_key].bids_of_today:add(private.price_class(auction_info.high_bid / auction_info.aux_quantity))
|
||||
local unit_high_bid = ceil(auction_info.high_bid / auction_info.aux_quantity)
|
||||
item_record.daily_max_bid = item_record.daily_max_bid and max(item_record.daily_max_bid, unit_high_bid) or unit_high_bid
|
||||
end
|
||||
|
||||
if auction_info.buyout_price > 0 then
|
||||
data.item_records[auction_info.item_key] = data.item_records[auction_info.item_key] or private.new_item_record()
|
||||
local buyout = Aux.round(auction_info.buyout_price / auction_info.aux_quantity)
|
||||
data.item_records[auction_info.item_key].buyouts_of_today:add(buyout)
|
||||
-- data.item_records[auction_info.item_key].buyouts_of_today:add(private.price_class(auction_info.buyout_price / auction_info.aux_quantity))
|
||||
local unit_buyout_price = ceil(auction_info.buyout_price / auction_info.aux_quantity)
|
||||
item_record.daily_max_buyout = item_record.daily_max_buyout and max(item_record.daily_max_buyout, unit_buyout_price) or unit_buyout_price
|
||||
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
|
||||
|
||||
private.write_record(auction_info.item_key, item_record)
|
||||
end
|
||||
|
||||
--function public.price_data(item_key)
|
||||
@@ -101,62 +74,45 @@ end
|
||||
-- return item_record.auction_count, item_record.day_count, private.daily_market_value(item_record.histogram), private.median(item_record.last_daily_values)
|
||||
--end
|
||||
|
||||
function public.market_value(item_key)
|
||||
local record = data.item_records[item_key]
|
||||
function public.value(item_key)
|
||||
local item_record = private.read_record(item_key)
|
||||
|
||||
if not record then
|
||||
return
|
||||
end
|
||||
|
||||
local daily_values = aux_market_value_type == 'buyout' and record.daily_buyout_values or record.daily_bid_values
|
||||
|
||||
if getn(daily_values) == 0 then
|
||||
if aux_market_value_type == 'buyout' then
|
||||
return private.daily_buyout_value(item_key)
|
||||
elseif aux_market_value_type == 'bid' then
|
||||
return private.daily_bid_value(item_key)
|
||||
end
|
||||
local past_market_values
|
||||
if aux_conservative_value then
|
||||
past_market_values = item_record.conservative_market_values
|
||||
else
|
||||
return private.median(daily_values)
|
||||
past_market_values = item_record.market_values
|
||||
end
|
||||
|
||||
if getn(past_market_values) > 0 then
|
||||
return private.median(past_market_values)
|
||||
elseif aux_conservative_value then
|
||||
return private.conservative_market_value(item_key)
|
||||
else
|
||||
return private.market_value(item_key)
|
||||
end
|
||||
end
|
||||
|
||||
function private.daily_bid_value(item_key)
|
||||
local prices = data.item_records[item_key].bids_of_today:values()
|
||||
function private.market_value(item_key)
|
||||
local item_record = private.read_record(item_key)
|
||||
|
||||
if getn(prices) == 0 then
|
||||
return
|
||||
if item_record.daily_max_bid and item_record.daily_min_buyout then
|
||||
return max(item_record.daily_max_bid, item_record.daily_min_buyout)
|
||||
elseif item_record.daily_min_buyout then
|
||||
return max(ceil(item_record.daily_min_buyout * 1.15), item_record.daily_max_buyout)
|
||||
elseif item_record.daily_max_bid then
|
||||
return item_record.daily_max_bid
|
||||
end
|
||||
|
||||
sort(prices, function(a,b) return b < a end)
|
||||
|
||||
local acc = 0
|
||||
local cutoff = ceil(getn(prices) * 0.2)
|
||||
for i=1,cutoff do
|
||||
acc = acc + prices[i]
|
||||
-- acc = acc + private.price_class_price(prices[i])
|
||||
end
|
||||
|
||||
return acc / cutoff
|
||||
end
|
||||
|
||||
function private.daily_buyout_value(item_key)
|
||||
local prices = data.item_records[item_key].buyouts_of_today:values()
|
||||
function private.conservative_market_value(item_key)
|
||||
local item_record = private.read_record[item_key]
|
||||
|
||||
if getn(prices) == 0 then
|
||||
return
|
||||
if item_record.daily_max_bid and item_record.daily_min_buyout then
|
||||
return min(item_record.daily_max_bid, item_record.daily_min_buyout)
|
||||
elseif item_record.daily_max_bid then
|
||||
return item_record.daily_max_bid
|
||||
end
|
||||
|
||||
sort(prices)
|
||||
|
||||
local acc = 0
|
||||
local cutoff = ceil(getn(prices) * 0.2)
|
||||
for i=1,cutoff do
|
||||
acc = acc + prices[i]
|
||||
-- acc = acc + private.price_class_price(prices[i])
|
||||
end
|
||||
|
||||
return acc / cutoff
|
||||
end
|
||||
|
||||
function private.median(list)
|
||||
@@ -175,28 +131,35 @@ function private.median(list)
|
||||
end
|
||||
|
||||
function private.push_data()
|
||||
local data = private.load_data()
|
||||
local item_data = data.item_data
|
||||
|
||||
for item_key, record in pairs(data.item_records) do
|
||||
for item_key, _ in pairs(item_data) do
|
||||
|
||||
local daily_bid_value = private.daily_bid_value(item_key)
|
||||
if daily_bid_value then
|
||||
tinsert(record.daily_bid_values, Aux.round(daily_bid_value))
|
||||
end
|
||||
while getn(record.daily_bid_values) > 11 do
|
||||
tremove(record.daily_bid_values, 1)
|
||||
end
|
||||
record.bids_of_today = Aux.util.set()
|
||||
local item_record = private.read_record(item_key)
|
||||
|
||||
local daily_buyout_value = private.daily_buyout_value(item_key)
|
||||
if daily_buyout_value then
|
||||
tinsert(record.daily_buyout_values, Aux.round(daily_buyout_value))
|
||||
local market_value = private.market_value(item_key)
|
||||
if market_value then
|
||||
tinsert(item_record.market_values, market_value)
|
||||
while getn(item_record.market_values) > 11 do
|
||||
tremove(item_record.market_values, 1)
|
||||
end
|
||||
end
|
||||
while getn(record.daily_buyout_values) > 11 do
|
||||
tremove(record.daily_buyout_values, 1)
|
||||
end
|
||||
record.buyouts_of_today = Aux.util.set()
|
||||
|
||||
local conservative_market_value = private.conservative_market_value(item_key)
|
||||
if conservative_market_value then
|
||||
tinsert(item_record.conservative_market_values, conservative_market_value)
|
||||
while getn(item_record.conservative_market_values) > 11 do
|
||||
tremove(item_record.conservative_market_values, 1)
|
||||
end
|
||||
end
|
||||
|
||||
item_record.daily_max_bid = nil
|
||||
item_record.daily_min_buyout = nil
|
||||
item_record.daily_max_buyout = nil
|
||||
|
||||
if market_value or conservative_market_value then
|
||||
private.write_record(item_key, item_record)
|
||||
end
|
||||
end
|
||||
|
||||
data.next_push = time() + private.PUSH_INTERVAL
|
||||
end
|
||||
Reference in New Issue
Block a user