4 # :title: dice plugin for rbot
6 # Author:: David Dorward (http://david.us-lot.org/ - you might find a more up to date version of this plugin there)
7 # Author:: Moritz Augsburger <moritz@augsburger.name>
9 # Description:: Rolls rpg style dice
11 # Date:: Mon 8 Feb 2008
14 # 0.1 - Initial release
15 # 0.1.1 - bug fix, only 1 digit for number of dice sides on first roll
16 # 0.3.0 - Spelling correction on changelog 0.1.1
17 # - Return results of each roll
18 # 0.3.1 - Minor documentation update
19 # 0.3.2 - Bug fix, could not subtract numbers (String can't be coerced into Fixnum)
20 # 0.4 - Limit number of dices and number of sides per dice
22 # TODO:: Test! Test! Test!
24 # Fumble/Critical counter (1's and x's where x is sides on dice)
27 attr_reader :total, :view, :dice
28 def initialize(dice, view, total)
35 return "["+ dice.to_s + ": " + total.to_s + " | " + view + "] "
39 class DicePlugin < Plugin
40 Config.register Config::IntegerValue.new('dice.max_dices',
41 :default => 100, :validate => Proc.new{|v| v > 0},
42 :desc => "Maximum number of dices to throw.")
44 Config.register Config::IntegerValue.new('dice.max_sides',
45 :default => 100, :validate => Proc.new{|v| v > 0},
46 :desc => "Maximum number of sides per dice.")
48 def help(plugin, topic="")
49 plugin + " <string> (where <string> is something like: d6 or 2d6 or 2d6+4 or 2d6+1d20 or 2d6+1d5+4d7-3d4-6) => Rolls that set of virtual dice"
56 unless dice[0] =~ /^\d+/
59 for i in 0...dice[0].to_i
60 tmp = rand(dice[1].to_i) + 1
64 return DiceDisplay.new(d, repr.join(", "), r)
82 viewer = DiceDisplay.new(porm + dice, d.to_s, r)
87 # If either not given parameters or given incorrect parameters, return with
89 unless m.params && m.params =~ /^\d*d\d+(\s*[-]\s*(\d+|\d*d\d)+)*$/
90 m.reply "incorrect usage: " + help(m.plugin)
94 # Extract the actual dice request from the message parameters, splitting it
95 # into dice and modifiers
96 a = m.params.gsub(/\s+/,'').scan(/^\d*d\d+|[+-]\d*d\d+|[+-]\d+/)
97 # check nr of total dices and sides per dice
100 dc, ds = dice.split(/d/)
102 if ds.to_i > @bot.config['dice.max_sides']
103 m.reply "sorry, don't have any dices with more than %u sides" % @bot.config['dice.max_sides'], :nick => true
106 # We use .max with 1 so that specs such as d6 count as 1 and not as 0
107 nr += [dc.to_i, 1].max
109 if nr > @bot.config['dice.max_dices']
110 m.reply "can't handle more than %u dices" % @bot.config['dice.max_dices'], :nick => true
114 # Roll the dice with the extracted request
115 rolled = rolldice(a[0])
117 t = rolled.get_view()
119 # Deal with all the remaining parts of the given dice request
120 for i in 1...a.length
122 r = r + tmp.total.to_i
126 m.reply(r.to_s + " || " + m.params + ": " + t, :nick => true)
129 plugin = DicePlugin.new
130 plugin.register("dice")
131 plugin.register("roll")
132 ##############################################