PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: Quicksilver on 23 March, 2005, 22:50:29

Title: Dice Simulator
Post by: Quicksilver on 23 March, 2005, 22:50:29
Here a request for a script.

I hope someone can help me out.  I need a script for BCDC/DCDM  to simulate Multiple six sided dices.

Best would be if the script would look for someone typing "+roll #"  in mainchat  or via PM and then give back the numbers rolled (in pm if received via pm).

I would be glad if someone could code that for me  since I am (still) very bad in Lua. Hope it is not too much work, but it would help me a lot with my roleplaying group.
Title:
Post by: plop on 23 March, 2005, 23:24:41
i get from this that you wanna learn.
if so check the uptime.lua script from bcdc for some hints.
here's the dice.
local t = os.date()
t = string.gsub(t, "%D", "")
math.randomseed(t)


function RollDice()
   return math.random(1, 6)
end

plop
Title:
Post by: Quicksilver on 24 March, 2005, 12:02:39
local t = os.date()
t = string.gsub(t, "%D", "")
math.randomseed(t)


function RollDice()
   return math.random(1, 6)
end


dcpp:setListener( "chat", "roll",
   function( hub, user, text )
      local s = string.lower( text )
      if string.find( s, "+roll" ) then
         hub:sendChat( RollDice())
      end
   end            

so thats what I got together with your Dice,  well my main problems are how to addres specific parts of the string +roll 5 or +roll 16  to get the number. (Also I am not shure if this listener thingy will work like this?)
Title:
Post by: Sedulus on 24 March, 2005, 13:52:12
something like this?
--// vim:ts=4:sw=4:noet
--// rolldice.lua

math.randomseed( os.time() )
local rolldice = {}

function rolldice.Dice( n )
    local self = {}
    self._rolled = {}
    function self.toString( this )
        if table.getn( this._rolled ) == 0 then
            return "Illegal input, choose a number between 1 and 16"
        end
        local str = ""
        local total = 0
        table.foreach( this._rolled, function( k, v )
            str = str .. v .. " "
            total = total + v
        end )
        str = str .. "= " .. total
        return str
    end
    if not n or n <= 0 or n > 16 then return self end
    for i = 1,n do table.insert( self._rolled, math.random( 1, 6 ) ) end
    return self
end

function rolldice.onInput( hub, user, msg, pm )
    local ret,_,n = string.find( msg, "^%+roll (%d+)$" )
    if ret then
        local out = "The dice rolled and turned up: " ..
                    rolldice.Dice( tonumber( n ) ):toString()
        if not pm then
            hub:sendChat( out )
        else
            user:sendPrivMsgFmt( out, 1 )
        end
        return 1
    end
end

dcpp:setListener( "chat", "rolldice", rolldice.onInput )
dcpp:setListener( "ownChat", "rolldice", function( hub, msg )
    rolldice.onInput( hub, nil, msg )
end )
dcpp:setListener( "pm", "rolldice", function( hub, user, msg )
    return rolldice.onInput( hub, user, msg, 1 )
end )

DC():PrintDebug( "  ** Loaded rolldice.lua **" )
Title: bbcode code-tag?
Post by: Sedulus on 24 March, 2005, 14:02:16
wtf is up with the bbcode code tag nowadays?

plop's post shows this here:

local t = os.date()

t = string.gsub(t, "%D", "")

there I get the tahoma font, and double line-feeds

my code shows this:


code:



--// vim:ts=4:sw=4:noet

there I get courier because of the

 and double line-feeds

1) Tahoma isn't really a neat code font
2)
 doesn't want 
's, now we get double line-feeds..
Title:
Post by: Quicksilver on 24 March, 2005, 22:04:53
--// vim:ts=4:sw=4:noet
--// rolldice.lua

math.randomseed( os.time() )
local rolldice = {}

function rolldice.Dice( n )
local self = {}
self._rolled = {}
function self.toString( this )
if table.getn( this._rolled ) == 0 then
return "Illegal input, choose a number between 1 and 50"
end
local str = ""
local total = 0
table.foreach( this._rolled, function( k, v )
str = str .. v .. " "
total = total + v
end )
str = str .. "= " .. total
return str
end
if not n or n <= 0 or n > 50 then return self end
for i = 1,n do table.insert( self._rolled, math.random( 1, 6 ) ) end
return self
end

function rolldice.onInput( hub, user, msg, pm )
local ret,_,n = string.find( msg, "^%+roll (%d+)$" )
if ret then
local out = "Die W?rfel zeigen: " ..
rolldice.Dice( tonumber( n ) ):toString()
if not pm then
hub:sendChat( out )
else
[U]DC():SendHubMessage(hub:getId(), "$To: "..user.." From: "..hub:getOwnNick().." $<"..hub:getOwnNick().."> "..out.."|") [/U]
end
return 1
end
end

dcpp:setListener( "chat", "rolldice", rolldice.onInput )
dcpp:setListener( "ownChat", "rolldice", function( hub, msg )
rolldice.onInput( hub, nil, msg )
end )
dcpp:setListener( "pm", "rolldice", function( hub, user, msg )
return rolldice.onInput( hub, user, msg, 1 )
end )


DC():PrintDebug( " ** Loaded rolldice.lua **" )



Thats what I tried to get the dice also work in chatrooms, but this doesn't work.  Well may be because I am rather guessing what I need to use.  Any hints for me?
My main Problem  is somehow the whole syntax of lua, I wouldn't have a problem to program this in c++ or java, but I don't have a clue what objects exist and how to call them so what I need to send where to get some message out.

like hub:getid()  is the syntax correct?  normally I would type this hub.getid()  but lua seems to have here a different syntax.  I simply don't get it.
Title:
Post by: Sedulus on 24 March, 2005, 22:34:46
if you didn't want the message stopped, you should've replaced:

user:sendPrivMsgFmt( out, 1 )

with

user:sendPrivMsgFmt( out )

and

return rolldice.onInput( hub, user, msg, 1 )

with

rolldice.onInput( hub, user, msg, 1 )
Title:
Post by: Quicksilver on 25 March, 2005, 11:54:39
Hmm message stopped? Don't know what you mean with that.

Problem is that
user:sendPrivMsgFmt( out)   also doesn't help  still the Chatroom won't get a dice rolled, don't know if the command doesn't  reach the chatroom  or if the listener doesn't work with it.
Don't know.  But with the now underlined raw  at least the chatroom should be reached (if the variables were set correct(?))  but well it doesn't work like this.
Title:
Post by: Sedulus on 25 March, 2005, 15:03:58
aha, okay you got me off track there with your output modification (which (apart from that user is an object and not a string) is how sendPrivMsgFmt is implemented anyway).

what you need, is a different listener:

--   hubPm      = pm message with a different prefix than the nick in the From field
--                f( hub, user, "message which may include a " )
--                DISCARDABLE
Title:
Post by: Quicksilver on 25 March, 2005, 17:35:26
So then thanks a  lot to you two. Now it works  very well

and best of all I learned a bit more about lua.

so this works fine for sending:
DC():SendHubMessage(hub:getId(), "$To: "..user:getNick().." From: "..hub:getOwnNick().." $<"..hub:getOwnNick().."> "..out.."|")



And last a question about these  hub und user objects.  They are called tables right?
Am I right with   table = array in lua?

and with the  :getnick() I address one field of these arrays?
Title:
Post by: Sedulus on 25 March, 2005, 20:19:26
QuoteOriginally posted by Quicksilver
so this works fine for sending:
DC():SendHubMessage(hub:getId(), "$To: "..user:getNick().." From: "..hub:getOwnNick().." $<"..hub:getOwnNick().."> "..out.."|")
yes, it works, but it's not the smart thing to use.
user:sendPrivMsgFmt works just fine for your purpose, is shorter, and best of all, works when the client-server protocol is different (it's very likely that ADC support will be added when ADC hubs gets more common, and your solution will break in those hubs).

QuoteAnd last a question about these  hub und user objects.  They are called tables right?
Am I right with   table = array in lua?
yes, tables are used for both arrays and maps.

Quoteand with the  :getnick() I address one field of these arrays?
yes and no,
'myTable.getNick( myTable, ... )' == 'myTable:getNick( ... )'
it's syntactic sugar
this way you can implement objects by using a table as well.

more syntactic sugar:
'a = function( ... )' == 'function a( ... )'

there are various links to lua manuals, references and beginners guides on this forum.
Title:
Post by: Sedulus on 25 March, 2005, 20:35:26
beware: it seems that on win2k the random number generator has some issues.

Quote[18:59:31] <[rug.nl]Foppe> +roll 16
[18:59:31] <[SU]Sedulus> The dice rolled and turned up: 6 2 2 4 5 6 5 3 2 5 3 3 6 5 1 4 = 62
[18:59:31] <[hhs.nl]TTB> The dice rolled and turned up: 6 2 2 4 5 6 5 3 2 5 3 3 6 5 1 4 = 62
[19:55:35] <[hhs.nl]Strandman> :S

QuoteA good 'seed' is os.time(). To get nice random numbers use:

math.randomseed( os.time() )

But beware! The first random number you get is not really 'randomized' (at least in Windows 2k). To get better pseudo-random number just pop some random number before using them for real:

-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)

this works