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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Alias and table

Alias and table

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


Posted by Monkeysushi   (2 posts)  Bio
Date Sun 07 Oct 2018 06:21 PM (UTC)
Message
I've got a bit of an idea of the logic but not the specifics for a few parts of what I want to do in Mush.

I want to make an alias that will add items to a list and check if it exists in a trigger. I don't know the best way to set up the variable with the alias or if an array and ArrayKeyExists is easier. Or something else.

For example,

alias addname * -- adds %1 to table or array friends

Trigger * tells you *

for _,v in pairs(friends) do
  if %1 == v then 
    colournote("white","black","From %1 - %2")
  else
    colournote("red","black","%1 tells you %2")
  end
end
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 07 Oct 2018 08:19 PM (UTC)

Amended on Sun 07 Oct 2018 08:24 PM (UTC) by Nick Gammon

Message
It's ColourNote not colournote.

Something like this should do it. Using a Lua table is easily the easiest.


<aliases>
  <alias
   match="^addname( \w+)?$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

require "serialize"  -- needed to serialize table to string

local name = Trim ("%1")
if name == "" then
  ColourNote ("orange", "", "Usage: addname (name)")
  return
end -- if no name

name = string.lower (name)  -- case-insensitive

-- get friends variable (de-serialize the client variable)
assert (loadstring (GetVariable ("friends") or "")) ()

friends = friends or {}  -- make sure table exists

-- check already in list
if friends [name] then
  ColourNote ("orange", "", "Name " .. name .. " is already in list of friends")
  return
end -- if already known

-- add to table
friends [name] = true  -- now in list

-- serialize the table so it will be saved with the world file
SetVariable ("friends", "friends = " .. serialize.save_simple (friends))

-- tell the user it worked
ColourNote ("orange", "", "Name " .. name .. " added to list of friends")

</send>
  </alias>
</aliases>



What this does is look for "addname" followed by a word (letters, numbers or underscore). You might need to amend that if names have spaces or other stuff in them.

Then it de-serializes the "friends" variable (which is saved along with the world file) and adds the name to it, if it isn't there already. Then it re-serializes the variables.

This way friends will persist across MUSHclient sessions.




Now for the trigger:


<triggers>
  <trigger
   enabled="y"
   match="* tells you *"
   omit_from_output="y"
   send_to="12"
   sequence="100"
  >
  <send>

local name = Trim ("%1")

name = string.lower (name)  -- case-insensitive

-- get friends variable
assert (loadstring (GetVariable ("friends") or "")) ()

friends = friends or {}  -- make sure table exists

if friends [name] then 
   ColourNote ("white", "", "From %1 - %2")
else
   ColourNote ("red", "", "%1 tells you %2")
end -- in table

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



Once again the variable is de-serialized into a table, and then it is compared to %1 in the trigger. By using string.lower the friend names are not case-sensitive. The trigger omits from output so you don't see the tell twice.

- Nick Gammon

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

Posted by Monkeysushi   (2 posts)  Bio
Date Reply #2 on Mon 08 Oct 2018 02:10 AM (UTC)
Message
Worked absolutely swimmingly and way less complicated than I was making it out to be. Thanks much.

I'm guessing it's as simple as changing friends [name] = false in a similar alias to remove from the list?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 09 Oct 2018 05:54 AM (UTC)
Message
Yes, that's right.

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


10,703 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.