PtokaX forum

Development Section => HOW-TO's => Topic started by: BottledHate on 11 July, 2004, 06:30:17

Title: get a random user name.... BCDC++
Post by: BottledHate on 11 July, 2004, 06:30:17
ok..  next on my list.. is to grab a random user name... once again in bcdc++... any lua pro out there want to tackle this one?  i can grab a random name from an array or table.... just need to get the userlist there first.
Title:
Post by: NotRabidWombat on 11 July, 2004, 09:17:45
Quick excert from startup.lua.
function createHub( hubid )
local hub = {}

hub._id = hubid
hub._users = {}
Not sure how well you can guarentee entropy on a linked list of connecting / disconnecting users.

-NotRabidWombat
Title:
Post by: BottledHate on 11 July, 2004, 20:27:56
i've messed with that a little bit, but i can't seem to get anything.  any other ideas?
Title:
Post by: NotRabidWombat on 11 July, 2004, 23:39:01
You should be able to traverse the table with:

for key, val in hub._users do
  ...
end

-NotRabidWombat
Title:
Post by: BottledHate on 12 July, 2004, 02:28:08
all i get back is nil on the hub._users...  it says it is a table value.. so i try to 'getn' which always comes back 0.   i'm a lua newb.. so i may just be goign about it totally wrong. :/ i'll keep trying.
Title:
Post by: NotRabidWombat on 12 July, 2004, 17:27:07
Show us some of your code.

-NotRabidWombat
Title:
Post by: Sedulus on 12 July, 2004, 18:02:37
Quotetable.getn (table)
Returns the size of a table, when seen as a list. If the table has an n field with a numeric value, this value is the size of the table. Otherwise, if there was a previous call to table.setn over this table, the respective value is returned. Otherwise, the size is one less the first integer index with a nil value.
i.e. that you're getting 0 is not odd..

function getRandomTableElem( tbl )
  -- count # of elements
  local n = 0
  table.foreach( tbl, function() n = n + 1 end )
  -- get random #
  n = math.random( n )
  for k,v in tbl do
    if n == 1 return k,v end
    n = n - 1
  end
end
that should work..
Title:
Post by: BottledHate on 13 July, 2004, 03:56:57
QuoteOriginally posted by NotRabidWombat
Show us some of your code.

..... here is what i have so far.  i havn't even added the grab random part yet.. still just trying to get a list of the users in a specific hub.  (BCDC++!!!!!)

thanks again for the help everyone!

--------------------------------------------------------------
lisT = {}
dcpp:setListener( "chat", "userlist",
   function( hub, user, text )
      if string.find( text, "rnduser" ) then
         for k,v in hub._users do --//get user names?
         local ret,c,n = string.find( k, "^%[.*%](.-)$" )
            if n ~= nil then
               table.insert( lisT, v )  --//make new list (table) of users?
            end
         end
         sstring = ""
         for key,value in lisT do --//table to string
            DC():PrintDebug(key.." "..value)
            sstring = sstring..key.." = "..value.."\r\n"
            --DC():PrintDebug(savestring)
         end
         if sstring ~= nil then
            DC():PrintDebug("bleh: "..sstring.." \"")
         end
      end
   end
)
DC():PrintDebug("***rnd2 loaded***")
---------------------------------------------------------------


**Edit: ok.. it was "tabbed" out.. wonder why the post just has it all left align??**
Title:
Post by: NotRabidWombat on 13 July, 2004, 05:34:18
for k,v in hub._users do

to

for k,v in dcpp:getHub( hub )._users do

Use the CODE tag if you want your text to remain tabbed properly.

"local ret,c,n = string.find( k, "^%[.*%](.-)$" )"
You only want users with tags?

Why do you copy everything to lisT?

-NotRabidWombat
Title:
Post by: BottledHate on 13 July, 2004, 07:05:39
error attempt to index a nil value.... after chaging:
for k,v in hub._users do  
to
for k,v in dcpp:getHub( hub )._users do

simplified.... i just want to get the damn list and know it exists!!!  ;) and finally it does!!! this works:
dcpp:setListener( "chat", "userlist",
function( hub, user, text )
      if string.find( text, "rnduser" ) then
         for k,v in hub._users do  
            if v ~= nil then
              DC():PrintDebug(k)
            end
         end
      end
   end
)
from here i can do whatever i need with the list...  i just need to get at it.. and learn the syntax beter i guess.. i've been scripting w/ lua for a whole week now ;)


also..  how can i create directories and new files via lua? all of the open and write functions seem to require the file allready being there. which doesn't help when u want to dynamically create dirs and txt files based on nick and hub... ie... /data/hub/nick.txt....
Title:
Post by: NotRabidWombat on 13 July, 2004, 07:27:14
Duh! I'm retarded. Sorry about that.

Create a file is easy. Just open a file for writing and write to it. :-)

Create is also easy.
execute("mkdir directory_name");
execute("mkdir relative\\path\\directory_name");
execute("mkdir C:\\absolute\\path\\directory_name");
execute("mkdir \"directory with\\spaces in\\name\"");

Execute performs an operation through the system shell.

-NotRabidWombat
Title:
Post by: BottledHate on 13 July, 2004, 07:37:03
i figured it was an external thing for making dirs....


anyways... to finalize this thread...... this may not be the best way of going about it... but it works!!! take what you need from it and move on.   this grabs a random username from a specific hub...
****edit: for BCDC++ !!!!!!*****
randomName = function(tbl)
   local n = 0
   table.foreach( tbl, function() n = n + 1 end )
   n = math.random( n )
      for k,v in tbl do
         if n == 1 then
            return k
         end
         n = n - 1
      end
end

dcpp:setListener( "chat", "userlist",
function( hub, user, text )
      if string.find( text, "rnduser" ) then
         rand = randomName(hub._users)
         DC():PrintDebug("Random Nick: "..rand)
       end
   end
)

thanks alot NotRabidWombat and Sedulus for your help.

-BottledHate