Dice Simulator
 

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

Dice Simulator

Started by Quicksilver, 23 March, 2005, 22:50:29

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Quicksilver

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.

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Quicksilver

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?)

Sedulus

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 **" )

Sedulus

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

Quicksilver

#5
--// 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.

Sedulus

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 )

Quicksilver

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.

Sedulus

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

Quicksilver

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?

Sedulus

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.

Sedulus

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

SMF spam blocked by CleanTalk