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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc6f9c347f892aeb166fcae63db74f852f4f78d5
4
- data.tar.gz: 4b719b9fa625bef4c4cc5a9bf531249d1cd366b6
3
+ metadata.gz: a04e07e333e0dbee195dc20264c31791b3df0e30
4
+ data.tar.gz: e142197e3d345460e984497d85daf271468bc8c3
5
5
  SHA512:
6
- metadata.gz: 78bf99b571914d1a28c26f964cdaf23ffbdb1bbb923527bacf1f613785f46b82cbfba283471cc080bdcc53a5c95263550472294ee4e3c22f6e5eaa77d3907769
7
- data.tar.gz: da473d03f4524f4e0824d02b2875b705a2ef41c5254f6ad002ec1816696f2d78e348a5fe1ed3b636199b91587e6d8da4614b5d70f2ec6f2e67eedf03b84d8b03
6
+ metadata.gz: f48eea7ecae33fd45c4f8849b74a627b8cbaf13ef01084320d8cc66397c581501a7852be399a7734f2118562c66e383c5c7d149099722b44387e1744b5b4981a
7
+ data.tar.gz: 98c8bb9b2e6199487db54d8b11a95f11cac4225a2687a54e356b58c31184e497e633226cf921445f13e5f883fedf92c116d72381dd1af4260a3d3a73bade4959
@@ -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.0
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
- else
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(xdx)
116
- return unless xdx[:options].key? :explode
117
-
118
- explode = xdx[:options][:explode]
117
+ def self.normalize_explode(hash)
118
+ return unless hash[:options].key? :explode
119
119
 
120
- if explode.nil? || explode.zero? || explode == 1
121
- xdx[:options][:explode] = xdx[:sides]
120
+ if hash[:options][:explode] == 1
121
+ hash[:options][:explode] = hash[:sides]
122
122
 
123
- xdx[:notes].push("Explode set to #{xdx[:sides]}")
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(xdx)
129
- return unless xdx[:options].key? :reroll
128
+ def self.normalize_reroll(hash)
129
+ return unless hash[:options].key? :reroll
130
130
 
131
- if xdx[:options][:reroll] >= xdx[:sides]
132
- xdx[:options][:reroll] = 0
131
+ if hash[:options][:reroll] >= hash[:sides]
132
+ hash[:options][:reroll] = 0
133
133
 
134
- xdx[:notes].push 'Reroll reset to 0.'
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(xdx)
142
- drop = xdx[:options].fetch(:drop, 0)
143
- keep = xdx[:options].fetch(:keep, 0)
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) >= xdx[:count]
146
- xdx[:options][:drop] = 0
147
- xdx[:options][:keep] = 0
145
+ if (drop + keep) >= hash[:count]
146
+ hash[:options][:drop] = 0
147
+ hash[:options][:keep] = 0
148
148
 
149
- xdx[:notes].push 'Drop and Keep Conflict. Both reset to 0.'
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, make sure it is equal
154
- # to or less than the dice sides and greater than 0, otherwise,
155
- # set it to 0 (aka no target number) and add a note.
156
- def self.normalize_target(xdx)
157
- return unless xdx[:options].key? :target
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 = xdx[:options][:target]
161
+ target = hash[:options][:target]
160
162
 
161
- return if target >= 0 && target <= xdx[:sides]
163
+ return if target >= 0 && target <= hash[:sides]
162
164
 
163
- xdx[:options][:target] = 0
165
+ hash[:options][:target] = 0
164
166
 
165
- xdx[:notes].push 'Target number too large or is negative; reset to 0.'
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.
@@ -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
- # This is handled in RollPart#initialize.
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? }
@@ -14,10 +14,9 @@ module DiceBag
14
14
  opts
15
15
  end
16
16
 
17
- # Option transforms. These are turned into an array of
18
- # 2-element arrays ('tagged arrays'), which is then
19
- # hashified later. (There is no way to update the
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) : nil) } }
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
- label = String(s)
62
-
63
- [[:label, label], [:start, part]]
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.1
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-18 00:00:00.000000000 Z
11
+ date: 2016-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet