new history, changed sorting of names for autocompletion
This commit is contained in:
+69
-81
@@ -3,53 +3,59 @@ Aux.history = public
|
||||
|
||||
private.PUSH_INTERVAL = 57600
|
||||
|
||||
local item_records, next_push
|
||||
aux_market_value_type = 'buyout'
|
||||
|
||||
function private.encode_item_records(item_records)
|
||||
local record_strings = {}
|
||||
for item_key, record in pairs(item_records) do
|
||||
tinsert(record_strings, private.encode_item_record(item_key, record))
|
||||
end
|
||||
return Aux.persistence.serialize(record_strings, '|')
|
||||
local data
|
||||
|
||||
function private.load_data()
|
||||
local dataset = Aux.persistence.load_dataset()
|
||||
dataset.history = dataset.history or {
|
||||
item_records = {},
|
||||
next_push = time() + private.PUSH_INTERVAL,
|
||||
}
|
||||
return dataset.history
|
||||
end
|
||||
|
||||
function private.encode_item_record(item_key, record)
|
||||
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({
|
||||
item_key,
|
||||
Aux.persistence.serialize(record.daily_bid_values, ';'),
|
||||
Aux.persistence.serialize(record.daily_buyout_values, ';'),
|
||||
Aux.persistence.serialize(record.bids_of_today:values(), ';'),
|
||||
Aux.persistence.serialize(record.buyouts_of_today:values(), ';'),
|
||||
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.decode_item_records(data_string)
|
||||
local item_records = {}
|
||||
for _, record_string in Aux.persistence.deserialize(data_string, '|') do
|
||||
local item_key, record = private.decode_item_record(record_string)
|
||||
item_records[item_key] = record
|
||||
end
|
||||
return item_records
|
||||
end
|
||||
|
||||
function private.decode_item_record(data_string)
|
||||
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[4], ';'), function(value)
|
||||
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[5], ';'), function(value)
|
||||
buyouts_of_today:add_all(Aux.util.map(Aux.persistence.deserialize(fields[4], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end))
|
||||
|
||||
return fields[1], {
|
||||
daily_bid_values = Aux.util.map(Aux.persistence.deserialize(fields[2], ';'), function(value)
|
||||
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[3], ';'), function(value)
|
||||
daily_buyout_values = Aux.util.map(Aux.persistence.deserialize(fields[2], ';'), function(value)
|
||||
return tonumber(value)
|
||||
end),
|
||||
bids_of_today = bids_of_today,
|
||||
@@ -57,38 +63,36 @@ function private.decode_item_record(data_string)
|
||||
}
|
||||
end
|
||||
|
||||
function public.on_login()
|
||||
local dataset = Aux.persistence.load_dataset()
|
||||
local fields = Aux.persistence.deserialize(dataset.history, '/')
|
||||
next_push = tonumber(fields[1]) or time() + private.PUSH_INTERVAL
|
||||
item_records = private.decode_item_records(fields[2])
|
||||
end
|
||||
|
||||
function public.on_logout()
|
||||
local dataset = Aux.persistence.load_dataset()
|
||||
dataset.history = Aux.util.join({ next_push, private.encode_item_records(item_records) }, '/')
|
||||
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)
|
||||
|
||||
if next_push < time() then
|
||||
if data.next_push < time() then
|
||||
private.push_data()
|
||||
end
|
||||
|
||||
if auction_info.high_bid > 0 then
|
||||
item_records[auction_info.item_key] = item_records[auction_info.item_key] or private.new_item_record()
|
||||
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)
|
||||
item_records[auction_info.item_key].bids_of_today:add(bid)
|
||||
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))
|
||||
end
|
||||
|
||||
if auction_info.buyout_price > 0 then
|
||||
item_records[auction_info.item_key] = item_records[auction_info.item_key] or private.new_item_record()
|
||||
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)
|
||||
item_records[auction_info.item_key].buyouts_of_today:add(buyout)
|
||||
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))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -98,21 +102,27 @@ end
|
||||
--end
|
||||
|
||||
function public.market_value(item_key)
|
||||
local record = item_records[item_key]
|
||||
local record = data.item_records[item_key]
|
||||
|
||||
if not record then
|
||||
return
|
||||
end
|
||||
|
||||
if getn(record.daily_buyout_values) == 0 then
|
||||
return private.daily_buyout_value(item_key)
|
||||
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
|
||||
else
|
||||
return private.median(record.daily_buyout_values)
|
||||
return private.median(daily_values)
|
||||
end
|
||||
end
|
||||
|
||||
function private.daily_bid_value(item_key)
|
||||
local prices = item_records[item_key].bids_of_today:values()
|
||||
local prices = data.item_records[item_key].bids_of_today:values()
|
||||
|
||||
if getn(prices) == 0 then
|
||||
return
|
||||
@@ -123,13 +133,15 @@ function private.daily_bid_value(item_key)
|
||||
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 = item_records[item_key].buyouts_of_today:values()
|
||||
local data = private.load_data()
|
||||
local prices = data.item_records[item_key].buyouts_of_today:values()
|
||||
|
||||
if getn(prices) == 0 then
|
||||
return
|
||||
@@ -140,36 +152,12 @@ function private.daily_buyout_value(item_key)
|
||||
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_market_value(histogram)
|
||||
--
|
||||
-- local auction_count = 0
|
||||
-- for _, frequency in ipairs(histogram) do
|
||||
-- auction_count = auction_count + frequency
|
||||
-- end
|
||||
--
|
||||
-- if auction_count == 0 then
|
||||
-- return 0
|
||||
-- end
|
||||
--
|
||||
-- -- average of lowest 25%
|
||||
-- local sum, count = 0, 0
|
||||
-- local limit = auction_count * 0.25
|
||||
-- for i, frequency in ipairs(histogram) do
|
||||
-- local limited_frequency = min(frequency, limit - count)
|
||||
-- sum = sum + 1.1 ^ (i - 1) * 1.05 * limited_frequency
|
||||
-- count = count + limited_frequency
|
||||
-- if count >= limit then
|
||||
-- break
|
||||
-- end
|
||||
-- end
|
||||
-- return sum / limit
|
||||
--end
|
||||
|
||||
function private.median(list)
|
||||
if getn(list) == 0 then
|
||||
return
|
||||
@@ -185,9 +173,9 @@ function private.median(list)
|
||||
return (sorted_list[floor(middle)] + sorted_list[ceil(middle)]) / 2
|
||||
end
|
||||
|
||||
function private.push_data()
|
||||
function public.push_data()
|
||||
|
||||
for item_key, record in pairs(item_records) do
|
||||
for item_key, record in pairs(data.item_records) do
|
||||
|
||||
local daily_bid_value = private.daily_bid_value(item_key)
|
||||
if daily_bid_value then
|
||||
@@ -196,7 +184,7 @@ function private.push_data()
|
||||
while getn(record.daily_bid_values) > 11 do
|
||||
tremove(record.daily_bid_values, 1)
|
||||
end
|
||||
record.bids_of_today = {}
|
||||
record.bids_of_today = Aux.util.set()
|
||||
|
||||
local daily_buyout_value = private.daily_buyout_value(item_key)
|
||||
if daily_buyout_value then
|
||||
@@ -205,9 +193,9 @@ function private.push_data()
|
||||
while getn(record.daily_buyout_values) > 11 do
|
||||
tremove(record.daily_buyout_values, 1)
|
||||
end
|
||||
record.buyouts_of_today = {}
|
||||
record.buyouts_of_today = Aux.util.set()
|
||||
|
||||
end
|
||||
|
||||
next_push = time() + private.PUSH_INTERVAL
|
||||
data.next_push = time() + private.PUSH_INTERVAL
|
||||
end
|
||||
Reference in New Issue
Block a user