]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/dice.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / dice.rb
1 ##################
2 # Filename: dice.rb
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)
5 # Version: 0.3.2
6 # Date: Sat 6 Apr 2002
7 #
8 # You can get rbot from: http://www.linuxbrit.co.uk/rbot/
9 #
10 # Changelog
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)
17 #
18 # TODO: Test! Test! Test!
19 #       Comment!
20 #       Fumble/Critical counter (1's and x's where x is sides on dice)
21 ####################################################
22
23 class DiceDisplay
24   attr_reader :total, :view
25   def initialize(view, total)
26     @total = total
27     @view = view
28   end
29 end
30
31 class DicePlugin < Plugin
32   def help(plugin, topic="")
33     "dice <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"
34   end
35
36   def rolldice(d)
37     dice = d.split(/d/)
38     r = 0
39     unless dice[0] =~ /^[0-9]+/
40       dice[0] = 1
41     end
42     for i in 0...dice[0].to_i
43       r = r + rand(dice[1].to_i) + 1
44     end
45     return r
46   end
47
48   def iddice(d)
49     porm = d.slice!(0,1)
50     if d =~ /d/
51       r = rolldice(d)
52     else
53       r = d
54     end
55     if porm == "-"
56       r = 0 - r.to_i
57     end
58     viewer = DiceDisplay.new("[" + porm.to_s + d.to_s + "=" + r.to_s + "] ", r)
59     return viewer
60   end
61
62   def privmsg(m)
63     unless(m.params && m.params =~ /^[0-9]*d[0-9]+([+-]([0-9]+|[0-9]*d[0-9])+)*$/)
64       m.reply "incorrect usage: " + help(m.plugin)
65       return
66     end
67     a = m.params.scan(/^[0-9]*d[0-9]+|[+-][0-9]*d[0-9]+|[+-][0-9]+/)
68     r = rolldice(a[0])
69     t = "[" + a[0].to_s + "=" + r.to_s + "] "
70     for i in 1...a.length
71       tmp = iddice(a[i])
72       r = r + tmp.total.to_i
73       t = t + tmp.view.to_s
74     end
75     m.reply r.to_s + " | " + t
76   end
77 end
78 plugin = DicePlugin.new
79 plugin.register("dice")
80 ##############################################
81 #fin