local t = os.date()
t = string.gsub(t, "%D", "")
math.randomseed(t)
function RollDice()
return math.random(1, 6)
end
--// 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 **" )
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..
--// 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 **" )
-- hubPm = pm message with a different prefix than the nick in the From field
-- f( hub, user, "message which may include a " )
-- DISCARDABLE
QuoteOriginally posted by Quicksilveryes, it works, but it's not the smart thing to use.
so this works fine for sending:
DC():SendHubMessage(hub:getId(), "$To: "..user:getNick().." From: "..hub:getOwnNick().." $<"..hub:getOwnNick().."> "..out.."|")
QuoteAnd last a question about these hub und user objects. They are called tables right?yes, tables are used for both arrays and maps.
Am I right with table = array in lua?
Quoteand with the :getnick() I address one field of these arrays?yes and no,
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. :-)