module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+75 -82
View File
@@ -1,8 +1,9 @@
module 'aux.tabs.post'
include 'T'
include 'aux'
local T = require 'T'
local info = require 'aux.util.info'
local sort_util = require 'aux.util.sort'
local persistence = require 'aux.util.persistence'
@@ -11,28 +12,31 @@ local scan_util = require 'aux.util.scan'
local post = require 'aux.core.post'
local scan = require 'aux.core.scan'
local history = require 'aux.core.history'
local cache = require 'aux.core.cache'
local item_listing = require 'aux.gui.item_listing'
local al = require 'aux.gui.auction_listing'
TAB 'Post'
local tab = TAB 'Post'
local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440
local settings_schema = {'tuple', '#', {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}}
local scan_id, inventory_records, bid_records, buyout_records = 0, {}, {}, {}
function get.default_settings()
return O('duration', DURATION_8, 'start_price', 0, 'buyout_price', 0, 'hidden', false)
refresh = true
selected_item = nil
function get_default_settings()
return T.map('duration', DURATION_8, 'start_price', 0, 'buyout_price', 0, 'hidden', false)
end
function LOAD2()
function handle.LOAD2()
data = faction_data'post'
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
return data[item_key] and persistence.read(settings_schema, data[item_key]) or get_default_settings()
end
function write_settings(settings, item_key)
item_key = item_key or selected_item.key
@@ -41,16 +45,16 @@ end
do
local bid_selections, buyout_selections = {}, {}
function get.bid_selection()
function get_bid_selection()
return bid_selections[selected_item.key]
end
function set.bid_selection(record)
function set_bid_selection(record)
bid_selections[selected_item.key] = record
end
function get.buyout_selection()
function get_buyout_selection()
return buyout_selections[selected_item.key]
end
function set.buyout_selection(record)
function set_buyout_selection(record)
buyout_selections[selected_item.key] = record
end
end
@@ -61,48 +65,36 @@ function refresh_button_click()
refresh = true
end
do
local item
function get.selected_item() return item end
function set.selected_item(v) item = v end
end
do
local c = 0
function get.refresh() return c end
function set.refresh(v) c = v end
end
function OPEN()
function tab.OPEN()
frame:Show()
update_inventory_records()
refresh = true
end
function CLOSE()
function tab.CLOSE()
selected_item = nil
frame:Hide()
end
function USE_ITEM(item_info)
function tab.USE_ITEM(item_info)
select_item(item_info.item_key)
end
function get.unit_start_price()
function get_unit_start_price()
return selected_item and read_settings().start_price or 0
end
function set.unit_start_price(amount)
function set_unit_start_price(amount)
local settings = read_settings()
settings.start_price = amount
write_settings(settings)
end
function get.unit_buyout_price()
function get_unit_buyout_price()
return selected_item and read_settings().buyout_price or 0
end
function set.unit_buyout_price(amount)
function set_unit_buyout_price(amount)
local settings = read_settings()
settings.buyout_price = amount
write_settings(settings)
@@ -118,34 +110,34 @@ function update_inventory_listing()
end
function update_auction_listing(listing, records, reference)
local rows = T
local rows = T.acquire()
if selected_item then
local historical_value = history.value(selected_item.key)
local stack_size = stack_size_slider:GetValue()
for _, record in pairs(records[selected_item.key] or empty) do
for _, record in pairs(records[selected_item.key] or T.empty) do
local price_color = undercut(record, stack_size_slider:GetValue(), listing == 'bid') < reference and color.red
local price = record.unit_price * (listing == 'bid' and record.stack_size / stack_size_slider:GetValue() or 1)
tinsert(rows, O(
'cols', A(
O('value', record.own and color.green(record.count) or record.count),
O('value', al.time_left(record.duration)),
O('value', record.stack_size == stack_size and color.green(record.stack_size) or record.stack_size),
O('value', money.to_string(price, true, nil, price_color)),
O('value', historical_value and al.percentage_historical(round(price / historical_value * 100)) or '---')
tinsert(rows, T.map(
'cols', T.list(
T.map('value', record.own and color.green(record.count) or record.count),
T.map('value', al.time_left(record.duration)),
T.map('value', record.stack_size == stack_size and color.green(record.stack_size) or record.stack_size),
T.map('value', money.to_string(price, true, nil, price_color)),
T.map('value', historical_value and al.percentage_historical(round(price / historical_value * 100)) or '---')
),
'record', record
))
end
if historical_value then
tinsert(rows, O(
'cols', A(
O('value', '---'),
O('value', '---'),
O('value', '---'),
O('value', money.to_string(historical_value, true, nil, color.green)),
O('value', historical_value and al.percentage_historical(100) or '---')
tinsert(rows, T.map(
'cols', T.list(
T.map('value', '---'),
T.map('value', '---'),
T.map('value', '---'),
T.map('value', money.to_string(historical_value, true, nil, color.green)),
T.map('value', historical_value and al.percentage_historical(100) or '---')
),
'record', O('historical_value', true, 'stack_size', stack_size, 'unit_price', historical_value, 'own', true)
'record', T.map('historical_value', true, 'stack_size', stack_size, 'unit_price', historical_value, 'own', true)
))
end
sort(rows, function(a, b)
@@ -175,8 +167,8 @@ function update_auction_listing(listing, records, reference)
end
function update_auction_listings()
update_auction_listing('bid', bid_records, unit_start_price)
update_auction_listing('buyout', buyout_records, unit_buyout_price)
update_auction_listing('bid', bid_records, get_unit_start_price())
update_auction_listing('buyout', buyout_records, get_unit_buyout_price())
end
function M.select_item(item_key)
@@ -191,23 +183,23 @@ end
function price_update()
if selected_item then
local historical_value = history.value(selected_item.key)
if bid_selection or buyout_selection then
unit_start_price = undercut(bid_selection or buyout_selection, stack_size_slider:GetValue(), bid_selection)
unit_start_price_input:SetText(money.to_string(unit_start_price, true, nil, nil, true))
if get_bid_selection() or get_buyout_selection() then
set_unit_start_price(undercut(get_bid_selection() or get_buyout_selection(), stack_size_slider:GetValue(), get_bid_selection()))
unit_start_price_input:SetText(money.to_string(get_unit_start_price(), true, nil, nil, true))
end
if buyout_selection then
unit_buyout_price = undercut(buyout_selection, stack_size_slider:GetValue())
unit_buyout_price_input:SetText(money.to_string(unit_buyout_price, true, nil, nil, true))
if get_buyout_selection() then
set_unit_buyout_price(undercut(get_buyout_selection(), stack_size_slider:GetValue()))
unit_buyout_price_input:SetText(money.to_string(get_unit_buyout_price(), true, nil, nil, true))
end
start_price_percentage:SetText(historical_value and al.percentage_historical(round(unit_start_price / historical_value * 100)) or '---')
buyout_price_percentage:SetText(historical_value and al.percentage_historical(round(unit_buyout_price / historical_value * 100)) or '---')
start_price_percentage:SetText(historical_value and al.percentage_historical(round(get_unit_start_price() / historical_value * 100)) or '---')
buyout_price_percentage:SetText(historical_value and al.percentage_historical(round(get_unit_buyout_price() / historical_value * 100)) or '---')
end
end
function post_auctions()
if selected_item then
local unit_start_price = unit_start_price
local unit_buyout_price = unit_buyout_price
local unit_start_price = get_unit_start_price()
local unit_buyout_price = get_unit_buyout_price()
local stack_size = stack_size_slider:GetValue()
local stack_count
stack_count = stack_count_slider:GetValue()
@@ -258,11 +250,11 @@ function validate_parameters()
post_button:Disable()
return
end
if unit_buyout_price > 0 and unit_start_price > unit_buyout_price then
if get_unit_buyout_price() > 0 and get_unit_start_price() > get_unit_buyout_price() then
post_button:Disable()
return
end
if unit_start_price == 0 then
if get_unit_start_price() == 0 then
post_button:Disable()
return
end
@@ -345,15 +337,15 @@ function quantity_update(maximize_count)
end
function unit_vendor_price(item_key)
for slot in info.inventory do
temp(slot)
local item_info = temp-info.container_item(unpack(slot))
for slot in info.inventory() do
T.temp(slot)
local item_info = T.temp-info.container_item(unpack(slot))
if item_info and item_info.item_key == item_key then
if info.auctionable(item_info.tooltip, nil, true) and not item_info.lootable then
ClearCursor()
PickupContainerItem(unpack(slot))
ClickAuctionSellItemButton()
local auction_sell_item = temp-info.auction_sell_item()
local auction_sell_item = T.temp-info.auction_sell_item()
ClearCursor()
ClickAuctionSellItemButton()
ClearCursor()
@@ -411,20 +403,20 @@ function update_item(item)
end
function update_inventory_records()
local auctionable_map = temp-T
for slot in info.inventory do
temp(slot)
local item_info = temp-info.container_item(unpack(slot))
local auctionable_map = T.temp-T.acquire()
for slot in info.inventory() do
T.temp(slot)
local item_info = T.temp-info.container_item(unpack(slot))
if item_info then
local charge_class = item_info.charges or 0
if info.auctionable(item_info.tooltip, nil, true) and not item_info.lootable then
if not auctionable_map[item_info.item_key] then
local availability = T
local availability = T.acquire()
for i = 0, 10 do
availability[i] = 0
end
availability[charge_class] = item_info.count
auctionable_map[item_info.item_key] = O(
auctionable_map[item_info.item_key] = T.map(
'item_id', item_info.item_id,
'suffix_id', item_info.suffix_id,
'key', item_info.item_key,
@@ -445,7 +437,7 @@ function update_inventory_records()
end
end
end
release(inventory_records)
T.release(inventory_records)
inventory_records = values(auctionable_map)
refresh = true
end
@@ -453,7 +445,8 @@ end
function refresh_entries()
if selected_item then
local item_key = selected_item.key
bid_selection, buyout_selection = nil, nil
set_bid_selection()
set_buyout_selection()
bid_records[item_key], buyout_records[item_key] = nil, nil
local query = scan_util.item_query(selected_item.item_id)
status_bar:update_status(0, 0)
@@ -462,7 +455,7 @@ function refresh_entries()
scan_id = scan.start{
type = 'list',
ignore_owner = true,
queries = A(query),
queries = T.list(query),
on_page_loaded = function(page, total_pages)
status_bar:update_status(page / total_pages, 0) -- TODO
status_bar:set_text(format('Scanning Page %d / %d', page, total_pages))
@@ -485,8 +478,8 @@ function refresh_entries()
status_bar:set_text('Scan aborted')
end,
on_complete = function()
bid_records[item_key] = bid_records[item_key] or T
buyout_records[item_key] = buyout_records[item_key] or T
bid_records[item_key] = bid_records[item_key] or T.acquire()
buyout_records[item_key] = buyout_records[item_key] or T.acquire()
refresh = true
status_bar:update_status(1, 1)
status_bar:set_text('Scan complete')
@@ -496,31 +489,31 @@ function refresh_entries()
end
function record_auction(key, aux_quantity, unit_blizzard_bid, unit_buyout_price, duration, owner)
bid_records[key] = bid_records[key] or T
bid_records[key] = bid_records[key] or T.acquire()
do
local entry
for _, record in pairs(bid_records[key]) do
if unit_blizzard_bid == record.unit_price and aux_quantity == record.stack_size and duration == record.duration and cache.is_player(owner) == record.own then
if unit_blizzard_bid == record.unit_price and aux_quantity == record.stack_size and duration == record.duration and info.is_player(owner) == record.own then
entry = record
end
end
if not entry then
entry = O('stack_size', aux_quantity, 'unit_price', unit_blizzard_bid, 'duration', duration, 'own', cache.is_player(owner), 'count', 0)
entry = T.map('stack_size', aux_quantity, 'unit_price', unit_blizzard_bid, 'duration', duration, 'own', info.is_player(owner), 'count', 0)
tinsert(bid_records[key], entry)
end
entry.count = entry.count + 1
end
buyout_records[key] = buyout_records[key] or T
buyout_records[key] = buyout_records[key] or T.acquire()
if unit_buyout_price == 0 then return end
do
local entry
for _, record in pairs(buyout_records[key]) do
if unit_buyout_price == record.unit_price and aux_quantity == record.stack_size and duration == record.duration and cache.is_player(owner) == record.own then
if unit_buyout_price == record.unit_price and aux_quantity == record.stack_size and duration == record.duration and info.is_player(owner) == record.own then
entry = record
end
end
if not entry then
entry = O('stack_size', aux_quantity, 'unit_price', unit_buyout_price, 'duration', duration, 'own', cache.is_player(owner), 'count', 0)
entry = T.map('stack_size', aux_quantity, 'unit_price', unit_buyout_price, 'duration', duration, 'own', info.is_player(owner), 'count', 0)
tinsert(buyout_records[key], entry)
end
entry.count = entry.count + 1