class FifthedSim::RollNode
Model a single roll of the dice. Users of the library will rarely interact with this class, and will instead manpiulate values based on the DiceResult type.
Attributes
type[R]
value[R]
Public Class Methods
average(type)
click to toggle source
Obtain a DieRoll filled with the average result of this die type This will round down.
# File lib/fifthed_sim/nodes/roll_node.rb, line 20 def self.average(type) self.new((type + 1) / 2, type) end
average_value(type)
click to toggle source
Obtain an average value for this die type, as a float We're extremely lazy here.
# File lib/fifthed_sim/nodes/roll_node.rb, line 27 def self.average_value(type) self.new(1, type).average end
new(val, type)
click to toggle source
# File lib/fifthed_sim/nodes/roll_node.rb, line 31 def initialize(val, type) unless val.is_a?(Fixnum) && type.is_a?(Fixnum) raise ArgumentError, "Type invald" end @value = val @type = type end
roll(type)
click to toggle source
Create a diceresult by rolling a certain type.
# File lib/fifthed_sim/nodes/roll_node.rb, line 12 def self.roll(type) raise ArgumentError, "Must be an Integer" unless type.is_a? Fixnum self.new(SecureRandom.random_number(type) + 1, type) end
Public Instance Methods
average()
click to toggle source
The average roll for a die of this type
# File lib/fifthed_sim/nodes/roll_node.rb, line 47 def average (@type + 1) / 2.0 end
crit?()
click to toggle source
Is this roll a critical? (AKA, is it the max value of the dice?)
# File lib/fifthed_sim/nodes/roll_node.rb, line 65 def crit? @value == @type end
critfail?()
click to toggle source
Is this roll a critical failure? (AKA, is it a 1?)
# File lib/fifthed_sim/nodes/roll_node.rb, line 59 def critfail? @value == 1 end
difference_from_average()
click to toggle source
How far away this roll is from the average roll
# File lib/fifthed_sim/nodes/roll_node.rb, line 53 def difference_from_average @value - average end
distribution()
click to toggle source
# File lib/fifthed_sim/nodes/roll_node.rb, line 69 def distribution Distribution.for((1..@type)) end
expression_equation()
click to toggle source
# File lib/fifthed_sim/nodes/roll_node.rb, line 84 def expression_equation "d#{@type}" end
reroll()
click to toggle source
# File lib/fifthed_sim/nodes/roll_node.rb, line 39 def reroll self.class.roll(@type) end
value_equation(terminal: false)
click to toggle source
# File lib/fifthed_sim/nodes/roll_node.rb, line 73 def value_equation(terminal: false) return value.to_s unless terminal if critfail? Rainbow(value.to_s).color(:red).bright.to_s elsif crit? Rainbow(value.to_s).color(:yellow).bright.to_s else value.to_s end end