Re: How to capture and store data (and view it via an alias)?
Posted: Thu Aug 16, 2018 12:22 pm
by Jor'Mox
I modified the two functions in the script (and added a constant table at the top, so don't skip that) that should sort dates properly and ignore auctions being added that match price and item with an existing entry.
auction_data = auction_data or {}
local months = {Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8,
Sep = 9, Oct = 10, Nov = 11, Dec = 12}
function add_auction_data(item, price, buyer)
local date = os.date("%b %d, %Y")
price = tonumber(price)
local match
for k,v in ipairs(auction_data) do
if item == v.item and price == v.price then
match = true
break
end
end
if not match then
table.insert(auction_data,{item = item, price = price, buyer = buyer, date = date})
table.save(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end
end
function show_auction_data(sort_by)
local sort_func
sort_by = sort_by or "default"
local sort_orders = {
default = {'item','price','date'},
date = {'date','item','price'},
price = {'price','item','date'},
buyer = {'buyer','item','date'},
}
local order = sort_orders[sort_by] or sort_orders.default
local sort_date = function (a,b)
a,b = a.date, b.date
local montha, daya, yeara = string.match(a,"(%a+) (%d+), (%d+)")
local monthb, dayb, yearb = string.match(b,"(%a+) (%d+), (%d+)")
daya, yeara, dayb, yearb = tonumber(daya), tonumber(yeara), tonumber(dayb), tonumber(yearb)
montha, monthb = months[montha], months[monthb]
if yeara ~= yearb then return yeara < yearb end
if montha ~= monthb then return montha < monthb end
return daya < dayb
end
sort_func = function (a,b)
if a[ order[1] ] ~= b[ order[1] ] then
if order[1] == "date" then return sort_date(a,b) end
return a[ order[1] ] < b[ order[1] ]
end
if a[ order[2] ] ~= b[ order[2] ] then
if order[2] == "date" then return sort_date(a,b) end
return a[ order[2] ] < b[ order[2] ]
end
if a[ order[3] ] ~= b[ order[3] ] then
if order[3] == "date" then return sort_date(a,b) end
return a[ order[3] ] < b[ order[3] ]
end
return false
end
table.sort(auction_data,sort_func)
print(string.format("%-50s: %5s, %12s, %s", "ITEM", "PRICE", "DATE", "BUYER"))
for k,v in ipairs(auction_data) do
print(string.format("%-50s: %5s, %12s, %s",v.item,v.price,v.date,v.buyer))
end
end
if io.exists(getMudletHomeDir() .. "/auction_data.lua") and table.is_empty(auction_data) then
table.load(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end