Class: Tools::CheckRoll
- Inherits:
-
Object
- Object
- Tools::CheckRoll
- Defined in:
- app/models/tools/check_roll.rb
Instance Attribute Summary collapse
-
#equal_to_threshold ⇒ Object
Returns the value of attribute equal_to_threshold.
-
#greater_than_threshold ⇒ Object
Returns the value of attribute greater_than_threshold.
-
#result ⇒ Object
readonly
Returns the result as an
Array
with the format [true/false, simple_best_rolled]. -
#rolling_chances ⇒ Object
Returns the value of attribute rolling_chances.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
-
#throw ⇒ Object
Returns the value of attribute throw.
Instance Method Summary collapse
-
#detailed_result ⇒ Object
Returns the result as an
Array
with the format [true/false, detailed_best_rolled]. -
#initialize(throw, threshold, rolling_chances = 1, greater_than_threshold = true, equal_to_threshold = true) ⇒ CheckRoll
constructor
Creates a new
CheckRoll
with aThrow
, aFixnum
asthreshold
and, optionally: *rolling_chances
: The number of times the Throw will be rolled before checking. -
#is_successful? ⇒ Boolean
Returns whether the check was successful or not.
-
#reroll ⇒ Object
Forces a new roll by deleting the old result.
-
#roll ⇒ Object
Rolls the Throw as many times as rolling chances are set.If has already been rolled just returns the last result.
-
#to_s ⇒ Object
Returns the CheckRoll as a string.
Constructor Details
#initialize(throw, threshold, rolling_chances = 1, greater_than_threshold = true, equal_to_threshold = true) ⇒ CheckRoll
Creates a new CheckRoll
with a Throw
, a Fixnum
as threshold
and, optionally:
-
rolling_chances
: The number of times the Throw will be rolled before checking. By default is1
. -
greater_than_threshold
: Wheter the check is done as greater than (>) or less than (<). By default istrue
. -
equal_to_threshold
: Wheter the check accepts the threshold as success (>= and <= iftrue
, > and < iffalse
). By default istrue
.
10 11 12 13 14 15 16 |
# File 'app/models/tools/check_roll.rb', line 10 def initialize throw, threshold, rolling_chances=1, greater_than_threshold=true, equal_to_threshold=true @throw = throw @threshold = threshold @rolling_chances = rolling_chances @greater_than_threshold = greater_than_threshold @equal_to_threshold = equal_to_threshold end |
Instance Attribute Details
#equal_to_threshold ⇒ Object
Returns the value of attribute equal_to_threshold.
4 5 6 |
# File 'app/models/tools/check_roll.rb', line 4 def equal_to_threshold @equal_to_threshold end |
#greater_than_threshold ⇒ Object
Returns the value of attribute greater_than_threshold.
4 5 6 |
# File 'app/models/tools/check_roll.rb', line 4 def greater_than_threshold @greater_than_threshold end |
#result ⇒ Object (readonly)
Returns the result as an Array
with the format [true/false, simple_best_rolled]. The first element of the array illustrates whether the check was successful or not. The second element is the best rolled throw as a simple Throw result, a Fixnum
. For a more detailed result (with a detailed Throw result), use detailed_result
. It rolls the check if not done before.
52 53 54 |
# File 'app/models/tools/check_roll.rb', line 52 def result @result end |
#rolling_chances ⇒ Object
Returns the value of attribute rolling_chances.
4 5 6 |
# File 'app/models/tools/check_roll.rb', line 4 def rolling_chances @rolling_chances end |
#threshold ⇒ Object
Returns the value of attribute threshold.
4 5 6 |
# File 'app/models/tools/check_roll.rb', line 4 def threshold @threshold end |
#throw ⇒ Object
Returns the value of attribute throw.
4 5 6 |
# File 'app/models/tools/check_roll.rb', line 4 def throw @throw end |
Instance Method Details
#detailed_result ⇒ Object
Returns the result as an Array
with the format [true/false, detailed_best_rolled]. The first element of the array illustrates whether the check was successful or not. The second element is the best rolled throw as a detailed Throw result, please refer to Tools::Throw.roll
for more info on detailed Throw. It rolls the check if not done before.
62 63 64 65 |
# File 'app/models/tools/check_roll.rb', line 62 def detailed_result roll if @result.blank? @result end |
#is_successful? ⇒ Boolean
Returns whether the check was successful or not. It rolls the check if not done before.
42 43 44 45 |
# File 'app/models/tools/check_roll.rb', line 42 def is_successful? roll if @result.blank? return @result[0] end |
#reroll ⇒ Object
Forces a new roll by deleting the old result.
36 37 38 39 |
# File 'app/models/tools/check_roll.rb', line 36 def reroll @result = nil roll end |
#roll ⇒ Object
Rolls the Throw as many times as rolling chances are set.If has already been rolled just returns the last result. Returns the result as an Array
with the format [true/false, simple_best_rolled]. The first element of the array illustrates whether the check was successful or not. The second element is the best rolled throw as a simple Throw result, a Fixnum
. For a more detailed result (with a detailed Throw result), use detailed_result
.
24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/tools/check_roll.rb', line 24 def roll return @result if @result best_rolled = 0 @rolling_chances.times do rolled_throw = @throw.roll 1,true #Detailed throw best_rolled = rolled_throw if rolled_throw[0] > best_rolled[0] end @result = [compare(best_rolled[0], @threshold),best_rolled] result end |
#to_s ⇒ Object
Returns the CheckRoll as a string. Example “2D4,1D6,1D20+3 must be greater or equal to 20.”
68 69 70 71 72 73 74 75 76 |
# File 'app/models/tools/check_roll.rb', line 68 def to_s string = "#{@throw.to_s} #{I18n.t('rpg-tools.check_roll.must_be')} #{comparatives_text} #{@threshold}." if @result string << " #{I18n.t('rpg-tools.check_roll.already_checked')}" string << " #{@result[0] ? I18n.t('rpg-tools.check_roll.success') : I18n.t('rpg-tools.check_roll.failure')}" string << " (#{@result[1][0]})." end string end |