refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-23 17:37:17 +02:00
parent 303ef7f9c3
commit c65417aa9c
3 changed files with 69 additions and 114 deletions
+4 -2
View File
@@ -57,14 +57,14 @@ do
}) })
end end
local temp, perm = modifier(function(t) transient[t] = true; return setmetatable(t, nil) end), modifier(function(t) transient[t] = nil; return t end) local temp, perm = modifier(function(t) transient[t] = true; return setmetatable(t, nil) end), modifier(function(t) transient[t] = nil; return t end)
public.temp() -- TODO or 'auto' 'free' 'release'? public.temp() -- TODO or 'auto' 'free' 'deprecate' 'release'?
function accessor() return temp end function accessor() return temp end
function mutator(t) return temp(t) end function mutator(t) return temp(t) end
public.perm() public.perm()
function accessor() return perm end function accessor() return perm end
function mutator(t) return perm(t) end function mutator(t) return perm(t) end
function public.collector_mt(f) function public.collector_mt(f) -- TODO somehow use an existing table
return { return {
__unm=function(self) __unm=function(self)
return setmetatable(self, nil) return setmetatable(self, nil)
@@ -127,6 +127,8 @@ do
elseif event == 'PLAYER_LOGIN' then elseif event == 'PLAYER_LOGIN' then
for _, f in handlers2 do f() end for _, f in handlers2 do f() end
log('v'..version..' loaded.') log('v'..version..' loaded.')
-- TODO test __lt
else else
_m[event]() _m[event]()
end end
+63 -98
View File
@@ -3,8 +3,39 @@ module 'post_tab' import 'scan' 'scan_util' 'post' 'history' 'info' 'persistence
local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440 local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440
local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}} local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}}
function accessor.default_settings()
return -object
:duration(DURATION_8)
:stack_size(1)
:start_price(0)
:buyout_price(0)
:hidden(false)
end
do
local data
function accessor.data()
if not data then
local dataset = persistence.dataset
data = dataset.post or t
dataset.post = data
end
return data
end
end
function read_settings(item_key)
item_key = item_key or selected_item.key
return data[item_key] and persistence.read(settings_schema, data[item_key]) or default_settings
end
function write_settings(settings, item_key)
item_key = item_key or selected_item.key
data[item_key] = persistence.write(settings_schema, settings)
end
existing_auctions = t existing_auctions = t
mutable.inventory_records = nil inventory_records = t
mutable.scan_id = 0 mutable.scan_id = 0
mutable.selected_item = nil mutable.selected_item = nil
mutable.refresh = nil mutable.refresh = nil
@@ -15,14 +46,7 @@ end
function OPEN() function OPEN()
frame:Show() frame:Show()
deposit:SetText('Deposit: '..money.to_string(0, nil, nil, nil, gui.inline_color.text.enabled))
set_unit_start_price(0)
set_unit_buyout_price(0)
update_inventory_records() update_inventory_records()
refresh = true refresh = true
end end
@@ -35,39 +59,6 @@ function USE_ITEM(item_info)
select_item(item_info.item_key) select_item(item_info.item_key)
end end
function accessor.default_settings()
return {
duration = DURATION_8,
stack_size = 1,
start_price = 0,
buyout_price = 0,
hidden = false,
}
end
function read_settings(item_key)
item_key = item_key or selected_item.key
local dataset = persistence.load_dataset()
dataset.post = dataset.post or t
local settings
if dataset.post[item_key] then
settings = persistence.read(settings_schema, dataset.post[item_key])
else
settings = default_settings
end
return settings
end
function write_settings(settings, item_key)
item_key = item_key or selected_item.key
local dataset = persistence.load_dataset()
dataset.post = dataset.post or t
dataset.post[item_key] = persistence.write(settings_schema, settings)
end
function get_unit_start_price() function get_unit_start_price()
local money_text = unit_start_price:GetText() local money_text = unit_start_price:GetText()
return money.from_string(money_text) or 0 return money.from_string(money_text) or 0
@@ -171,7 +162,7 @@ function public.select_item(item_key)
for _, inventory_record in filter(inventory_records, function(record) return record.aux_quantity > 0 end) do for _, inventory_record in filter(inventory_records, function(record) return record.aux_quantity > 0 end) do
if inventory_record.key == item_key then if inventory_record.key == item_key then
set_item(inventory_record) set_item(inventory_record)
break return
end end
end end
end end
@@ -225,7 +216,6 @@ function post_auctions()
for i=1,posted do for i=1,posted do
new_auction_record = record_auction(key, stack_size, unit_start_price, unit_buyout_price, duration_code, UnitName('player')) new_auction_record = record_auction(key, stack_size, unit_start_price, unit_buyout_price, duration_code, UnitName('player'))
end end
update_inventory_records() update_inventory_records()
selected_item = nil selected_item = nil
for _, record in inventory_records do for _, record in inventory_records do
@@ -233,7 +223,6 @@ function post_auctions()
set_item(record) set_item(record)
end end
end end
refresh = true refresh = true
end end
) )
@@ -241,32 +230,26 @@ function post_auctions()
end end
function validate_parameters() function validate_parameters()
if not selected_item then if not selected_item then
post_button:Disable() post_button:Disable()
return return
end end
if get_unit_buyout_price() > 0 and get_unit_start_price() > get_unit_buyout_price() then if get_unit_buyout_price() > 0 and get_unit_start_price() > get_unit_buyout_price() then
post_button:Disable() post_button:Disable()
return return
end end
if get_unit_start_price() == 0 then if get_unit_start_price() == 0 then
post_button:Disable() post_button:Disable()
return return
end end
if stack_count_slider:GetValue() == 0 then if stack_count_slider:GetValue() == 0 then
post_button:Disable() post_button:Disable()
return return
end end
post_button:Enable() post_button:Enable()
end end
function update_item_configuration() function update_item_configuration()
if not selected_item then if not selected_item then
refresh_button:Disable() refresh_button:Disable()
@@ -283,6 +266,10 @@ function update_item_configuration()
duration_dropdown:Hide() duration_dropdown:Hide()
historical_value_button:Hide() historical_value_button:Hide()
hide_checkbox:Hide() hide_checkbox:Hide()
-- deposit:SetText('Deposit: '..money.to_string(0, nil, nil, nil, gui.inline_color.text.enabled))
-- set_unit_start_price(0)
-- set_unit_buyout_price(0)
else else
start_price_frame:Show() start_price_frame:Show()
buyout_price_frame:Show() buyout_price_frame:Show()
@@ -308,12 +295,9 @@ function update_item_configuration()
do do
local deposit_factor = neutral_faction() and 0.25 or 0.05 local deposit_factor = neutral_faction() and 0.25 or 0.05
local stack_size = stack_size_slider:GetValue() local stack_size, stack_count = stack_size_slider:GetValue(), stack_count_slider:GetValue()
local stack_count local amount = floor(selected_item.unit_vendor_price * deposit_factor * (selected_item.max_charges and 1 or stack_size)) * stack_count * UIDropDownMenu_GetSelectedValue(duration_dropdown) / 120
stack_count = stack_count_slider:GetValue() deposit:SetText('Deposit: '..money.to_string(amount, nil, nil, nil, gui.inline_color.text.enabled))
local deposit = floor(selected_item.unit_vendor_price * deposit_factor * (selected_item.max_charges and 1 or stack_size)) * stack_count * UIDropDownMenu_GetSelectedValue(duration_dropdown) / 120
_m.deposit:SetText('Deposit: '..money.to_string(deposit, nil, nil, nil, gui.inline_color.text.enabled))
end end
refresh_button:Enable() refresh_button:Enable()
@@ -323,12 +307,10 @@ end
function undercut(record, stack_size, stack) function undercut(record, stack_size, stack)
local start_price = round(record.unit_blizzard_bid * (stack and record.stack_size or stack_size)) local start_price = round(record.unit_blizzard_bid * (stack and record.stack_size or stack_size))
local buyout_price = round(record.unit_buyout_price * (stack and record.stack_size or stack_size)) local buyout_price = round(record.unit_buyout_price * (stack and record.stack_size or stack_size))
if not record.own then if not record.own then
start_price = max(0, start_price - 1) start_price = max(0, start_price - 1)
buyout_price = max(0, buyout_price - 1) buyout_price = max(0, buyout_price - 1)
end end
return start_price / stack_size, buyout_price / stack_size return start_price / stack_size, buyout_price / stack_size
end end
@@ -344,12 +326,9 @@ function quantity_update(max_count)
end end
function unit_vendor_price(item_key) function unit_vendor_price(item_key)
for slot in info.inventory do temp=slot for slot in info.inventory do temp=slot
local item_info = info.container_item(unpack(slot)) local item_info = info.container_item(unpack(slot))
if item_info and item_info.item_key == item_key then if item_info and item_info.item_key == item_key then
if info.auctionable(item_info.tooltip, nil, item_info.lootable) then if info.auctionable(item_info.tooltip, nil, item_info.lootable) then
ClearCursor() ClearCursor()
PickupContainerItem(unpack(slot)) PickupContainerItem(unpack(slot))
@@ -358,7 +337,6 @@ function unit_vendor_price(item_key)
ClearCursor() ClearCursor()
ClickAuctionSellItemButton() ClickAuctionSellItemButton()
ClearCursor() ClearCursor()
if auction_sell_item then if auction_sell_item then
return auction_sell_item.vendor_price / auction_sell_item.count return auction_sell_item.vendor_price / auction_sell_item.count
end end
@@ -411,53 +389,41 @@ function set_item(item)
end end
function update_inventory_records() function update_inventory_records()
inventory_records = {} local auctionable_map = tt
refresh = true for slot in info.inventory do temp=slot
for item_info in present(info.container_item(unpack(slot))) do
local auction_candidate_map = {}
for slot in info.inventory do
local item_info = info.container_item(unpack(slot))
if item_info then
local charge_class = item_info.charges or 0 local charge_class = item_info.charges or 0
if info.auctionable(item_info.tooltip, nil, item_info.lootable) then if info.auctionable(item_info.tooltip, nil, item_info.lootable) then
if not auction_candidate_map[item_info.item_key] then if not auctionable_map[item_info.item_key] then
local availability = t
local availability = {}
for i=0,10 do for i=0,10 do
availability[i] = 0 availability[i] = 0
end end
availability[charge_class] = item_info.count availability[charge_class] = item_info.count
auctionable_map[item_info.item_key] = -object
auction_candidate_map[item_info.item_key] = { :item_id (item_info.item_id)
item_id = item_info.item_id, :suffix_id (item_info.suffix_id)
suffix_id = item_info.suffix_id, :key (item_info.item_key)
:itemstring (item_info.itemstring)
key = item_info.item_key, :name (item_info.name)
itemstring = item_info.itemstring, :texture (item_info.texture)
:quality (item_info.quality)
name = item_info.name, :aux_quantity (item_info.charges or item_info.count)
texture = item_info.texture, :max_stack (item_info.max_stack)
quality = item_info.quality, :max_charges (item_info.max_charges)
aux_quantity = item_info.charges or item_info.count, :availability (availability)
max_stack = item_info.max_stack,
max_charges = item_info.max_charges,
availability = availability,
}
else else
local candidate = auction_candidate_map[item_info.item_key] local auctionable = auctionable_map[item_info.item_key]
candidate.availability[charge_class] = (candidate.availability[charge_class] or 0) + item_info.count auctionable.availability[charge_class] = (auctionable.availability[charge_class] or 0) + item_info.count
candidate.aux_quantity = candidate.aux_quantity + (item_info.charges or item_info.count) auctionable.aux_quantity = auctionable.aux_quantity + (item_info.charges or item_info.count)
end end
end end
end end
end end
inventory_records = {} wipe(inventory_records)
for _, auction_candidate in auction_candidate_map do for _, auctionable in auctionable_map do
tinsert(inventory_records, auction_candidate) tinsert(inventory_records, auctionable)
end end
sort(inventory_records, function(a, b) return a.name < b.name end) sort(inventory_records, function(a, b) return a.name < b.name end)
refresh = true refresh = true
@@ -546,7 +512,6 @@ function on_update()
update_inventory_listing() update_inventory_listing()
update_auction_listing() update_auction_listing()
end end
validate_parameters() validate_parameters()
end end
+2 -14
View File
@@ -14,18 +14,6 @@ do
end end
end end
--do TODO syntactic sugar record '#' {key='string'} {foo='number'}
-- local mt = {
-- __unm = function(self, separator)
-- self.separator = separator
-- end,
-- __call = function(self, field) end
-- }
-- function public.accessor.record()
--
-- end
--end
function public.read(schema, str) function public.read(schema, str)
if schema == 'string' then if schema == 'string' then
return str return str
@@ -38,7 +26,7 @@ function public.read(schema, str)
elseif type(schema) == 'table' and schema[1] == 'record' then elseif type(schema) == 'table' and schema[1] == 'record' then
return read_record(schema, str) return read_record(schema, str)
else else
error 'Invalid schema.' error('Invalid schema.', 2)
end end
end end
@@ -54,7 +42,7 @@ function public.write(schema, obj)
elseif type(schema) == 'table' and schema[1] == 'record' then elseif type(schema) == 'table' and schema[1] == 'record' then
return write_record(schema, obj) return write_record(schema, obj)
else else
error 'Invalid schema.' error('Invalid schema.', 2)
end end
end end