Class: FifthedSim::RollNode

Inherits:
DiceExpression show all
Defined in:
lib/fifthed_sim/nodes/roll_node.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DiceExpression

#*, #+, #-, #/, #max, #min, #or_greater, #or_least, #percentile, #range, #test_then, #to_dice_expression, #to_f, #to_i

Constructor Details

#initialize(val, type) ⇒ RollNode

Returns a new instance of RollNode.



31
32
33
34
35
36
37
# 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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 43

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



43
44
45
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 43

def value
  @value
end

Class Method Details

.average(type) ⇒ Object

Obtain a DieRoll filled with the average result of this die type This will round down.



20
21
22
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 20

def self.average(type)
  self.new((type + 1) / 2, type)
end

.average_value(type) ⇒ Object

Obtain an average value for this die type, as a float We’re extremely lazy here.



27
28
29
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 27

def self.average_value(type)
  self.new(1, type).average
end

.roll(type) ⇒ Object

Create a diceresult by rolling a certain type.

Raises:

  • (ArgumentError)


12
13
14
15
# 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

Instance Method Details

#averageObject

The average roll for a die of this type



47
48
49
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 47

def average
  (@type + 1) / 2.0
end

#crit?Boolean

Is this roll a critical? (AKA, is it the max value of the dice?)

Returns:

  • (Boolean)


65
66
67
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 65

def crit?
  @value == @type
end

#critfail?Boolean

Is this roll a critical failure? (AKA, is it a 1?)

Returns:

  • (Boolean)


59
60
61
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 59

def critfail?
  @value == 1
end

#difference_from_averageObject

How far away this roll is from the average roll



53
54
55
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 53

def difference_from_average
  @value - average
end

#distributionObject



69
70
71
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 69

def distribution
  Distribution.for((1..@type))
end

#expression_equationObject



84
85
86
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 84

def expression_equation
  "d#{@type}"
end

#rerollObject



39
40
41
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 39

def reroll
  self.class.roll(@type)
end

#value_equation(terminal: false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# 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