From 9c61a7745630fc7fa481a0755775775fd4f6cd9f Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Thu, 31 Mar 2016 17:39:36 +0200 Subject: [PATCH] persistence refactoring --- cache.lua | 68 +++++++++++++++++++++++++++---------------------- core.lua | 2 +- history.lua | 21 +++------------ persistence.lua | 53 +++++++++++++------------------------- post_frame.lua | 21 +++------------ 5 files changed, 62 insertions(+), 103 deletions(-) diff --git a/cache.lua b/cache.lua index a97c6f3..ab92640 100644 --- a/cache.lua +++ b/cache.lua @@ -9,8 +9,8 @@ aux_auctionable_items = {} aux_merchant_buy = {} aux_merchant_sell = {} -local items_schema = {'record', '#', {'string', 'number', 'number', 'string', 'string', 'string', 'number', 'string'}} -local merchant_buy_schema = {'record', '#', {'number', 'boolean'} } +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', '#', {unit_price='number'}, {limited='boolean'}} function public.on_load() private.scan_wdb() @@ -32,28 +32,28 @@ function public.on_load() end function public.merchant_info(item_id) - local unit_price, limited + local buy_info 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 - 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 function public.item_info(item_id) local data_string = aux_items[item_id] 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 { - name = name, + name = cached_data.name, itemstring = 'item:'..item_id..':0:0:0', - quality = quality, - level = level, - class = class, - subclass = subclass, - slot = slot, - max_stack = max_stack, - texture = texture, + quality = cached_data.quality, + level = cached_data.level, + class = cached_data.class, + subclass = cached_data.subclass, + slot = cached_data.slot, + max_stack = cached_data.max_stack, + texture = cached_data.texture, } end end @@ -79,20 +79,26 @@ function private.scan_merchant() local _, _, price, count, stock = GetMerchantItemInfo(i) local new_unit_price, new_limited = price / count, stock >= 0 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 - if old_limited and not new_limited then + if buy_info.limited and not new_limited then unit_price = new_unit_price - elseif new_limited and not old_limited then - unit_price = old_unit_price + elseif new_limited and not buy_info.limited then + unit_price = buy_info.unit_price else - unit_price = min(old_unit_price, new_unit_price) + unit_price = min(buy_info.unit_price, new_unit_price) 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 - 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 @@ -107,16 +113,16 @@ function private.scan_wdb() local name, _, quality, level, class, subclass, max_stack, slot, texture = GetItemInfo(itemstring) if name and not aux_item_ids[strlower(name)] then aux_item_ids[strlower(name)] = item_id - aux_items[item_id] = Aux.persistence.write(items_schema, - name, - quality, - level, - class, - subclass, - slot, - max_stack, - texture - ) + aux_items[item_id] = Aux.persistence.write(items_schema, { + name = name, + quality = quality, + level = level, + class = class, + subclass = subclass, + slot = slot, + max_stack = max_stack, + texture = texture, + }) local tooltip = Aux.info.tooltip(function(tt) tt:SetHyperlink(itemstring) end) if Aux.info.auctionable(tooltip, quality) then tinsert(aux_auctionable_items, strlower(name)) diff --git a/core.lua b/core.lua index ebc7f84..f48865e 100644 --- a/core.lua +++ b/core.lua @@ -1,5 +1,5 @@ Aux = { - version = '2.10.7', + version = '2.10.8', blizzard_ui_shown = false, orig = {}, } diff --git a/history.lua b/history.lua index dbef0b3..f2b12bb 100644 --- a/history.lua +++ b/history.lua @@ -1,6 +1,7 @@ local private, 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 = {} function private.next_push() @@ -24,16 +25,7 @@ function private.read_record(item_key) local record if data[item_key] then - local fields = Aux.util.split(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), - } + record = Aux.persistence.read(history_schema, data[item_key]) else record = private.new_record() end @@ -49,14 +41,7 @@ end function private.write_record(item_key, record) private.value_cache[item_key] = nil local data = private.load_data() - data[item_key] = Aux.util.join({ - 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), ';'), - },'#') + data[item_key] = Aux.persistence.write(history_schema, record) end function public.process_auction(auction_record) diff --git a/persistence.lua b/persistence.lua index 6541a32..2436f1d 100644 --- a/persistence.lua +++ b/persistence.lua @@ -19,22 +19,6 @@ function public.load_dataset() end 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 return str elseif schema == 'boolean' then @@ -42,15 +26,15 @@ function private.read(schema, str) elseif schema == 'number' then return tonumber(str) 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 - return private.read_record(schema, str) + return public.read_record(schema, str) else error('Unknown schema.') end end -function private.write(schema, obj) +function public.write(schema, obj) if schema == 'string' then return obj or '' elseif schema == 'boolean' then @@ -58,15 +42,15 @@ function private.write(schema, obj) elseif schema == 'number' then return obj and tostring(obj) or '' 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 - return private.write_record(schema, obj) + return public.write_record(schema, obj) else error('Unknown schema.') end end -function private.read_list(schema, str) +function public.read_list(schema, str) if str == '' then return {} end @@ -75,39 +59,36 @@ function private.read_list(schema, str) 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) + return public.read(element_type, part) end) end -function private.write_list(schema, list) +function public.write_list(schema, list) local separator = schema[2] local element_type = schema[3] local parts = Aux.util.map(list, function(element) - return private.write(element_type, element) + return public.write(element_type, element) end) return Aux.util.join(parts, separator) end -function private.read_record(schema, str) +function public.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 + for i=3,getn(schema) do + local key, type = next(schema[i]) + record[key] = public.read(type, parts[i - 2]) end return record end -function private.write_record(schema, record) +function public.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])) + for i=3,getn(schema) do + local key, type = next(schema[i]) + tinsert(parts, public.write(type, record[key])) end return Aux.util.join(parts, separator) end diff --git a/post_frame.lua b/post_frame.lua index 6af7962..048b226 100644 --- a/post_frame.lua +++ b/post_frame.lua @@ -7,6 +7,8 @@ local inventory_records local selected_item 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 BUYOUT_MODE, BID_MODE, FULL_MODE = 1, 2, 3 @@ -30,15 +32,7 @@ function private.read_settings(item_key) local settings if dataset.post[item_key] then - local fields = Aux.util.split(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]), - } + settings = Aux.persistence.read(settings_schema, dataset.post[item_key]) else settings = private.default_settings() end @@ -51,14 +45,7 @@ function private.write_settings(settings, item_key) local dataset = Aux.persistence.load_dataset() dataset.post = dataset.post or {} - dataset.post[item_key] = Aux.util.join({ - settings.stack_size, - settings.duration, - settings.start_price, - settings.buyout_price, - tostring(settings.post_all), - tostring(settings.hidden), - }, '#') + dataset.post[item_key] = Aux.persistence.write(settings_schema, settings) end function private.get_unit_start_price()