Alias and table

Posted by Monkeysushi on Sun 07 Oct 2018 06:21 PM — 4 posts, 15,115 views.

#0
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
Australia Forum Administrator #1
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.
Amended on Sun 07 Oct 2018 08:24 PM by Nick Gammon
#2
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?
Australia Forum Administrator #3
Yes, that's right.