dicebag 3.0.5 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,7 @@
18
18
  # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19
19
  # USE OR OTHER DEALINGS IN THE SOFTWARE.
20
20
  #
21
- # dicelib.rb -- version: 3.0.5
21
+ # dicelib.rb -- version: 3.1.0
22
22
 
23
23
  require 'parslet'
24
24
 
@@ -131,6 +131,18 @@ module DiceBag
131
131
 
132
132
  # Negate :drop. See why in RollPart#roll.
133
133
  xdx[:options][:drop] = -(drop)
134
+
135
+ # Finally, if we have a target number, make sure it is equal
136
+ # to or less than the dice sides and greater than 0, otherwise,
137
+ # set it to 0 (aka no target number) and add a note.
138
+ if xdx[:options].has_key?(:target)
139
+ target = xdx[:options][:target]
140
+
141
+ if target > sides or target < 0
142
+ xdx[:options][:target] = 0
143
+ notes.push("Target number too large or is negative; reset to 0.")
144
+ end
145
+ end
134
146
  end
135
147
 
136
148
  xdx[:notes] = notes unless notes.empty?
@@ -41,15 +41,16 @@ module DiceBag
41
41
  # assigned, which will leave it with a nil value.
42
42
  # This is handled in RollPart#initialize.
43
43
  rule(:explode) { str('e') >> number?.as(:explode) >> space? }
44
- rule(:drop) { str('~') >> number.as(:drop) >> space? }
45
- rule(:keep) { str('!') >> number.as(:keep) >> space? }
44
+ rule(:drop) { str('d') >> number.as(:drop) >> space? }
45
+ rule(:keep) { str('k') >> number.as(:keep) >> space? }
46
46
  rule(:reroll) { str('r') >> number.as(:reroll) >> space? }
47
+ rule(:target) { str('t') >> number.as(:target) >> space? }
47
48
 
48
49
  # This allows options to be defined in any order and
49
50
  # even have more than one of the same option, however
50
51
  # only the last option of a given key will be kept.
51
52
  rule(:options) {
52
- space? >> (drop | explode | keep | reroll).repeat >> space?
53
+ space? >> (drop | explode | keep | reroll | target).repeat >> space?
53
54
  }
54
55
 
55
56
  rule(:options?) { options.maybe.as(:options) }
@@ -20,7 +20,8 @@ module DiceBag
20
20
  :explode => 0,
21
21
  :drop => 0,
22
22
  :keep => 0,
23
- :reroll => 0
23
+ :reroll => 0,
24
+ :target => 0
24
25
  }
25
26
 
26
27
  @options.update(part[:options]) if part.has_key?(:options)
@@ -82,8 +83,15 @@ module DiceBag
82
83
  results = results[0 ... @options[:keep]]
83
84
  end
84
85
 
85
- # I think reduce(:+) is ugly, but it's very fast.
86
- @total = results.reduce(:+)
86
+ # If we have a target number, count how many rolls
87
+ # in the tally are >= than this number, otherwise
88
+ # we just add up all the numbers.
89
+ if @options[:target] and @options[:target] > 0
90
+ @total = results.count {|r| r >= @options[:target]}
91
+ else
92
+ # I think reduce(:+) is ugly, but it's very fast.
93
+ @total = results.reduce(:+)
94
+ end
87
95
 
88
96
  return self
89
97
  end
@@ -118,6 +126,7 @@ module DiceBag
118
126
  s += @options[:explode].to_s unless @options[:explode] == self.sides
119
127
  end
120
128
 
129
+ s += "#{sp}t" + @options[:target].to_s unless @options[:target].zero?
121
130
  s += "#{sp}~" + @options[:drop].abs.to_s unless @options[:drop].zero?
122
131
  s += "#{sp}!" + @options[:keep].to_s unless @options[:keep].zero?
123
132
  s += "#{sp}r" + @options[:reroll].to_s unless @options[:reroll].zero?
@@ -11,9 +11,10 @@ module DiceBag
11
11
  # 2-element arrays ('tagged arrays'), which is then
12
12
  # hashified later. (There is no way to update the
13
13
  # options when these rules are matched.)
14
- rule(:drop => simple(:x)) { [:drop, Integer(x)] }
15
- rule(:keep => simple(:x)) { [:keep, Integer(x)] }
16
- rule(:reroll => simple(:x)) { [:reroll, Integer(x)] }
14
+ rule(:drop => simple(:x)) { [:drop, Integer(x)] }
15
+ rule(:keep => simple(:x)) { [:keep, Integer(x)] }
16
+ rule(:reroll => simple(:x)) { [:reroll, Integer(x)] }
17
+ rule(:target => simple(:x)) { [:target, Integer(x)] }
17
18
 
18
19
  # Explode is special, in that if it is nil, then it
19
20
  # must remain that way.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dicebag
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: