new persistence functionality

This commit is contained in:
Manuel Simon Hirsig
2016-03-21 11:12:10 +01:00
parent 0e3e7255c1
commit fe115de8bb
2 changed files with 86 additions and 39 deletions
+7 -13
View File
@@ -9,6 +9,8 @@ aux_auctionable_items = {}
aux_merchant_buy = {} aux_merchant_buy = {}
aux_merchant_sell = {} aux_merchant_sell = {}
local merchant_buy_schema = {'record', '#', {'number', 'boolean'}}
function public.on_load() function public.on_load()
private.scan_wdb() private.scan_wdb()
Aux.control.event_listener('MERCHANT_SHOW', private.scan_merchant).start() Aux.control.event_listener('MERCHANT_SHOW', private.scan_merchant).start()
@@ -21,8 +23,7 @@ end
function public.merchant_info(item_id) function public.merchant_info(item_id)
local unit_price, limited local unit_price, limited
if aux_merchant_buy[item_id] then if aux_merchant_buy[item_id] then
local buy_fields = Aux.util.split(aux_merchant_buy[item_id], '#') local unit_price, limited = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
unit_price, limited = tonumber(buy_fields[1]), not not tonumber(buy_fields[2])
end end
return aux_merchant_sell[item_id], unit_price, limited return aux_merchant_sell[item_id], unit_price, limited
@@ -67,10 +68,9 @@ function private.scan_merchant()
if link then if link then
local item_id = Aux.info.parse_hyperlink(link) local item_id = Aux.info.parse_hyperlink(link)
local _, _, price, count, stock = GetMerchantItemInfo(i) local _, _, price, count, stock = GetMerchantItemInfo(i)
local new_unit_price, new_limited = price / count, Aux.persistence.blizzard_boolean(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 fields = Aux.util.split(aux_merchant_buy[item_id], '#') local old_unit_price, old_limited = Aux.persistence.read(merchant_buy_schema, aux_merchant_buy[item_id])
local old_unit_price, old_limited = tonumber(fields[1]), tonumber(fields[2])
local unit_price local unit_price
if old_limited and not new_limited then if old_limited and not new_limited then
@@ -81,15 +81,9 @@ function private.scan_merchant()
unit_price = min(old_unit_price, new_unit_price) unit_price = min(old_unit_price, new_unit_price)
end end
aux_merchant_buy[item_id] = Aux.util.join({ aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, unit_price, old_limited or new_limited)
unit_price,
tostring(old_limited or new_limited),
}, '#')
else else
aux_merchant_buy[item_id] = Aux.util.join({ aux_merchant_buy[item_id] = Aux.persistence.write(merchant_buy_schema, new_unit_price, new_limited)
new_unit_price,
tostring(new_limited),
}, '#')
end end
end end
end end
+79 -26
View File
@@ -18,45 +18,98 @@ function public.load_dataset()
return aux_datasets[dataset_key] return aux_datasets[dataset_key]
end end
function private.read(type, str) function public.read(schema, str)
if type == 'string' then 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
return str return str
elseif type == 'boolean' then elseif schema == 'boolean' then
return str == '1' return str == '1'
elseif type == 'number' then elseif schema == 'number' then
return tonumber(str) return tonumber(str)
elseif type(schema) == 'table' and schema[1] == 'list' then
return private.read_list(schema, str)
elseif type(schema) == 'table' and schema[1] == 'record' then
return private.read_record(schema, str)
else
error('Unknown schema.')
end end
end end
function private.write(type, obj) function private.write(schema, obj)
if type == 'string' then if schema == 'string' then
return obj return obj or ''
elseif type == 'boolean' then elseif schema == 'boolean' then
return obj and '1' or '0' return obj and '1' or '0'
elseif type == 'number' then elseif schema == 'number' then
return tostring(obj) return obj and tostring(obj) or ''
elseif type(schema) == 'table' and schema[1] == 'list' then
return private.write_list(schema, obj)
elseif type(schema) == 'table' and schema[1] == 'record' then
return private.write_record(schema, obj)
else
error('Unknown schema.')
end end
end end
function public.schema(separator, ...) function private.read_list(schema, str)
return function(record) if str == '' then
local fields return {}
local parts = Aux.util.split(record, separator)
for i=1,arg.n do
tinsert(fields, private.read(arg[i], parts[i]))
end
return fields
end, function(fields)
local parts = {}
for i=1,arg.n do
tinsert(parts, private.write(arg[i], fields[i]))
end
return Aux.util.join(parts, '#')
end end
local separator = schema[2]
local element_type = schema[3]
local parts = Aux.util.split(str, separator)
return Aux.util.map(parts, function(part)
return private.read(element_type, part)
end)
end end
function public.blizzard_boolean(boolean) function private.write_list(schema, list)
return boolean and 1 or nil local separator = schema[2]
local element_type = schema[3]
local parts = Aux.util.map(list, function(element)
return private.write(element_type, element)
end)
return Aux.util.join(parts, separator)
end
function private.read_record(schema, str)
local separator = schema[2]
local record = {}
local parts = Aux.util.split(str, separator)
for i, type in ipairs(schema[3]) do
local field = private.read(type, parts[i])
if field ~= nil then
tinsert(record, field)
else
tinsert(record, nil)
end
end
return record
end
function private.write_record(schema, record)
local separator = schema[2]
local parts = {}
for i, type in ipairs(schema[3]) do
tinsert(parts, private.write(type, record[i]))
end
return Aux.util.join(parts, separator)
end end
function public.serialize(data, separator, compactor) function public.serialize(data, separator, compactor)