How to capture and store data (and view it via an alias)?

Jor'Mox
Posts: 1089
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox » Thu Aug 16, 2018 12:22 pm

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.
Code: [show] | [select all] lua
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

Vooku
Posts: 70
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku » Thu Aug 16, 2018 12:51 pm

Wow, thanks. I'll give it a try!

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: How to capture and store data (and view it via an alias)?

Post by Nyyrazzilyss » Thu Aug 16, 2018 3:31 pm

Something I usually suggest printing out can be found at http://regexlib.com/CheatSheet.aspx

Vooku
Posts: 70
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku » Fri Aug 17, 2018 12:25 am

Thanks Nyyr!

Jor, question... I tried manually removing a bunch of duplicates but it when I print in game there is only 1 item on the list. Does it have to do with this line? I'm guessing there is still a reference to those items in this 'return' line and that is messing it up.
Code: [show] | [select all] lua
return {{[1] = {2},[2] = {3},[3] = {4},[4] = {5},[5] = {6},[6] = {7},[7] = {8},[8] = {9},[9] = {10},[10] = {11},[11] = {12},[12] = {13},[13] = {14},[14] = {15},[15] = {16},[16] = {17},[17] = {18},[18] = {19},[19] = {20},[20]

Jor'Mox
Posts: 1089
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox » Fri Aug 17, 2018 11:41 am

Yup. You can of course technically edit within the text file, but I usually find it simpler to load in the table, edit it within Mudlet, and then save it, since you don't have any such complications that way.

Vooku
Posts: 70
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku » Fri Aug 17, 2018 12:21 pm

How do I do it within Mudlet?

Jor'Mox
Posts: 1089
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox » Fri Aug 17, 2018 12:28 pm

So, assuming you have an unmessed-up file to load the table from, you can use table.load and table.save exactly how you see it in the script. Then you have two options. First, you can just locate duplicates manually, identify what spot they are at, and use table.remove to cut them out. Or, you could run a pair of loops that go through the table, comparing entries to find ones that don't have the same index but otherwise match, and cut them out that way.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests