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

#inspect, #to_s

Methods inherited from SimplePart

#inspect, #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
# File 'lib/dicebag/roll_part.rb', line 14

def initialize(part)
  super(part)

  @total   = nil
  @tally   = []
  @count   = part[:count]
  @sides   = part[:sides]
  @notes   = part[:notes]
  @options = default_options

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

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#reroll_countObject (readonly)

Returns the value of attribute reroll_count.



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

def reroll_count
  @reroll_count
end

#sidesObject (readonly)

Returns the value of attribute sides.



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

def sides
  @sides
end

#tallyObject (readonly)

Returns the value of attribute tally.



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

def tally
  @tally
end

Instance Method Details

#<=>(other) ⇒ Object



102
103
104
# File 'lib/dicebag/roll_part.rb', line 102

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

#__roll_for_keep_lowestObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/dicebag/roll_part.rb', line 71

def __roll_for_keep_lowest
  @tally = @results.dup

  @tally.sort!
  @tally.reverse!
  @results.sort!

  handle_keeplowest
  handle_total
end

#averageObject



90
91
92
# File 'lib/dicebag/roll_part.rb', line 90

def average
  (minimum + maximum) / 2.0
end

#default_optionsObject

Our Default Options

Note the absence of :explode, that is handled below.



30
31
32
33
34
35
36
37
38
39
# File 'lib/dicebag/roll_part.rb', line 30

def default_options
  {
    drop:       0,
    keep:       0,
    keeplowest: 0,
    reroll:     0,
    target:     0,
    failure:    0
  }
end

#maximumObject



94
95
96
# File 'lib/dicebag/roll_part.rb', line 94

def maximum
  count * sides
end

#minimumObject



98
99
100
# File 'lib/dicebag/roll_part.rb', line 98

def minimum
  count
end

#notesObject



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

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

#rollObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dicebag/roll_part.rb', line 50

def roll
  generate_results

  return __roll_for_keep_lowest if @options[:keeplowest].positive?

  @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
end

#rolled?Boolean

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/dicebag/roll_part.rb', line 46

def rolled?
  !@total.nil?
end

#totalObject

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



84
85
86
87
88
# File 'lib/dicebag/roll_part.rb', line 84

def total
  roll if @total.nil?

  @total
end