Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Trouble with a script, need some help

Trouble with a script, need some help

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Bakarus   (17 posts)  Bio
Date Thu 18 Oct 2012 04:24 AM (UTC)

Amended on Thu 18 Oct 2012 05:04 AM (UTC) by Bakarus

Message
I'm trying to create a script that catches 2 lines from the world and saves them to a table.

the lines i'm trying to catch are these:


The Joker says to Tom, 'Perhaps this number will bring you luck!'

He blows a cloud of smoke which slowly forms the number 20.
Then, with a wink and a smile, he is gone.


I need to capture the players name, in this case "Tom", and the number he got, in this case "20"

I will do this with 5 players, each going in succession, after which I would like to print back to the world the table containing the players names and their number, sorted by the number.

Here is what I have so far, be gentle I am new to lua in general.


require "wait"

wait.make(function () -- coroutine starts here

-- wait for randoms to start
local x = wait.match("The Joker says to", 10)
local players = {}
local num = 1

if not x then
	ColourNote("white", "blue", "No randoms withing 10 seconds")
	return
end -- if

-- loop until last player randoms
while num < 5 do
	for i=1, 5, 1, do
	local line, wildcards, styles = wait.match("He blows a cloud of smoke which slowly forms the number *.")
	players.name = %1
	players.num = %2
	end
	num = num + 1
end

end) -- end of coroutine


Its bad, and doesn't work obviously, any help would be greatly appreciated.

I realize this doesn't print anything back at this stage, thanks again.

A sample printout would be like:

You say, "Bob 20"
You say, "Frank 17"
You say, "Tom 10"
etc.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 19 Oct 2012 05:32 AM (UTC)
Message
Your first problem is here:


-- wait for randoms to start
local x = wait.match("The Joker says to", 10)


A match matches exactly that. Since there is more on the line you probably want:


-- wait for randoms to start
local x = wait.regexp("^The Joker says to ", 10)



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Bakarus   (17 posts)  Bio
Date Reply #2 on Fri 19 Oct 2012 08:11 PM (UTC)
Message
How would I go about writing out the table to the world, such as


You say, "Bob 89"
You say, "Neil 55"
You say, "Frank 44"

and order them by the Number they rolled like above.

Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 19 Oct 2012 11:21 PM (UTC)
Message
You need to rework the script a bit first. The name changes for each one, right?

So it's no good capturing only one name like this:


local x = wait.match("The Joker says to", 10)


You need to capture the name, and the roll, 5 times.

Get that part going and then we can work on sorting them.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Bakarus   (17 posts)  Bio
Date Reply #4 on Sun 21 Oct 2012 04:39 AM (UTC)
Message
That's the part I'm having trouble with, capturing the name and roll 5 times and storing them,

The idea is the person types the alias, rvr, w/o and argument it waits for 5 people to 'random', with an argument it waits for that many people to 'random', like rvr 15, would wait and capture 15 people.

I've tried this, but the 2nd local line has a problem.


-- loop until last player randoms
while num < 5 do
	for i=1, 5, 1 do
        local line, wildcards, styles = wait.match("The Joker says to  *")
        players.name = %1
	local line, wildcards, styles = wait.match("He blows a cloud of smoke which slowly forms the number *.")
	players.num = %1
	end
	num = num + 1
end


I'm unsure of how to capture both lines one after the other here.

And yes the name changes each time, along with the number rolled. I'll keep reading and trying things, but really clueless at this point.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Sun 21 Oct 2012 06:02 AM (UTC)
Message
Can you show the whole alias please?

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 21 Oct 2012 06:03 AM (UTC)

Amended on Sun 21 Oct 2012 06:04 AM (UTC) by Nick Gammon

Message
This part is wrong, for sure:


    players.name = %1


More like:


    local line, wildcards, styles = wait.match("The Joker says to  *")
    players.name = wildcards [1]

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Bakarus   (17 posts)  Bio
Date Reply #7 on Sun 21 Oct 2012 06:53 AM (UTC)

Amended on Sun 21 Oct 2012 07:19 PM (UTC) by Bakarus

Message
Here's the alias thus far,..

