X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Fdice.rb;h=230035ec419acf36b447b0bcb08bf326542d4d58;hb=787d368e9fb6bc0ed972d5825769d598d45d1ca4;hp=0c47052a0d26482c823de19a2a0168e0dd488c08;hpb=257761628ebabf6c176a1def26ea2c92982f9658;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dice.rb b/data/rbot/plugins/dice.rb index 0c47052a..230035ec 100644 --- a/data/rbot/plugins/dice.rb +++ b/data/rbot/plugins/dice.rb @@ -1,11 +1,14 @@ -################## -# Filename: dice.rb -# Description: Rbot plugin. Rolls rpg style dice -# Author: David Dorward (http://david.us-lot.org/ - you might find a more up to date version of this plugin there) -# Version: 0.3.2 -# Date: Sat 6 Apr 2002 +#-- vim:sw=2:et +#++ # -# You can get rbot from: http://www.linuxbrit.co.uk/rbot/ +# :title: dice plugin for rbot +# +# Author:: David Dorward (http://david.us-lot.org/ - you might find a more up to date version of this plugin there) +# Author:: Moritz Augsburger +# +# Description:: Rolls rpg style dice +# Version:: 0.4 +# Date:: Mon 8 Feb 2008 # # Changelog # 0.1 - Initial release @@ -14,11 +17,11 @@ # - Return results of each roll # 0.3.1 - Minor documentation update # 0.3.2 - Bug fix, could not subtract numbers (String can't be coerced into Fixnum) +# 0.4 - Limit number of dices and number of sides per dice # -# TODO: Test! Test! Test! -# Comment! -# Fumble/Critical counter (1's and x's where x is sides on dice) -#################################################### +# TODO:: Test! Test! Test! +# Comment! +# Fumble/Critical counter (1's and x's where x is sides on dice) class DiceDisplay attr_reader :total, :view, :dice @@ -34,6 +37,14 @@ class DiceDisplay end class DicePlugin < Plugin + Config.register Config::IntegerValue.new('dice.max_dices', + :default => 100, :validate => Proc.new{|v| v > 0}, + :desc => "Maximum number of dices to throw.") + + Config.register Config::IntegerValue.new('dice.max_sides', + :default => 100, :validate => Proc.new{|v| v > 0}, + :desc => "Maximum number of sides per dice.") + def help(plugin, topic="") plugin + " (where is something like: d6 or 2d6 or 2d6+4 or 2d6+1d20 or 2d6+1d5+4d7-3d4-6) => Rolls that set of virtual dice" end @@ -83,6 +94,22 @@ class DicePlugin < Plugin # Extract the actual dice request from the message parameters, splitting it # into dice and modifiers a = m.params.gsub(/\s+/,'').scan(/^[0-9]*d[0-9]+|[+-][0-9]*d[0-9]+|[+-][0-9]+/) + # check nr of total dices and sides per dice + nr = 0 + a.each { |dice| + dc, ds = dice.split(/d/) + # check sides + if ds.to_i > @bot.config['dice.max_sides'] + m.reply "sorry, don't have any dices with more than %u sides" % @bot.config['dice.max_sides'] + return + end + # We use .max with 1 so that specs such as d6 count as 1 and not as 0 + nr += [dc.to_i, 1].max + } + if nr > @bot.config['dice.max_dices'] + m.reply "can't handle more than %u dices" % @bot.config['dice.max_dices'] + return + end # Roll the dice with the extracted request rolled = rolldice(a[0])