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 ➜ General ➜ Name Trigger

Name Trigger

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


Pages: 1  2 

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #15 on Thu 03 Feb 2011 12:28 AM (UTC)
Message
This is one way of doing the colours:


<triggers>
  <trigger
   enabled="y"
   ignore_case="y"
   match="*funny looking clown*"
   omit_from_output="y"
   send_to="14"
   sequence="110"
  >
  <send>

-- show each style in its original colour

for i, style in ipairs (TriggerStyleRuns) do
  ColourTell (RGBColourToName (style.textcolour), RGBColourToName (style.backcolour), style.text)

  -- if person's description, append their name
  if style.text == "a funny looking clown" then
    ColourTell ("white", "", " (Tom)")
   end -- if
end

Note ""  -- start new line

</send>
  </trigger>
</triggers>


The appending of the name here depends on the description "a funny looking clown" being in its own style run (that is, all the same colour).

Skasi said:

Also is there a way to access the string between the asterisks?


You can do that if you make it a regular expression. Hit the "convert to regular expression" button, and then add brackets around the middle bit. This also lets you do a more general solution. eg.


<triggers>
  <trigger
   enabled="y"
   ignore_case="y"
   match="^(.*?)(a funny looking clown|a sleepy fellow|a pretty girl)(.*?)$"
   omit_from_output="y"
   regexp="y"
   send_to="14"
   sequence="110"
  >
  <send>

friends = {
 ["a funny looking clown"] = "Tom",
 ["a sleepy fellow"] = "John",
 ["a pretty girl"] = "Sarah",

-- more here

}

-- show each style in its original colour

for i, style in ipairs (TriggerStyleRuns) do
  ColourTell (RGBColourToName (style.textcolour), RGBColourToName (style.backcolour), style.text)

  -- if person's description, append their name
  if style.text == "%2" then
    ColourTell ("white", "", " (" ..
      (friends ["%2"] or "unknown") ..
       ")")
   end -- if
end

Note ""  -- start new line

</send>
  </trigger>
</triggers>


This keeps a table of friends, and then uses a regular expression to check for one of them.

You could improve this a bit by generating the matching names into a variable rather than having to repeat all the descriptions.
Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.




- Nick Gammon

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

Posted by Skasi   (3 posts)  Bio
Date Reply #16 on Sat 05 Feb 2011 11:42 PM (UTC)

Amended on Sat 05 Feb 2011 11:47 PM (UTC) by Skasi

Message
That's more or less exactly what I needed, thanks! :)

Is it possible to use variables as arrays? Is there a way to use scripts in 'match'? I would like to do something like the following:

function getNames()
 local names = GetVariable("names")
 for i in ipairs(names) do
  print(i)
  if i < #names then print("|") end
 end
end

match="^(.*?)(getNames())(.*?)$"
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #17 on Sun 06 Feb 2011 12:03 AM (UTC)

Amended on Sun 06 Feb 2011 12:05 AM (UTC) by Nick Gammon

Message
You can't directly put tables into variables. However it is easy enough with serialization:

http://www.gammon.com.au/forum/?id=4960

For example with this test data:



-- example table - in practice you would make an alias to add them

names = {
 ["a funny looking clown"] = "Tom",
 ["a sleepy fellow"] = "John",
 ["a pretty girl"] = "Sarah",

-- more here

}


Put this into the "on world save" callback:


require "serialize"

SetVariable ("saved_names", "names = " .. serialize.save_simple (names))


Now if you look at the (MUSHclient) variable "saved_names" (in the variables configuration dialog) you see this:


names = {
  ["a sleepy fellow"] = "John",
  ["a pretty girl"] = "Sarah",
  ["a funny looking clown"] = "Tom",
  }


This will now be saved as part of the world file, whenever you save that.

Next time (using the "world open" script callback) you put the names back like this:


assert (loadstring (GetVariable ("saved_names") or "")) ()


That converts the "string" table into an actual table.

So much for saving and loading.

Now to use in the trigger. Do the following whenever the list of names changes:


function getNames ()
 local t = {}
 for k, v in pairs (names) do
   table.insert (t, k)
 end
 SetVariable ("names_match", table.concat (t, "|"))
end


I use a second table here to convert the descriptions/names into a numerically-keyed table of just the descriptions. Then table.concat puts a "|" between each one.

Thus now names_match is:


a sleepy fellow|a pretty girl|a funny looking clown


Finally we can use the variable names_match in the trigger by putting it there with "expand variables" checked. Also note the use of the "!" which stops MUSHclient trying to put a backslash in front of the "|" characters ...


<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   ignore_case="y"
   match="^(.*?)(@!names_match)(.*?)$"
   omit_from_output="y"
   regexp="y"
   send_to="14"
   sequence="110"
  >
  <send>

-- show each style in its original colour

for i, style in ipairs (TriggerStyleRuns) do
  ColourTell (RGBColourToName (style.textcolour), RGBColourToName (style.backcolour), style.text)

  -- if person's description, append their name
  if style.text == "%2" then
    ColourTell ("white", "", " (" ..
      (names ["%2"] or "unknown") ..
       ")")
   end -- if
end

Note ""  -- start new line

</send>
  </trigger>
</triggers>

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


66,915 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

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.