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

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

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

Post by Vooku »

Thanks for the help. I'll mess with it some more and see if I can get it working.

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

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

Post by Vooku »

I still can't get it working...

Here is how I have it set up. I have the code for the two functions in my 'initCustom()' script which gets called when I first log on a character. If I make changes to initCustom I either have to log out and back in or type 'lua initCustom(). I've attached how I have it set up. Was working fine this way. No idea what happened. I can't even get it to capture the data. I'm not getting the cecho I have in the trigger.
Attachments
Alias.JPG
Trigger.JPG
initCustom.JPG

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

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

Post by Vooku »

Here is the error I get when something matches my trigger pattern...
Attachments
TriggerError.JPG

Jor'Mox
Posts: 1146
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 »

That bit of script is missing a critical piece, namely the declaration of the auction_data table. You took it out, and put it as the last line of your trigger. You need that line to be positioned just above the two functions in the script. Assuming it has been this way the whole time, other than when you first got things going, you likely created the auction_data table properly initially, but then when you moved things around, the declaration wasn't happening, so nothing was working the way it should have.

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

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

Post by Vooku »

Oh ok. I must have had it correct originally, and then changed it for some reason.

Back to working great!

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

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

Post by Vooku »

Now that it is working great again, how would I go about making another alias (like @aucdatadate) that would display the data in the same table by date then alphabetical item name?

Jor'Mox
Posts: 1146
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 »

To do that, I would first modify the two major functions that this uses to look like the following:
Code: [show] | [select all] lua
function add_auction_data(item, price, buyer)
    local date = os.date("%b %d, %Y")
    table.insert(auction_data,{item = item, price = price, buyer = buyer, date = date})
    table.save(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end

function show_auction_data(sort_by_date)
    local sort_func
    if sort_by_date then
        sort_func = function (a,b)
                if a.date ~= b.date then return a.date < b.date end
                if a.item ~= b.item then return a.item < b.item end
                if a.price ~= b.price then return a.price < b.price end
                return false
            end
    else
        sort_func = function (a,b)
                if a.item ~= b.item then return a.item < b.item end
                if a.price ~= b.price then return a.price < b.price end
                if a.date ~= b.date then return a.date < b.date end
                return false
            end
    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
You will notice that this pulls the sorting parts out of the add_auction_data function and moves them into the show_auction_data function, and then allows you to select between two different sorting functions. Now your first display alias should continue working as expected, and you can add another by simply having it call show_auction_data(true).

Alternatively, you could use a more generalizable version of the sorting code in the show_auction_data function like this:
Code: [show] | [select all] lua
function show_auction_data(sort_by_date)
    local sort_func
    local order = {'item','price','date'}
    if sort_by_date then order = {'date','item','price'} end
    sort_func = function (a,b)
            if a.[order[1]] ~= b.[order[1]] then return a.[order[1]] < b.[order[1]] end
            if a.[order[2]] ~= b.[order[2]] then return a.[order[2]] < b.[order[2]] end
            if a.[order[3]] ~= b.[order[3]] then 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
Doing it this way makes changing the sort order as simple as changing what is put into the order table, and would allow you to easily add some option to sort by buyer for example.

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

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

Post by Vooku »

Wow! The was a fast reply. So if I use the 2nd option I could type "@aucdata item" based on how I have my alias currently?

I'm getting this error when I copied it into Mudlet...

Lua syntax error:[string "Script: initCustom"]:85: " expected near '['

line 85 is...

if a.[order[1]] ~= b.[order[1]] then return a.[order[1]] < b.[order[1]] end
Last edited by Vooku on Thu Jul 26, 2018 8:28 pm, edited 1 time in total.

Jor'Mox
Posts: 1146
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 »

No, though we could set that up if you wanted it to work that way. As it is, both versions of the function actually produce exactly the same result, the second is just structured to make it easier to add more options later. If you wanted to be able to select several different sorting varieties, you just need to come up with what sorting preferences you want, and we can add them in, using the argument to select which to use, and adjusting your alias to allow for an argument as needed.

Jor'Mox
Posts: 1146
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 »

If you changed the function to this, then you could select from the table using a text argument, similar to what you mentioned (though modifying the alias still would have to happen obviously, and you would need to add in any additional sort options you wanted, above and beyond the default and "date").
Code: [show] | [select all] lua
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'},
    }
    local order = sort_orders[sort_by] or sort_orders.default
    
    sort_func = function (a,b)
            if a.[order[1]] ~= b.[order[1]] then return a.[order[1]] < b.[order[1]] end
            if a.[order[2]] ~= b.[order[2]] then return a.[order[2]] < b.[order[2]] end
            if a.[order[3]] ~= b.[order[3]] then 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
To make the alias work the way you want, I would change the pattern to this: ^@aucdata(?: \w+)?$
And its code to this: show_auction_data(matches[2])
Last edited by Jor'Mox on Thu Jul 26, 2018 8:36 pm, edited 1 time in total.

Post Reply