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.
- data/lib/dicebag.rb +13 -1
- data/lib/dicebag/parser.rb +4 -3
- data/lib/dicebag/roll_part.rb +12 -3
- data/lib/dicebag/transform.rb +4 -3
- metadata +1 -1
data/lib/dicebag.rb
CHANGED
@@ -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
|
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?
|
data/lib/dicebag/parser.rb
CHANGED
@@ -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('
|
45
|
-
rule(:keep) { str('
|
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) }
|
data/lib/dicebag/roll_part.rb
CHANGED
@@ -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
|
-
#
|
86
|
-
|
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?
|
data/lib/dicebag/transform.rb
CHANGED
@@ -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,
|
15
|
-
rule(:keep => simple(:x)) { [:keep,
|
16
|
-
rule(:reroll => simple(:x)) { [:reroll,
|
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.
|