get a random user name.... BCDC++
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

get a random user name.... BCDC++

Started by BottledHate, 11 July, 2004, 06:30:17

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

BottledHate

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.
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

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


I like childish behavior. Maybe this post will be deleted next.

BottledHate

i've messed with that a little bit, but i can't seem to get anything.  any other ideas?
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

You should be able to traverse the table with:

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

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

BottledHate

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.
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

Show us some of your code.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Sedulus

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

BottledHate

#7
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??**
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

#8
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


I like childish behavior. Maybe this post will be deleted next.

BottledHate

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....
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

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


I like childish behavior. Maybe this post will be deleted next.

BottledHate

#11
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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

SMF spam blocked by CleanTalk