Class: Cinch::Plugins::Dicebag::Bag

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/plugins/dicebag/bag.rb

Overview

Class to handle rolling of a preset bag of dice.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dice_hash) ⇒ Bag

Create a new bag

Parameters:

  • dice_hash (Hash)

    Hash of dice, in the format of { :num_sides => :max_dice_to_roll, … } The bag will randomly roll (1-:max_dice_to_roll) :num_sided dice.



12
13
14
15
16
17
# File 'lib/cinch/plugins/dicebag/bag.rb', line 12

def initialize(dice_hash)
  fail unless good_hash?(dice_hash)
  @dice = dice_hash
  @count = 0
  @score = 0
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



6
7
8
# File 'lib/cinch/plugins/dicebag/bag.rb', line 6

def count
  @count
end

#diceObject

Returns the value of attribute dice.



6
7
8
# File 'lib/cinch/plugins/dicebag/bag.rb', line 6

def dice
  @dice
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/cinch/plugins/dicebag/bag.rb', line 6

def score
  @score
end

Instance Method Details

#rollObject

Roll the bag of dice, this will roll the dice and update the current

score and count


30
31
32
33
34
35
# File 'lib/cinch/plugins/dicebag/bag.rb', line 30

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

#sizeString

Simple method to return a flavor text ‘size’ description based on

how many dice you happened to get in your dicebag roll.

Parameters:

  • size (Fixnum)

    The number of dice in the dicebag.

Returns:

  • (String)

    Description of the size of the bag.



41
42
43
44
45
46
47
48
49
50
# File 'lib/cinch/plugins/dicebag/bag.rb', line 41

def size
  case
  when @count < 1000 then 'tiny'
  when @count < 2000 then 'small'
  when @count < 3000 then 'medium'
  when @count < 4000 then 'large'
  when @count < 5000 then 'hefty'
  else 'massive'
  end
end

#statsObject



19
20
21
22
23
24
25
26
# File 'lib/cinch/plugins/dicebag/bag.rb', line 19

def stats
  max_score = @dice.keys.inject(0) { |sum, x| sum + (x * @dice[x]) }
  min_score = @dice.keys.count
  max_count = @dice.values.inject(0) { |sum, x| sum + x }
  min_count = @dice.keys.count
  { min_count: min_count, max_count: max_count,
    min_score: min_score, max_score: max_score }
end