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 ➜ Lua ➜ JScript -> Lua

JScript -> Lua

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


Posted by Mannoman   (7 posts)  Bio
Date Fri 01 Sep 2006 11:39 PM (UTC)

Amended on Sat 02 Sep 2006 12:29 AM (UTC) by Nick Gammon

Message
Kk ... so here's a task for one of you diehard Lua people out there :-D. I made a random ooc (global on this specific mud) alias that takes the list of aliases, selects all of them under the "global" group, puts them in an array, then randomly chooses which to spit out. It also replaces the %1 in the "send" field of the array to the text from "ooc *". I have had some people ask me if they can send me the alias, but they have scripts in Lua, and don't want to switch to JScript just for a nifty little alias. So ... my question is, can anyone convert this into Lua for me?


aliaslist = new VBArray(world.GetAliasList()).toArray();
nums = new Array(100); // Gets list of aliases
var k = 0 ;
for (i=0;i<aliaslist.length;i++) {
   if( world.GetAliasOption(aliaslist[i], "group") == "global") { // if alias is grouped "global"
       nums[k] = world.GetAliasOption(aliaslist[i], "send") ; // and the send field to nums alias
       k++; increases spot that next alias will be placed in, and the max for the random number generator
   }
}
var generator = Math.random()*(k); // selects random number that is in use
generator = Math.ceil(generator)-1; // fixes numbers so they range from 0 - nums.length
text = new String(nums[generator]) ;
var deleteafter = text.indexOf("%") ;
text1 = new String(text.substring(0, deleteafter)) ;
text2 = new String(text.substring(deleteafter+2, text.length)) ;
text = text1+"%1"+text2 // removes the %1 from the Send field, and replaces it with the %1 captured by the alias
world.Send(text); // sends text via random alias


Edited for formatting
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 02 Sep 2006 12:41 AM (UTC)
Message
This should do it:


<aliases>
  <alias
   match="ooc *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>nums = {}  -- aliases in the group 'global'

aliaslist = GetAliasList()
if aliaslist then
  for k, v in ipairs (aliaslist) do 
    if GetAliasOption (v, "group") == "global" then
      table.insert (nums, GetAliasOption (v, "send")) 
    end -- if in global group
  end  -- for
end -- if we have any aliases

n = table.getn (nums)  -- how many we found

if n &gt; 0 then
  text = nums [math.random (1, n)]  -- pick one randomly
  text = string.gsub (text, "%%%%1", "%1")  -- substitute target
  Send (text)
end -- if found one</send>
  </alias>
</aliases>


- Nick Gammon

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

Posted by Oldie   (1 post)  Bio
Date Reply #2 on Sat 02 Sep 2006 01:02 AM (UTC)
Message
I tried to use that code and after first realizing I needed to enable scripting, I got this error message.

Invalid character
Line in error:
nums = {}

I tried to mess around with it with what little knowledge I have of Lua, but I still could not get it to run.

I assume you enter the number of aliases there, but I have tested it and it does not work either way.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 02 Sep 2006 02:25 AM (UTC)

Amended on Sat 02 Sep 2006 02:27 AM (UTC) by Nick Gammon

Message
That line just defines an empty table. You don't need to change anything. Make sure scripting is enabled and set to Lua.

[EDIT] A quick test shows that error message will appear if you have the scripting language set to VBscript, change it to Lua and you should be OK.

Did you just copy and paste it into the aliases list? The technique is described here:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4777

Although that shows a trigger, doing an alias is the same idea.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 02 Sep 2006 07:47 AM (UTC)
Message
Quote:

... they have scripts in Lua, and don't want to switch to JScript just for a nifty little alias ...


It would be a quick job to turn it into a plugin, plugins can be in a different language for each one.

You could use GetPluginAliasList (with "" as the plugin ID) to find the global aliases, even though you are in a plugin.

http://www.gammon.com.au/scripts/doc.php?function=GetPluginAliasList

Then use GetPluginAliasInfo to pull in the information for the alias.

http://www.gammon.com.au/scripts/doc.php?function=GetPluginAliasInfo

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


21,262 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.