Sha256: 51ff33109590a2da3e7ff220bca5da948414a8a035b7890694f21277598ce6b9

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Cinch::Plugins
  class Dicebag
    class Bag
      attr_accessor :count, :dice, :score
      def initialize(dice_hash)
        fail unless good_hash?(dice_hash)
        @dice = dice_hash
        @count = 0
        @score = 0
      end

      def roll
        dice = die_array
        @score = Die.roll(dice)
        @count = dice.map { |d| d[/(\d+)d\d+/, 1].to_i || 1 }.inject(0, :+)
      end

      def good_hash?(dice_hash)
        dice_hash.keys { |k| return false unless k.is_a?(Integer) }
        dice_hash.values { |k| return false unless k.is_a?(Integer) }
        true
      end

      # Simple method to return a flavor text 'size' description based on
      #   how many dice you happened to get in your dicebag roll.
      # @param [Fixnum] size The number of dice in the dicebag.
      # @return [String] Description of the size of the bag.
      def size
        case count
        when 0..100     then 'tiny'
        when 101..500   then 'small'
        when 501..1000  then 'medium'
        when 1001..1500 then 'large'
        when 1501..2000 then 'hefty'
        else 'huge'
        end
      end

      private

      def die_array
        @dice.keys.map do |sides|
          "#{sides}d#{@dice[sides]}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-dicebag-1.0.10 lib/cinch/plugins/dicebag/bag.rb