Class: DiceBag::RollPart

Inherits:
SimplePart show all
Includes:
RollPartString
Defined in:
lib/dicebag/roll_part.rb

Overview

This represents the xDx part of the dice string.

Instance Attribute Summary collapse

Attributes inherited from SimplePart

#value

Instance Method Summary collapse

Methods included from RollPartString

#to_s

Methods inherited from SimplePart

#result, #to_s

Constructor Details

#initialize(part) ⇒ RollPart

Returns a new instance of RollPart.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dicebag/roll_part.rb', line 14

def initialize(part)
  @total  = nil
  @tally  = []
  @value  = part
  @count  = part[:count]
  @sides  = part[:sides]
  @notes  = part[:notes] || []

  # Our Default Options
  @options = { explode: 0, drop: 0, keep: 0, reroll: 0, target: 0 }

  @options.update(part[:options]) if part.key?(:options)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/dicebag/roll_part.rb', line 8

def count
  @count
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dicebag/roll_part.rb', line 11

def options
  @options
end

#partsObject (readonly)

Returns the value of attribute parts.



10
11
12
# File 'lib/dicebag/roll_part.rb', line 10

def parts
  @parts
end

#sidesObject (readonly)

Returns the value of attribute sides.



9
10
11
# File 'lib/dicebag/roll_part.rb', line 9

def sides
  @sides
end

#tallyObject (readonly)

Returns the value of attribute tally.



12
13
14
# File 'lib/dicebag/roll_part.rb', line 12

def tally
  @tally
end

Instance Method Details

#<=>(other) ⇒ Object



75
76
77
# File 'lib/dicebag/roll_part.rb', line 75

def <=>(other)
  total <=> other.total
end

#notesObject



28
29
30
# File 'lib/dicebag/roll_part.rb', line 28

def notes
  @notes.empty? ? '' : @notes.join("\n")
end

#rollObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dicebag/roll_part.rb', line 46

def roll
  generate_results

  @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

  self
end

#roll_dieObject

Rolls a single die from the xDx string.



39
40
41
42
43
44
# File 'lib/dicebag/roll_part.rb', line 39

def roll_die
  num = 0
  num = rand(sides) + 1 while num <= @options[:reroll]

  num
end

#rolled?Boolean

Checks to see if this instance has rolled yet or not.

Returns:

  • (Boolean)


34
35
36
# File 'lib/dicebag/roll_part.rb', line 34

def rolled?
  @total.nil? ? false : true
end

#totalObject

Gets the total of the last roll; if there is no last roll, it calls roll() first.



69
70
71
72
73
# File 'lib/dicebag/roll_part.rb', line 69

def total
  roll if @total.nil?

  @total
end