Class: DiceBag::RollPart
- Inherits:
-
SimplePart
- Object
- SimplePart
- DiceBag::RollPart
- Includes:
- RollPartString
- Defined in:
- lib/dicebag/roll_part.rb
Overview
This represents the xDx part of the dice string.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#reroll_count ⇒ Object
readonly
Returns the value of attribute reroll_count.
-
#sides ⇒ Object
readonly
Returns the value of attribute sides.
-
#tally ⇒ Object
readonly
Returns the value of attribute tally.
Attributes inherited from SimplePart
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #__roll_for_keep_lowest ⇒ Object
- #average ⇒ Object
-
#default_options ⇒ Object
Our Default Options.
-
#initialize(part) ⇒ RollPart
constructor
A new instance of RollPart.
- #maximum ⇒ Object
- #minimum ⇒ Object
- #notes ⇒ Object
- #roll ⇒ Object
-
#rolled? ⇒ Boolean
Checks to see if this instance has rolled yet or not.
-
#total ⇒ Object
Gets the total of the last roll; if there is no last roll, it calls roll() first.
Methods included from RollPartString
Methods inherited from SimplePart
Constructor Details
#initialize(part) ⇒ RollPart
Returns a new instance of RollPart.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dicebag/roll_part.rb', line 14 def initialize(part) super(part) @total = nil @tally = [] @count = part[:count] @sides = part[:sides] @notes = part[:notes] @options = @options.update(part[:options]) if part.key?(:options) end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
7 8 9 |
# File 'lib/dicebag/roll_part.rb', line 7 def count @count end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/dicebag/roll_part.rb', line 10 def @options end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
9 10 11 |
# File 'lib/dicebag/roll_part.rb', line 9 def parts @parts end |
#reroll_count ⇒ Object (readonly)
Returns the value of attribute reroll_count.
12 13 14 |
# File 'lib/dicebag/roll_part.rb', line 12 def reroll_count @reroll_count end |
#sides ⇒ Object (readonly)
Returns the value of attribute sides.
8 9 10 |
# File 'lib/dicebag/roll_part.rb', line 8 def sides @sides end |
#tally ⇒ Object (readonly)
Returns the value of attribute tally.
11 12 13 |
# File 'lib/dicebag/roll_part.rb', line 11 def tally @tally end |
Instance Method Details
#<=>(other) ⇒ Object
102 103 104 |
# File 'lib/dicebag/roll_part.rb', line 102 def <=>(other) total <=> other.total end |
#__roll_for_keep_lowest ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dicebag/roll_part.rb', line 71 def __roll_for_keep_lowest @tally = @results.dup @tally.sort! @tally.reverse! @results.sort! handle_keeplowest handle_total end |
#average ⇒ Object
90 91 92 |
# File 'lib/dicebag/roll_part.rb', line 90 def average (minimum + maximum) / 2.0 end |
#default_options ⇒ Object
Our Default Options
Note the absence of :explode, that is handled below.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dicebag/roll_part.rb', line 30 def { drop: 0, keep: 0, keeplowest: 0, reroll: 0, target: 0, failure: 0 } end |
#maximum ⇒ Object
94 95 96 |
# File 'lib/dicebag/roll_part.rb', line 94 def maximum count * sides end |
#minimum ⇒ Object
98 99 100 |
# File 'lib/dicebag/roll_part.rb', line 98 def minimum count end |
#notes ⇒ Object
41 42 43 |
# File 'lib/dicebag/roll_part.rb', line 41 def notes @notes.empty? ? '' : @notes.join("\n") end |
#roll ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dicebag/roll_part.rb', line 50 def roll generate_results return __roll_for_keep_lowest if @options[:keeplowest].positive? @results.sort! @results.reverse! # Save the tally in case it's requested later. @tally = @results.dup # Drop the low end numbers if :drop is not zero. handle_drop # Keep the high end numbers if :keep is greater than zero. handle_keep # Set the total. handle_total end |
#rolled? ⇒ Boolean
Checks to see if this instance has rolled yet or not.
46 47 48 |
# File 'lib/dicebag/roll_part.rb', line 46 def rolled? !@total.nil? end |
#total ⇒ Object
Gets the total of the last roll; if there is no last roll, it calls roll() first.
84 85 86 87 88 |
# File 'lib/dicebag/roll_part.rb', line 84 def total roll if @total.nil? @total end |