Class: GamesDice::DieResult
- Inherits:
-
Object
- Object
- GamesDice::DieResult
- Includes:
- Comparable
- Defined in:
- lib/games_dice/die_result.rb
Overview
This class models the output of GamesDice::ComplexDie.
An object of the class represents the results of a roll of a ComplexDie, including any re-rolls and value mapping.
Instance Attribute Summary collapse
-
#mapped ⇒ Boolean
readonly
Whether or not #value has been mapped from #total.
-
#roll_reasons ⇒ Array<Symbol>
readonly
The individual reasons for each roll of the die.
-
#rolls ⇒ Array<Integer>
readonly
The individual die rolls that combined to generate this result.
-
#total ⇒ Integer?
readonly
Combined result of all rolls, before mapping.
-
#value ⇒ Integer?
readonly
Combined result of all rolls, after mapping.
Instance Method Summary collapse
-
#add_roll(roll_result, roll_reason = :basic) ⇒ Integer
Adds value from a new roll to the object.
-
#apply_map(to_value, description = '') ⇒ nil
Sets value arbitrarily, and notes that the value has been mapped.
-
#clone ⇒ GamesDice::DieResult
This is a deep clone, all attributes are also cloned.
-
#explain_value ⇒ String
Generates a text description of how #value is determined.
-
#initialize(first_roll_result = nil, first_roll_reason = :basic) ⇒ GamesDice::DieResult
constructor
Creates new instance of GamesDice::DieResult.
Constructor Details
#initialize(first_roll_result = nil, first_roll_reason = :basic) ⇒ GamesDice::DieResult
Creates new instance of GamesDice::DieResult. The object can be initialised "empty" or with a first result.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/games_dice/die_result.rb', line 67 def initialize(first_roll_result = nil, first_roll_reason = :basic) unless GamesDice::REROLL_TYPES.key?(first_roll_reason) raise ArgumentError, "Unrecognised reason for roll #{first_roll_reason}" end if first_roll_result init_with_result(first_roll_result, first_roll_reason) else init_empty end @mapped = false @value = @total end |
Instance Attribute Details
#mapped ⇒ Boolean (readonly)
Whether or not #value has been mapped from #total.
99 100 101 |
# File 'lib/games_dice/die_result.rb', line 99 def mapped @mapped end |
#roll_reasons ⇒ Array<Symbol> (readonly)
The individual reasons for each roll of the die. See GamesDice::RerollRule for allowed values.
87 88 89 |
# File 'lib/games_dice/die_result.rb', line 87 def roll_reasons @roll_reasons end |
#rolls ⇒ Array<Integer> (readonly)
The individual die rolls that combined to generate this result.
83 84 85 |
# File 'lib/games_dice/die_result.rb', line 83 def rolls @rolls end |
#total ⇒ Integer? (readonly)
Combined result of all rolls, before mapping.
91 92 93 |
# File 'lib/games_dice/die_result.rb', line 91 def total @total end |
#value ⇒ Integer? (readonly)
Combined result of all rolls, after mapping.
95 96 97 |
# File 'lib/games_dice/die_result.rb', line 95 def value @value end |
Instance Method Details
#add_roll(roll_result, roll_reason = :basic) ⇒ Integer
Adds value from a new roll to the object. GamesDice::DieResult tracks reasons for the roll and makes the correct adjustment to the total so far. Any mapped value is cleared.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/games_dice/die_result.rb', line 106 def add_roll(roll_result, roll_reason = :basic) raise ArgumentError, "Unrecognised roll reason #{roll_reason}" unless GamesDice::REROLL_TYPES.key?(roll_reason) @rolls << Integer(roll_result) @roll_reasons << roll_reason @total = 0 if @rolls.length == 1 apply_roll_to_total(roll_reason, roll_result) @mapped = false @value = @total end |
#apply_map(to_value, description = '') ⇒ nil
Sets value arbitrarily, and notes that the value has been mapped. Used by GamesDice::ComplexDie when there are one or more GamesDice::MapRule objects to process for a die.
124 125 126 127 128 129 |
# File 'lib/games_dice/die_result.rb', line 124 def apply_map(to_value, description = '') @mapped = true @value = to_value @map_description = description nil end |
#clone ⇒ GamesDice::DieResult
This is a deep clone, all attributes are also cloned.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/games_dice/die_result.rb', line 154 def clone cloned = GamesDice::DieResult.new cloned.instance_variable_set(:@rolls, @rolls.clone) cloned.instance_variable_set(:@roll_reasons, @roll_reasons.clone) cloned.instance_variable_set(:@total, @total) cloned.instance_variable_set(:@value, @value) cloned.instance_variable_set(:@mapped, @mapped) cloned.instance_variable_set(:@map_description, @map_description) cloned end |
#explain_value ⇒ String
Generates a text description of how #value is determined. If #value has been mapped, includes the map description, but does not include the mapped value.
134 135 136 137 138 139 140 141 142 |
# File 'lib/games_dice/die_result.rb', line 134 def explain_value text = if @rolls.length < 2 @total.to_s else explain_value_multiple_rolls end text += " #{@map_description}" if @mapped && @map_description&.length&.positive? text end |