JScript -> Lua

Posted by Mannoman on Fri 01 Sep 2006 11:39 PM — 5 posts, 22,413 views.

#0
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
Amended on Sat 02 Sep 2006 12:29 AM by Nick Gammon
Australia Forum Administrator #1
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>

#2
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.
Australia Forum Administrator #3
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.
Amended on Sat 02 Sep 2006 02:27 AM by Nick Gammon
Australia Forum Administrator #4
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