<aliases>
  <alias
   match="rvr"
   enabled="y"
   group=""
   send_to="12"
   sequence="100"
  >
  <send>require "wait"

wait.make(function () -- coroutine starts here
local players = {}
-- wait for randoms to start
local x = wait.regex("^The Joker says to  *", 10)

if not x then
    ColourNote("white", "blue", "No randoms withing 10 seconds")
    return
end -- if

-- loop until last player randoms
while num > 5 do
    for i=1, 5, 1 do
        local line, wildcards, styles = wait.match("The Joker says to  *")
        players.name = wildcards[1]
   local line, wildcards, styles = wait.match("He blows a cloud of smoke which slowly forms the number *.")
    players.num = wildcards[1]
    end
    num = num + 1
end

end) -- end of coroutine</send>
  </alias>
</aliases>

Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #8 on Sun 21 Oct 2012 11:07 PM (UTC)

Amended on Sun 21 Oct 2012 11:08 PM (UTC) by Nick Gammon

Message
Hmm. You had a loop of 5 within a loop of 5 there. Let's keep it simple ...


<aliases>
  <alias
   match="rvr"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "wait"

wait.make(function () -- coroutine starts here

  local players = {}
  local line, wildcards, styles

  -- do 5 players

    for i= 1, 5 do
        line, wildcards = wait.regexp ("^The Joker says to (.+), ")
        players [i] = { name = wildcards [1] }

        line, wildcards = wait.match ("He blows a cloud of smoke which slowly forms the number *.")
        players [i].num = tonumber (wildcards [1])

        print ("Roll", i, "got player", players [i].name, "roll was", players [i].num)
    end

    -- sort into descending score order
    table.sort (players, function (a, b) return a.num &gt; b.num end )

    for k, v in ipairs (players) do
      Send ("say ", v.name, " ", v.num)
    end -- for
    

end) -- end of coroutine</send>
  </alias>
</aliases>



Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


Test data:



The Joker says to Tom, 'Perhaps this number will bring you luck!'
He blows a cloud of smoke which slowly forms the number 20.
Then, with a wink and a smile, he is gone.

The Joker says to Fred, 'Perhaps this number will bring you luck!'
He blows a cloud of smoke which slowly forms the number 10.
Then, with a wink and a smile, he is gone.

The Joker says to Jill, 'Perhaps this number will bring you luck!'
He blows a cloud of smoke which slowly forms the number 30.
Then, with a wink and a smile, he is gone.

The Joker says to Nadine, 'Perhaps this number will bring you luck!'
He blows a cloud of smoke which slowly forms the number 50.
Then, with a wink and a smile, he is gone.

The Joker says to Nick, 'Perhaps this number will bring you luck!'
He blows a cloud of smoke which slowly forms the number 5.
Then, with a wink and a smile, he is gone.


Output:


Roll 1 got player Tom roll was 20
...
Roll 2 got player Fred roll was 10
...
Roll 3 got player Jill roll was 30
...
Roll 4 got player Nadine roll was 50
...
Roll 5 got player Nick roll was 5

say Nadine 50
say Jill 30
say Tom 20
say Fred 10
say Nick 5

You say 'Nadine 50'
You say 'Jill 30'
You say 'Tom 20'
You say 'Fred 10'
You say 'Nick 5'

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Bakarus   (17 posts)  Bio
Date Reply #9 on Mon 22 Oct 2012 06:15 AM (UTC)

Amended on Mon 22 Oct 2012 06:31 AM (UTC) by Bakarus

Message
I get a small error using your alias,


Run-time error
World: test
Immediate execution
C:\Program Files\MUSHclient\lua\wait.lua:159: Timers not enabled
stack traceback:
        [C]: in function 'assert'
        C:\Program Files\MUSHclient\lua\wait.lua:159: in function 'make'
        [string "Alias: "]:3: in main chunk
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #10 on Mon 22 Oct 2012 06:33 AM (UTC)

Amended on Mon 22 Oct 2012 06:34 AM (UTC) by Nick Gammon

Message
Quote:
Timers not enabled


Better enable timers then, huh?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


24,672 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.