[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Name Trigger

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Name Trigger

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Pages: 1  2 

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #15 on Thu 03 Feb 2011 12:28 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Skasi   (3 posts)  [Biography] bio
Date Reply #16 on Sat 05 Feb 2011 11:42 PM (UTC)  quote  ]

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())(.*?)$"
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #17 on Sun 06 Feb 2011 12:03 AM (UTC)  quote  ]

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
[Go to top] 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.


4,081 views.

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

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]