Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Message
| This is easiest done with a coroutine, which lets you loop, sending things and checking the response.
<aliases>
<alias
match="finditem *"
enabled="y"
send_to="12"
sequence="100"
>
<send>
require "wait"
-- backslashes have to be entered as four backslashes (sigh)
re = "(Condition: excellent ~((?P<percent>~d+)%%~)|I don't sell that)"
re = string.gsub (re, '~', '\\\\')
wait.make (function ()
-- try 100 items
for i = 1, 100 do
-- inspect it
Send ("inspect " .. i .. ".%1")
-- wait for a response or not found, within 10 seconds
line, wildcards = wait.regexp (re, 10)
if not line then
ColourNote ("orange", "", "Timeout finding item.")
return
end -- if
-- if not found, give up
if string.match (line, "I don't sell that") then
ColourNote ("orange", "", "Item not found.")
return -- done
end -- if not found
-- found, check percentage
if wildcards.percent == "100" then
ColourNote ("orange", "", "Item found! - " .. i .. ".%1")
return
end -- found one
end -- for
end)
</send>
</alias>
</aliases>
 |
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
|
The important part of the above is the script:
require "wait"
-- backslashes have to be entered as four backslashes (sigh)
re = "(Condition: excellent ~((?P<percent>~d+)%%~)|I don't sell that)"
re = string.gsub (re, '~', '\\\\')
wait.make (function ()
-- try 100 items
for i = 1, 100 do
-- inspect it
Send ("inspect " .. i .. ".%1")
-- wait for a response or not found, within 10 seconds
line, wildcards = wait.regexp (re, 10)
if not line then
ColourNote ("orange", "", "Timeout finding item.")
return
end -- if
-- if not found, give up
if string.match (line, "I don't sell that") then
ColourNote ("orange", "", "Item not found.")
return -- done
end -- if not found
-- found, check percentage
if wildcards.percent == "100" then
ColourNote ("orange", "", "Item found! - " .. i .. ".%1")
return
end -- found one
end -- for
end)
What that does is loop around, trying items 1 to 100, sending "inspect x.item" where x is the current number.
It then does a wait.match with a timeout, looking for both the line "Condition" or "I don't sell that".
Depending on what it gets, it tries again, or exits.
More explanation here: http://www.gammon.com.au/forum/?id=4957 |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|