dicebag 3.2.1 → 3.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dicebag.rb +36 -30
- data/lib/dicebag/parser.rb +7 -6
- data/lib/dicebag/transform.rb +26 -38
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a04e07e333e0dbee195dc20264c31791b3df0e30
|
4
|
+
data.tar.gz: e142197e3d345460e984497d85daf271468bc8c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f48eea7ecae33fd45c4f8849b74a627b8cbaf13ef01084320d8cc66397c581501a7852be399a7734f2118562c66e383c5c7d149099722b44387e1744b5b4981a
|
7
|
+
data.tar.gz: 98c8bb9b2e6199487db54d8b11a95f11cac4225a2687a54e356b58c31184e497e633226cf921445f13e5f883fedf92c116d72381dd1af4260a3d3a73bade4959
|
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.2.
|
21
|
+
# dicelib.rb -- version: 3.2.2
|
22
22
|
|
23
23
|
require 'parslet'
|
24
24
|
|
@@ -73,8 +73,10 @@ module DiceBag
|
|
73
73
|
LabelPart.new val
|
74
74
|
when Hash
|
75
75
|
RollPart.new normalize_xdx(val)
|
76
|
-
|
76
|
+
when Integer
|
77
77
|
StaticPart.new val
|
78
|
+
else
|
79
|
+
val
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
@@ -112,57 +114,57 @@ module DiceBag
|
|
112
114
|
end
|
113
115
|
|
114
116
|
# Prevent Explosion abuse.
|
115
|
-
def self.normalize_explode(
|
116
|
-
return unless
|
117
|
-
|
118
|
-
explode = xdx[:options][:explode]
|
117
|
+
def self.normalize_explode(hash)
|
118
|
+
return unless hash[:options].key? :explode
|
119
119
|
|
120
|
-
if explode
|
121
|
-
|
120
|
+
if hash[:options][:explode] == 1
|
121
|
+
hash[:options][:explode] = hash[:sides]
|
122
122
|
|
123
|
-
|
123
|
+
hash[:notes].push("Explode set to #{hash[:sides]}")
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
127
|
# Prevent Reroll abuse.
|
128
|
-
def self.normalize_reroll(
|
129
|
-
return unless
|
128
|
+
def self.normalize_reroll(hash)
|
129
|
+
return unless hash[:options].key? :reroll
|
130
130
|
|
131
|
-
if
|
132
|
-
|
131
|
+
if hash[:options][:reroll] >= hash[:sides]
|
132
|
+
hash[:options][:reroll] = 0
|
133
133
|
|
134
|
-
|
134
|
+
hash[:notes].push 'Reroll reset to 0.'
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
138
|
# Make sure there are enough dice to
|
139
139
|
# handle both Drop and Keep values.
|
140
140
|
# If not, both are reset to 0. Harsh.
|
141
|
-
def self.normalize_drop_keep(
|
142
|
-
drop =
|
143
|
-
keep =
|
141
|
+
def self.normalize_drop_keep(hash)
|
142
|
+
drop = hash[:options].fetch(:drop, 0)
|
143
|
+
keep = hash[:options].fetch(:keep, 0)
|
144
144
|
|
145
|
-
if (drop + keep) >=
|
146
|
-
|
147
|
-
|
145
|
+
if (drop + keep) >= hash[:count]
|
146
|
+
hash[:options][:drop] = 0
|
147
|
+
hash[:options][:keep] = 0
|
148
148
|
|
149
|
-
|
149
|
+
hash[:notes].push 'Drop and Keep Conflict. Both reset to 0.'
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
153
|
-
# Finally, if we have a target number,
|
154
|
-
#
|
155
|
-
#
|
156
|
-
|
157
|
-
|
153
|
+
# Finally, if we have a target number,
|
154
|
+
# make sure it is equal to or less than
|
155
|
+
# the dice sides and greater than 0,
|
156
|
+
# otherwise, set it to 0 (aka no target
|
157
|
+
# number) and add a note.
|
158
|
+
def self.normalize_target(hash)
|
159
|
+
return unless hash[:options].key? :target
|
158
160
|
|
159
|
-
target =
|
161
|
+
target = hash[:options][:target]
|
160
162
|
|
161
|
-
return if target >= 0 && target <=
|
163
|
+
return if target >= 0 && target <= hash[:sides]
|
162
164
|
|
163
|
-
|
165
|
+
hash[:options][:target] = 0
|
164
166
|
|
165
|
-
|
167
|
+
hash[:notes].push 'Target number too large or is negative; reset to 0.'
|
166
168
|
end
|
167
169
|
|
168
170
|
# This is the wrapper for the parse, transform,
|
@@ -175,6 +177,10 @@ module DiceBag
|
|
175
177
|
|
176
178
|
normalize_tree ast
|
177
179
|
end
|
180
|
+
|
181
|
+
def self.roll(dstr = '')
|
182
|
+
Roll.new(dstr).roll
|
183
|
+
end
|
178
184
|
end
|
179
185
|
|
180
186
|
# Our sub-modules.
|
data/lib/dicebag/parser.rb
CHANGED
@@ -24,10 +24,7 @@ module DiceBag
|
|
24
24
|
rule(:lparen) { str('(') }
|
25
25
|
rule(:rparen) { str(')') }
|
26
26
|
rule(:label) do
|
27
|
-
lparen >>
|
28
|
-
match('[^(),]').repeat(1).as(:label) >>
|
29
|
-
rparen >>
|
30
|
-
space?
|
27
|
+
lparen >> match('[^(),]').repeat(1).as(:label) >> rparen >> space?
|
31
28
|
end
|
32
29
|
|
33
30
|
# count and sides rules.
|
@@ -39,12 +36,16 @@ module DiceBag
|
|
39
36
|
# xDx Parts.
|
40
37
|
# All xDx parts may be followed by none, one, or more
|
41
38
|
# options.
|
39
|
+
#
|
40
|
+
# TODO: Remove the .as(:xdx) and rework the Transform
|
41
|
+
# sub-class to account for it. It'll make the
|
42
|
+
# resulting data much cleaner.
|
42
43
|
rule(:xdx) { (count >> sides).as(:xdx) >> options? }
|
43
44
|
|
44
45
|
# xdx Options.
|
45
46
|
# Note that :explode is allowed to NOT have a number
|
46
|
-
# assigned, which will leave it with a nil value.
|
47
|
-
#
|
47
|
+
# assigned, which will leave it with a nil value. This
|
48
|
+
# is handled in the Transform class.
|
48
49
|
rule(:explode) { str('e') >> number?.as(:explode) >> space? }
|
49
50
|
rule(:drop) { str('d') >> number.as(:drop) >> space? }
|
50
51
|
rule(:keep) { str('k') >> number.as(:keep) >> space? }
|
data/lib/dicebag/transform.rb
CHANGED
@@ -14,10 +14,9 @@ module DiceBag
|
|
14
14
|
opts
|
15
15
|
end
|
16
16
|
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# options when these rules are matched.)
|
17
|
+
# Options.
|
18
|
+
# The full options hash is updated later with these
|
19
|
+
# sub-hashes.
|
21
20
|
rule(drop: simple(:x)) { { drop: Integer(x) } }
|
22
21
|
rule(keep: simple(:x)) { { keep: Integer(x) } }
|
23
22
|
rule(reroll: simple(:x)) { { reroll: Integer(x) } }
|
@@ -25,58 +24,47 @@ module DiceBag
|
|
25
24
|
|
26
25
|
# Explode is special, in that if it is nil, then it
|
27
26
|
# must remain that way.
|
28
|
-
rule(explode: simple(:x)) { { explode: (x ? Integer(x) :
|
29
|
-
|
30
|
-
# Parts {:ops => (:xdx | :number)}
|
31
|
-
# These are first-match, so the simple number will
|
32
|
-
# be matched before the xdx subtree.
|
33
|
-
|
34
|
-
# Match an operator followed by a static number.
|
35
|
-
# TODO: find out why this is not matching simple
|
36
|
-
# op => integers! -- 2016-04-18
|
37
|
-
rule(op: simple(:o), value: simple(:v)) do
|
38
|
-
[String(o), Integer(v)]
|
39
|
-
end
|
40
|
-
|
41
|
-
# Match an operator followed by an :xdx subtree.
|
42
|
-
rule(op: simple(:o), value: subtree(:part)) do
|
43
|
-
value = if part.is_a? Hash
|
44
|
-
count = Integer(part[:xdx][:count])
|
45
|
-
sides = Integer(part[:xdx][:sides])
|
46
|
-
options = Transform.hashify_options(part[:options])
|
47
|
-
|
48
|
-
{ xdx: { count: count, sides: sides }, options: options }
|
49
|
-
else
|
50
|
-
Integer(part)
|
51
|
-
end
|
52
|
-
|
53
|
-
[String(o), value]
|
54
|
-
end
|
27
|
+
rule(explode: simple(:x)) { { explode: (x ? Integer(x) : 1) } }
|
55
28
|
|
56
29
|
# Match a label by itself.
|
57
|
-
rule(label: simple(:s)) { [:label, String(s)] }
|
30
|
+
rule(label: simple(:s)) { [:label, LabelPart.new(String(s))] }
|
58
31
|
|
59
32
|
# Match a label followed by a :start subtree.
|
60
33
|
rule(label: simple(:s), start: subtree(:part)) do
|
61
|
-
|
62
|
-
|
63
|
-
|
34
|
+
[
|
35
|
+
[:label, LabelPart.new(String(s))],
|
36
|
+
[:start, part]
|
37
|
+
]
|
64
38
|
end
|
65
39
|
|
66
40
|
# Match a :start subtree, with the label not present.
|
67
|
-
# Note that this returns a hash, but the final output
|
68
|
-
# will still be in an array.
|
69
41
|
rule(start: subtree(:part)) do
|
70
42
|
[:start, part]
|
71
43
|
end
|
72
44
|
|
45
|
+
# Match the xdx and options hash.
|
46
|
+
#
|
47
|
+
# TODO: Remove the .as(:xdx) in the Parser sub-class
|
48
|
+
# and then update this class to account for it. It'll
|
49
|
+
# make the resulting data much cleaner.
|
73
50
|
rule(xdx: subtree(:xdx), options: subtree(:opts)) do
|
74
51
|
{ xdx: xdx, options: Transform.hashify_options(opts) }
|
75
52
|
end
|
76
53
|
|
77
54
|
# Convert the count and sides of an :xdx part.
|
78
55
|
rule(count: simple(:c), sides: simple(:s)) do
|
79
|
-
{ count: Integer(c), sides: Integer(s) }
|
56
|
+
{ count: (c ? Integer(c) : 1), sides: Integer(s) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Match an operator followed by an :xdx subtree.
|
60
|
+
rule(op: simple(:o), value: subtree(:part)) do
|
61
|
+
part[:options] = Transform.hashify_options(part[:options])
|
62
|
+
|
63
|
+
[String(o), part]
|
64
|
+
end
|
65
|
+
|
66
|
+
rule(op: simple(:o), value: simple(:v)) do
|
67
|
+
[String(o), Integer(v)]
|
80
68
|
end
|
81
69
|
end
|
82
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dicebag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SynTruth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|