3 # Description: Rbot plugin. Rolls rpg style dice
4 # Author: David Dorward (http://david.us-lot.org/ - you might find a more up to date version of this plugin there)
8 # You can get rbot from: http://www.linuxbrit.co.uk/rbot/
11 # 0.1 - Initial release
12 # 0.1.1 - bug fix, only 1 digit for number of dice sides on first roll
13 # 0.3.0 - Spelling correction on changelog 0.1.1
14 # - Return results of each roll
15 # 0.3.1 - Minor documentation update
16 # 0.3.2 - Bug fix, could not subtract numbers (String can't be coerced into Fixnum)
18 # TODO: Test! Test! Test!
20 # Fumble/Critical counter (1's and x's where x is sides on dice)
21 ####################################################
24 attr_reader :total, :view, :dice
25 def initialize(dice, view, total)
32 return "["+ dice.to_s + ": " + total.to_s + " | " + view + "] "
36 class DicePlugin < Plugin
37 Config.register Config::IntegerValue.new('dice.max_dices',
38 :default => 100, :validate => Proc.new{|v| v > 0},
39 :desc => "Maximum number of dices to throw.")
41 Config.register Config::IntegerValue.new('dice.max_sides',
42 :default => 100, :validate => Proc.new{|v| v > 0},
43 :desc => "Maximum number of sides per dice.")
45 def help(plugin, topic="")
46 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"
53 unless dice[0] =~ /^[0-9]+/
56 for i in 0...dice[0].to_i
57 tmp = rand(dice[1].to_i) + 1
61 return DiceDisplay.new(d, repr.join(", "), r)
79 viewer = DiceDisplay.new(porm + dice, d.to_s, r)
84 # If either not given parameters or given incorrect parameters, return with
86 unless(m.params && m.params =~ /^[0-9]*d[0-9]+(\s*[+-]\s*([0-9]+|[0-9]*d[0-9])+)*$/)
87 m.nickreply "incorrect usage: " + help(m.plugin)
91 # Extract the actual dice request from the message parameters, splitting it
92 # into dice and modifiers
93 a = m.params.gsub(/\s+/,'').scan(/^[0-9]*d[0-9]+|[+-][0-9]*d[0-9]+|[+-][0-9]+/)
94 # check nr of total dices and sides per dice
97 dc, ds = dice.split(/d/)
99 if ds.to_i > @bot.config['dice.max_sides']
100 m.reply "sorry, don't have any dices with more than %u sides" % @bot.config['dice.max_sides']
103 # We use .max with 1 so that specs such as d6 count as 1 and not as 0
104 nr += [dc.to_i, 1].max
106 if nr > @bot.config['dice.max_dices']
107 m.reply "can't handle more than %u dices" % @bot.config['dice.max_dices']
111 # Roll the dice with the extracted request
112 rolled = rolldice(a[0])
114 t = rolled.get_view()
116 # Deal with all the remaining parts of the given dice request
117 for i in 1...a.length
119 r = r + tmp.total.to_i
123 m.nickreply r.to_s + " || " + m.params + ": " + t
126 plugin = DicePlugin.new
127 plugin.register("dice")
128 plugin.register("roll")
129 ##############################################