Page 1 of 2

Count Money

Posted: Tue Sep 10, 2019 11:56 pm
by str3ss
While I know it's possible, I'm at a loss as to how approach the following problem. In this MUD you can count coins in <container> and it'll return "This is a pile of various types of coins, including ten copper shindigs, one gold boondoggle, one silver fart, nine copper troubles, and four silver cantaloupes."

Thanks to the help files, we know that shindigs are worth 1 each, boondoggles are worth 625 each, farts are worth 125 each, troubles are worth 5 each, and cantaloupes are worth 25 each.

How would we create a script that did the math and echo'd the actual value back to you?

Re: Count Money

Posted: Wed Sep 11, 2019 1:47 am
by demonnic
Hi!

So, I'm using code from viewtopic.php?t=22617 to do the number word to number conversion in the attached package. If you import it, the supplied example line comes out looking like this:
unknown.png
unknown.png (6.3 KiB) Viewed 177 times

To summarize what I did:

I used the first part of the example line, "This is a pile of various types of coins, including" as a begin of line substring trigger. This trigger has the following in the code block, to reset the value to 0 since we're about to add it all up:
Code: [show] | [select all] lua
value = 0
I then made child triggers for each type of currency as a perl regex trigger, the example pattern for shindigs being: "(.*) copper shindigs"
The code for the shindigs looks like this:
Code: [show] | [select all] lua
local coins = convert_number_words(matches[2])
value = value + coins * coin_value.shindigs
Finally, beneath the perl regex triggers for the currency types, I made a substring trigger matching a single period: "."
With the code block:
Code: [show] | [select all] lua
-- using a . as the substring pattern as the provided example only had one period, at the end of the line. 
-- you could also use a prompt trigger here to echo it at the next prompt, or 'silver cantaloupes' if it's 
-- always guaranteed to have the cantaloupes listed and at the end. This also closes the coin counter
cecho("My coins are worth: <green>$" .. value .. "\n")
setTriggerStayOpen('Coin counter', 0)
And the coin_value definition is in a script, and looks like this:
Code: [show] | [select all] lua
coin_value = {}
coin_value.shindigs = 1
coin_value.boondoggle = 625
coin_value.fart = 125
coin_value.troubles = 5
coin_value.cantaloupes = 25

Re: Count Money

Posted: Wed Sep 11, 2019 9:12 pm
by str3ss
Nice! Works really well. I think it might have a problem with hyphenated money (I forgot to mention things like fourty-seven), but it should be easy enough to modify if so. Thank you!

Re: Count Money

Posted: Wed Sep 11, 2019 9:45 pm
by demonnic
Yeah, I don't believe the code I took from the other forum post used hyphenates, but that's where I would look to make your edits. If I get time later tonight I might try to tackle it.

ETA: if you capture the value as a hyphenate, say "forty-seven", you can use the following to change it to "forty seven"
Code: [show] | [select all] lua
numberToChange = "forty-seven"
newNumber = string.gsub(numberToChange, '-', ' ')

Re: Count Money

Posted: Thu Sep 12, 2019 3:32 pm
by bruary
It looks like the calculation in the example is incorrect, but I'm not sure why. It came up as $9115 when it should be $895. Any suggestions?

Re: Count Money

Posted: Thu Sep 12, 2019 5:48 pm
by demonnic
hmm, might help to use parantheses to ensure order of operations. I used "value = value + coins * coin_value.shindigs" and it might be more accurate to use "value = value + (coins * coin_value.shindigs)"

Will take a look later to see if that's it.

Re: Count Money

Posted: Thu Sep 12, 2019 6:48 pm
by demonnic
My triggers were off, I have attached a better, more accurate version that comes up with 905 (did you forget the 10 copper shindigs to get 895 maybe?) and matches my math. Thanks for catching that.

Re: Count Money

Posted: Thu Sep 12, 2019 9:50 pm
by bruary
Almost perfect! I tested with various combinations using feedTriggers() with no problems.
There's one issue when the text comes from the mud though. The mud will show something like:
"This is a pile of various types of coins, including ten copper shindigs, one gold
boondoggle, one silver fart, nine copper troubles, and four silver cantaloupes."

Everything will be counted except the boondoggle, since it's split across a line break. Since you can hold many combinations of coins, it might break after a quantity, metal, or type of coin.

Looking around for similar problems people have had, maybe this is fixed with some sort of stayOpen?

Re: Count Money

Posted: Fri Sep 13, 2019 5:28 am
by Vadi
This is easiest and best fixed by asking the MUD not to wrap the lines for you, and getting Mudlet to wrap them instead - this way your triggers get 1 easy line to work with. Can you turn line wrapping off in your game?

Re: Count Money

Posted: Fri Sep 13, 2019 3:30 pm
by bruary
I asked, but there isn't a way to turn off the mud's wrap.