Trouble with a script, need some help

Posted by Bakarus on Thu 18 Oct 2012 04:24 AM — 11 posts, 36,092 views.

#0
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.
Amended on Thu 18 Oct 2012 05:04 AM by Bakarus
Australia Forum Administrator #1
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)


#2
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.

Australia Forum Administrator #3
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.
#4
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.
Australia Forum Administrator #5
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.
Australia Forum Administrator #6
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]
Amended on Sun 21 Oct 2012 06:04 AM by Nick Gammon
#7
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>

Amended on Sun 21 Oct 2012 07:19 PM by Bakarus
Australia Forum Administrator #8
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'
Amended on Sun 21 Oct 2012 11:08 PM by Nick Gammon
#9
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
Amended on Mon 22 Oct 2012 06:31 AM by Bakarus
Australia Forum Administrator #10
Quote:
Timers not enabled


Better enable timers then, huh?