persistence refactoring
This commit is contained in:
@@ -9,8 +9,8 @@ aux_auctionable_items = {}
|
|||||||
aux_merchant_buy = {}
|
aux_merchant_buy = {}
|
||||||
aux_merchant_sell = {}
|
aux_merchant_sell = {}
|
||||||
|
|
||||||
local items_schema = {'record', '#', {'string', 'number', 'number', 'string', 'string', 'string', 'number', 'string'}}
|
local items_schema = {'record', '#', {name='string'}, {quality='number'}, {level='number'}, {class='string'}, {subclass='string'}, {slot='string'}, {max_stack='number'}, {texture='string'}}
|
||||||
local merchant_buy_schema = {'record', '#', {'number', 'boolean'} }
|
local merchant_buy_schema = {'record', '#', {unit_price='number'}, {limited='boolean'}}
|
||||||
|
|
||||||
function public.on_load()
|
function public.on_load()
|
||||||
private.scan_wdb()
|
private.scan_wdb()
|
||||||
@@ -32,28 +32,28 @@ function public.on_load()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function public.merchant_info(item_id)
|
function public.merchant_info(item_id)
|
||||||
local unit_price, limited
|
local buy_info
|
||||||
if aux_merchant_buy[item_id] then
|
if aux_merchant_buy[item_id] then
|
||||||
unit_price, limited = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
|
buy_info = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
return aux_merchant_sell[item_id], unit_price, limited
|
return aux_merchant_sell[item_id], buy_info and buy_info.unit_price, buy_info and buy_info.limited
|
||||||
end
|
end
|
||||||
|
|
||||||
function public.item_info(item_id)
|
function public.item_info(item_id)
|
||||||
local data_string = aux_items[item_id]
|
local data_string = aux_items[item_id]
|
||||||
if data_string then
|
if data_string then
|
||||||
local name, quality, level, class, subclass, slot, max_stack, texture = Aux.persistence.read(items_schema, data_string)
|
local cached_data = Aux.persistence.read(items_schema, data_string)
|
||||||
return {
|
return {
|
||||||
name = name,
|
name = cached_data.name,
|
||||||
itemstring = 'item:'..item_id..':0:0:0',
|
itemstring = 'item:'..item_id..':0:0:0',
|
||||||
quality = quality,
|
quality = cached_data.quality,
|
||||||
level = level,
|
level = cached_data.level,
|
||||||
class = class,
|
class = cached_data.class,
|
||||||
subclass = subclass,
|
subclass = cached_data.subclass,
|
||||||
slot = slot,
|
slot = cached_data.slot,
|
||||||
max_stack = max_stack,
|
max_stack = cached_data.max_stack,
|
||||||
texture = texture,
|
texture = cached_data.texture,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -79,20 +79,26 @@ function private.scan_merchant()
|
|||||||
local _, _, price, count, stock = GetMerchantItemInfo(i)
|
local _, _, price, count, stock = GetMerchantItemInfo(i)
|
||||||
local new_unit_price, new_limited = price / count, stock >= 0
|
local new_unit_price, new_limited = price / count, stock >= 0
|
||||||
if aux_merchant_buy[item_id] then
|
if aux_merchant_buy[item_id] then
|
||||||
local old_unit_price, old_limited = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
|
local buy_info = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
|
||||||
|
|
||||||
local unit_price
|
local unit_price
|
||||||
if old_limited and not new_limited then
|
if buy_info.limited and not new_limited then
|
||||||
unit_price = new_unit_price
|
unit_price = new_unit_price
|
||||||
elseif new_limited and not old_limited then
|
elseif new_limited and not buy_info.limited then
|
||||||
unit_price = old_unit_price
|
unit_price = buy_info.unit_price
|
||||||
else
|
else
|
||||||
unit_price = min(old_unit_price, new_unit_price)
|
unit_price = min(buy_info.unit_price, new_unit_price)
|
||||||
end
|
end
|
||||||
|
|
||||||
aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, unit_price, old_limited and new_limited)
|
aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, {
|
||||||
|
unit_price = unit_price,
|
||||||
|
limited = buy_info.limited and new_limited,
|
||||||
|
})
|
||||||
else
|
else
|
||||||
aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, new_unit_price, new_limited)
|
aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, {
|
||||||
|
unit_price = new_unit_price,
|
||||||
|
limited = new_limited,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -107,16 +113,16 @@ function private.scan_wdb()
|
|||||||
local name, _, quality, level, class, subclass, max_stack, slot, texture = GetItemInfo(itemstring)
|
local name, _, quality, level, class, subclass, max_stack, slot, texture = GetItemInfo(itemstring)
|
||||||
if name and not aux_item_ids[strlower(name)] then
|
if name and not aux_item_ids[strlower(name)] then
|
||||||
aux_item_ids[strlower(name)] = item_id
|
aux_item_ids[strlower(name)] = item_id
|
||||||
aux_items[item_id] = Aux.persistence.write(items_schema,
|
aux_items[item_id] = Aux.persistence.write(items_schema, {
|
||||||
name,
|
name = name,
|
||||||
quality,
|
quality = quality,
|
||||||
level,
|
level = level,
|
||||||
class,
|
class = class,
|
||||||
subclass,
|
subclass = subclass,
|
||||||
slot,
|
slot = slot,
|
||||||
max_stack,
|
max_stack = max_stack,
|
||||||
texture
|
texture = texture,
|
||||||
)
|
})
|
||||||
local tooltip = Aux.info.tooltip(function(tt) tt:SetHyperlink(itemstring) end)
|
local tooltip = Aux.info.tooltip(function(tt) tt:SetHyperlink(itemstring) end)
|
||||||
if Aux.info.auctionable(tooltip, quality) then
|
if Aux.info.auctionable(tooltip, quality) then
|
||||||
tinsert(aux_auctionable_items, strlower(name))
|
tinsert(aux_auctionable_items, strlower(name))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
Aux = {
|
Aux = {
|
||||||
version = '2.10.7',
|
version = '2.10.8',
|
||||||
blizzard_ui_shown = false,
|
blizzard_ui_shown = false,
|
||||||
orig = {},
|
orig = {},
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-18
@@ -1,6 +1,7 @@
|
|||||||
local private, public = {}, {}
|
local private, public = {}, {}
|
||||||
Aux.history = public
|
Aux.history = public
|
||||||
|
|
||||||
|
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 = {}
|
private.value_cache = {}
|
||||||
|
|
||||||
function private.next_push()
|
function private.next_push()
|
||||||
@@ -24,16 +25,7 @@ function private.read_record(item_key)
|
|||||||
|
|
||||||
local record
|
local record
|
||||||
if data[item_key] then
|
if data[item_key] then
|
||||||
local fields = Aux.util.split(data[item_key], '#')
|
record = Aux.persistence.read(history_schema, data[item_key])
|
||||||
record = {
|
|
||||||
next_push = tonumber(fields[1]),
|
|
||||||
daily_min_buyout = tonumber(fields[2]),
|
|
||||||
daily_max_price = tonumber(fields[3]),
|
|
||||||
data_points = Aux.util.map(Aux.persistence.deserialize(fields[4], ';'), function(data_point)
|
|
||||||
local market_value, time = unpack(Aux.util.split(data_point, '@'))
|
|
||||||
return { market_value = tonumber(market_value), time = tonumber(time) }
|
|
||||||
end),
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
record = private.new_record()
|
record = private.new_record()
|
||||||
end
|
end
|
||||||
@@ -49,14 +41,7 @@ end
|
|||||||
function private.write_record(item_key, record)
|
function private.write_record(item_key, record)
|
||||||
private.value_cache[item_key] = nil
|
private.value_cache[item_key] = nil
|
||||||
local data = private.load_data()
|
local data = private.load_data()
|
||||||
data[item_key] = Aux.util.join({
|
data[item_key] = Aux.persistence.write(history_schema, record)
|
||||||
record.next_push or '',
|
|
||||||
record.daily_min_buyout or '',
|
|
||||||
record.daily_max_price or '',
|
|
||||||
Aux.persistence.serialize(Aux.util.map(record.data_points, function(data_point)
|
|
||||||
return Aux.util.join({data_point.market_value, data_point.time}, '@')
|
|
||||||
end), ';'),
|
|
||||||
},'#')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function public.process_auction(auction_record)
|
function public.process_auction(auction_record)
|
||||||
|
|||||||
+17
-36
@@ -19,22 +19,6 @@ function public.load_dataset()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function public.read(schema, str)
|
function public.read(schema, str)
|
||||||
if type(schema) == 'table' and schema[1] == 'record' then
|
|
||||||
return unpack(private.read(schema, str))
|
|
||||||
else
|
|
||||||
return private.read(schema, str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function public.write(schema, ...)
|
|
||||||
if type(schema) == 'table' and schema[1] == 'record' then
|
|
||||||
return private.write(schema, arg)
|
|
||||||
else
|
|
||||||
return private.write(schema, arg[1])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function private.read(schema, str)
|
|
||||||
if schema == 'string' then
|
if schema == 'string' then
|
||||||
return str
|
return str
|
||||||
elseif schema == 'boolean' then
|
elseif schema == 'boolean' then
|
||||||
@@ -42,15 +26,15 @@ function private.read(schema, str)
|
|||||||
elseif schema == 'number' then
|
elseif schema == 'number' then
|
||||||
return tonumber(str)
|
return tonumber(str)
|
||||||
elseif type(schema) == 'table' and schema[1] == 'list' then
|
elseif type(schema) == 'table' and schema[1] == 'list' then
|
||||||
return private.read_list(schema, str)
|
return public.read_list(schema, str)
|
||||||
elseif type(schema) == 'table' and schema[1] == 'record' then
|
elseif type(schema) == 'table' and schema[1] == 'record' then
|
||||||
return private.read_record(schema, str)
|
return public.read_record(schema, str)
|
||||||
else
|
else
|
||||||
error('Unknown schema.')
|
error('Unknown schema.')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.write(schema, obj)
|
function public.write(schema, obj)
|
||||||
if schema == 'string' then
|
if schema == 'string' then
|
||||||
return obj or ''
|
return obj or ''
|
||||||
elseif schema == 'boolean' then
|
elseif schema == 'boolean' then
|
||||||
@@ -58,15 +42,15 @@ function private.write(schema, obj)
|
|||||||
elseif schema == 'number' then
|
elseif schema == 'number' then
|
||||||
return obj and tostring(obj) or ''
|
return obj and tostring(obj) or ''
|
||||||
elseif type(schema) == 'table' and schema[1] == 'list' then
|
elseif type(schema) == 'table' and schema[1] == 'list' then
|
||||||
return private.write_list(schema, obj)
|
return public.write_list(schema, obj)
|
||||||
elseif type(schema) == 'table' and schema[1] == 'record' then
|
elseif type(schema) == 'table' and schema[1] == 'record' then
|
||||||
return private.write_record(schema, obj)
|
return public.write_record(schema, obj)
|
||||||
else
|
else
|
||||||
error('Unknown schema.')
|
error('Unknown schema.')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.read_list(schema, str)
|
function public.read_list(schema, str)
|
||||||
if str == '' then
|
if str == '' then
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
@@ -75,39 +59,36 @@ function private.read_list(schema, str)
|
|||||||
local element_type = schema[3]
|
local element_type = schema[3]
|
||||||
local parts = Aux.util.split(str, separator)
|
local parts = Aux.util.split(str, separator)
|
||||||
return Aux.util.map(parts, function(part)
|
return Aux.util.map(parts, function(part)
|
||||||
return private.read(element_type, part)
|
return public.read(element_type, part)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.write_list(schema, list)
|
function public.write_list(schema, list)
|
||||||
local separator = schema[2]
|
local separator = schema[2]
|
||||||
local element_type = schema[3]
|
local element_type = schema[3]
|
||||||
local parts = Aux.util.map(list, function(element)
|
local parts = Aux.util.map(list, function(element)
|
||||||
return private.write(element_type, element)
|
return public.write(element_type, element)
|
||||||
end)
|
end)
|
||||||
return Aux.util.join(parts, separator)
|
return Aux.util.join(parts, separator)
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.read_record(schema, str)
|
function public.read_record(schema, str)
|
||||||
local separator = schema[2]
|
local separator = schema[2]
|
||||||
local record = {}
|
local record = {}
|
||||||
local parts = Aux.util.split(str, separator)
|
local parts = Aux.util.split(str, separator)
|
||||||
for i, type in ipairs(schema[3]) do
|
for i=3,getn(schema) do
|
||||||
local field = private.read(type, parts[i])
|
local key, type = next(schema[i])
|
||||||
if field ~= nil then
|
record[key] = public.read(type, parts[i - 2])
|
||||||
tinsert(record, field)
|
|
||||||
else
|
|
||||||
tinsert(record, nil)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return record
|
return record
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.write_record(schema, record)
|
function public.write_record(schema, record)
|
||||||
local separator = schema[2]
|
local separator = schema[2]
|
||||||
local parts = {}
|
local parts = {}
|
||||||
for i, type in ipairs(schema[3]) do
|
for i=3,getn(schema) do
|
||||||
tinsert(parts, private.write(type, record[i]))
|
local key, type = next(schema[i])
|
||||||
|
tinsert(parts, public.write(type, record[key]))
|
||||||
end
|
end
|
||||||
return Aux.util.join(parts, separator)
|
return Aux.util.join(parts, separator)
|
||||||
end
|
end
|
||||||
|
|||||||
+4
-17
@@ -7,6 +7,8 @@ local inventory_records
|
|||||||
local selected_item
|
local selected_item
|
||||||
local scan_id
|
local scan_id
|
||||||
|
|
||||||
|
local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {post_all='boolean'}, {hidden='boolean'}}
|
||||||
|
|
||||||
local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440
|
local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440
|
||||||
local BUYOUT_MODE, BID_MODE, FULL_MODE = 1, 2, 3
|
local BUYOUT_MODE, BID_MODE, FULL_MODE = 1, 2, 3
|
||||||
|
|
||||||
@@ -30,15 +32,7 @@ function private.read_settings(item_key)
|
|||||||
|
|
||||||
local settings
|
local settings
|
||||||
if dataset.post[item_key] then
|
if dataset.post[item_key] then
|
||||||
local fields = Aux.util.split(dataset.post[item_key], '#')
|
settings = Aux.persistence.read(settings_schema, dataset.post[item_key])
|
||||||
settings = {
|
|
||||||
stack_size = tonumber(fields[1]),
|
|
||||||
duration = tonumber(fields[2]),
|
|
||||||
start_price = tonumber(fields[3]),
|
|
||||||
buyout_price = tonumber(fields[4]),
|
|
||||||
post_all = tonumber(fields[5]),
|
|
||||||
hidden = tonumber(fields[6]),
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
settings = private.default_settings()
|
settings = private.default_settings()
|
||||||
end
|
end
|
||||||
@@ -51,14 +45,7 @@ function private.write_settings(settings, item_key)
|
|||||||
local dataset = Aux.persistence.load_dataset()
|
local dataset = Aux.persistence.load_dataset()
|
||||||
dataset.post = dataset.post or {}
|
dataset.post = dataset.post or {}
|
||||||
|
|
||||||
dataset.post[item_key] = Aux.util.join({
|
dataset.post[item_key] = Aux.persistence.write(settings_schema, settings)
|
||||||
settings.stack_size,
|
|
||||||
settings.duration,
|
|
||||||
settings.start_price,
|
|
||||||
settings.buyout_price,
|
|
||||||
tostring(settings.post_all),
|
|
||||||
tostring(settings.hidden),
|
|
||||||
}, '#')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function private.get_unit_start_price()
|
function private.get_unit_start_price()
|
||||||
|
|||||||
Reference in New Issue
Block a user