randsum 1.0.3 → 1.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b40795441292e1f022b9964709ae9b1a1440b8d2
4
- data.tar.gz: e2403d1ed04af388474c6fb24ee69b1065bc2848
3
+ metadata.gz: c3a741642aa60b86f2c5f085ccce024f09606a29
4
+ data.tar.gz: dc30d622c0b53c70bae39a1ce4b6f15128b235dc
5
5
  SHA512:
6
- metadata.gz: 55770ea57631fe9e4d1297b78b824d45067b5b1b52113201491cf72f901e03bb5ce5096be5dd90b5650556fc1fd140a5208bb2e14e65f29d912aa8d75dc4230e
7
- data.tar.gz: 6f400f76cb73c5ce4cbc651941646f9cd66dc86c2afeb697e5b95602263c541394d81ce95cc3e6ce1faf4432d49e6c6a264a8e171cbe3dfad7153d198188760a
6
+ metadata.gz: 80dcc04a112b364b44deea35593bf7eb6df2bd63846c82e93da8f7b32373bf68692a9712591035f1d2cb0ab4ada315922ca48e3a4691714b7a9789971b48f97e
7
+ data.tar.gz: db464e7cc91dd7a58d1100a8d117adb6440f428b6eb04773d596d8a53bd514436443379d066a4da8ccb9aeb0fc2b462e17b80386d51a83a4075c9f9f8cb19a46
data/README.md CHANGED
@@ -89,75 +89,166 @@ Snakeyes. Rough.
89
89
  `Die#roll` returns a `Roll` object, which can teach you a lot about your rolls (but not, tragically, how to roll better.)
90
90
 
91
91
  ```
92
- > report = Randsum::D20.roll 5
92
+ > roll = Randsum::D20.roll 5
93
93
  #=> You rolled 5 d20, and got a 61. (Rolls: [20, 16, 9, 14, 1])
94
94
  ```
95
95
 
96
96
  The `#total` method represents the sum total of the dice that were rolled:
97
97
 
98
98
  ```
99
- > report.total
99
+ > roll.total
100
100
  #=> 61
101
101
  ```
102
102
 
103
103
  The `#rolls` array, which reports the individual results of any dice rolled:
104
104
 
105
105
  ```
106
- > report.rolls
106
+ > roll.rolls
107
107
  #=> [20, 17, 9, 14,1]
108
108
  ```
109
109
 
110
110
  `#quantity` tells you how many dice were rolled this time:
111
111
 
112
112
  ```
113
- > report.quantity
113
+ > roll.quantity
114
114
  #=> 5
115
115
  ```
116
116
 
117
117
  `#sides` reports the number of sides on the dice rolled in this result:
118
118
 
119
119
  ```
120
- > report.sides
120
+ > roll.sides
121
121
  #=> 20
122
122
  ```
123
123
 
124
124
  And if you don't like that roll (hey we get it) you can use `#die` to get another of the same die to roll again!
125
125
 
126
126
  ```
127
- > report.die.roll
127
+ > roll.die.roll
128
128
  #=> You rolled 1 D20, and got 17. (Rolls: [17])
129
129
  ```
130
130
 
131
131
  #### Manipulating `Roll`s
132
132
 
133
- Roll results can be further manipulated after their original creation. To facilitate popular use-cases for Dice rolling, `Roll`s also include public `#drop_lowest` and `#drop_highest`, and `#drop` methods.
133
+ `Roll`s can be directly created, using the `.roll` class method. Get ready to see the word `Roll` a lot below; its become meaningless to me at this point.
134
+
135
+ ```
136
+ > Randsum::Roll.roll 2 d:20
137
+ #=> You rolled 2 d20, and got 25. (Rolls: [12, 13])
138
+ ```
139
+
140
+
141
+ ##### Dropping Rolls
142
+ `Roll`s also include public `#drop_lowest` and `#drop_highest`, and `#drop` methods.
134
143
 
135
144
  `#drop_lowest` returns a new `Roll` without the lowest numerical die roll.
136
145
 
137
146
  ```
138
- > report = Randsum::D6.roll 4
147
+ > roll = Randsum::D6.roll 4
139
148
  #=> You rolled 4 d6, and got 11. (Rolls: [3, 2, 2, 4])
140
149
 
141
- > new_report = report.drop_lowest
142
- #=> You rolled 3 d6, and got 9. (Rolls: [4, 3, 2])
150
+ > roll.drop_lowest
151
+ #=> You rolled 3 d6, and got 9. (Rolls: [4, 3, 2])
143
152
  ```
144
153
 
145
154
  Similarly, `#drop_highest` will remove the highest number in the `rolls` array.
146
155
 
147
156
  ```
148
- > new_result = report.drop_highest
157
+ > new_roll = roll.drop_highest
149
158
  #=> You rolled 3 d6, and got 7. (Rolls: [2, 2, 3])
150
159
  ```
151
160
 
152
161
  Both `#drop_lowest` and `#drop_highest` can also take an optional integer argument.
153
162
 
154
163
  ```
155
- > result.drop_highest(2)
164
+ > roll.drop_highest(2)
156
165
  #=> You rolled 2 d6, and got 4. (Rolls: [2, 2])
157
166
  ```
167
+ `#drop` works as a catch-all for these, using keyword arguments:
158
168
 
159
- ### WHY IS THIS SO COMPLICATED
169
+ ```
170
+ > roll.drop(extremity: :highest, quantity: 2)
171
+ #=> You rolled 2 d6, and got 4. (Rolls: [2, 2])
172
+ ```
173
+
174
+ ##### Replacing Values
175
+ `Roll`s have a number of ways to further manipulate the rolls inside.
176
+
177
+ ```
178
+ > roll = Randsum::D20.roll(3)
179
+ #=> You rolled 3 d20, and got 35. (Rolls: [14, 12, 9])
180
+ ```
181
+
182
+ You can use the `#reroll` to completely trash the rolls you had and start again.
183
+ ```
184
+ > roll.reroll
185
+ #=> You rolled 3 d20, and got 44. (Rolls: [19, 11, 14])
186
+ ```
187
+
188
+ `#double_all` takes all instances of a value in the roll and - you guess it - doubles it.
189
+ ```
190
+ > roll.double_all(9)
191
+ #=> You rolled 3 d20, and got 44. (Rolls: [14, 12, 18])
192
+ ```
193
+
194
+ The catch-all `#replace` method takes a number of arguments that change its behavior:
195
+ - the `target:` keyword can be:
196
+
197
+ - an integer (what you want to replace) or
198
+ ```
199
+ > roll.replace(target: 9, with: 20)
200
+ #=> You rolled 3 d20, and got 46. (Rolls: [14, 12, 20])
201
+ ```
202
+ - the symbol `:all` (to replace all of the rolls)
203
+ ```
204
+ > roll.replace(target: :all, with: 20)
205
+ #=> You rolled 3 d20, and got 60. (Rolls: [20, 20, 20])
206
+ ```
207
+
208
+ - the `with:` keyword:
209
+ - an integer (what you want the target to be replaced by)
210
+ ```
211
+ > roll.replace(target: 9, with: 20)
212
+ #=> You rolled 3 d20, and got 46. (Rolls: [14, 12, 20])
213
+ ```
214
+ - the symbol `:reroll` (to replace the targets with a new roll using the same die)
215
+ ```
216
+ > roll.replace(target: 9, with: :reroll)
217
+ #=> You rolled 3 d20, and got 28. (Rolls: [14, 12, 2])
218
+ ```
219
+ - the symbol `:double` (to double the value of the targets)
220
+ ```
221
+ > roll.replace(target: 9, with: :double)
222
+ #=> You rolled 3 d20, and got 44. (Rolls: [14, 12, 18])
223
+ ```
224
+
225
+ #### Checking!
226
+ Need to see if a value meet or beat another value? Well, beans. We've got that, too.
227
+
228
+ ```
229
+ > roll = Randsum::D20.roll(3)
230
+ #=> You rolled 3 d20, and got 22. (Rolls: [20, 1, 1])
231
+
232
+ > roll.beats?(20)
233
+ #=> true
160
234
 
235
+ > roll.beats?(22)
236
+ #=> false
237
+
238
+ > roll.beats?(25)
239
+ #=> false
240
+
241
+ > roll.meets?(20)
242
+ #=> true
243
+
244
+ > roll.meets?(22)
245
+ #=> true
246
+
247
+ > roll.meets?(25)
248
+ #=> false
249
+ ```
250
+
251
+ ### WHY IS THIS SO COMPLICATED
161
252
  `Die#simple_roll` will just give you a random number, but where's the fun in that?
162
253
 
163
254
  ### Why build this?
@@ -1,6 +1,11 @@
1
1
  require "randsum/version"
2
2
  require "randsum/metadata"
3
- require "randsum/filters/filter"
3
+ require "randsum/filters/replacers/replacer"
4
+ require "randsum/filters/replacers/replacer_targets/replacer_target"
5
+ require "randsum/filters/replacers/replacer_targets/replacer_target_all"
6
+ require "randsum/filters/replacers/replacer_values/replacer_value"
7
+ require "randsum/filters/replacers/replacer_values/replacer_reroll_value"
8
+ require "randsum/filters/replacers/replacer_values/replacer_double_value"
4
9
  require "randsum/filters/droppers/dropper"
5
10
  require "randsum/filters/droppers/high_dropper"
6
11
  require "randsum/filters/droppers/low_dropper"
@@ -1,23 +1,38 @@
1
1
  module Randsum
2
- class Dropper < Filter
2
+ class Dropper
3
+ attr_reader :quantity, :roll
3
4
 
4
- def self.dropper_for(quantity:,extremity:, rolls:)
5
+ def self.for(quantity:, extremity:, roll:)
5
6
  Object.const_get(
6
7
  "Randsum::#{extremity.to_s.gsub("est","").capitalize}Dropper"
7
- ).new(quantity: quantity, rolls: rolls)
8
+ ).new(quantity: quantity, roll: roll)
9
+ end
10
+
11
+ def initialize(quantity:, roll:)
12
+ @quantity = quantity
13
+ @roll = roll
8
14
  end
9
15
 
10
16
  def filter
11
- ordered.first(remainder)
17
+ return Randsum::Roll.new(
18
+ die: roll.die,
19
+ quantity: roll.quantity,
20
+ result: result
21
+ )
12
22
  end
13
23
 
14
24
  def ordered
15
25
  raise NotImplementedError
16
26
  end
17
27
 
18
- def remainder
19
- rolls.length - quantity
28
+ private
29
+
30
+ def result
31
+ ordered.first(remainder)
20
32
  end
21
33
 
34
+ def remainder
35
+ roll.length - quantity
36
+ end
22
37
  end
23
38
  end
@@ -1,7 +1,7 @@
1
1
  module Randsum
2
2
  class HighDropper < Dropper
3
3
  def ordered
4
- rolls.sort { |a, b| a <=> b }
4
+ roll.result.sort { |a, b| a <=> b }
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module Randsum
2
2
  class LowDropper < Dropper
3
3
  def ordered
4
- rolls.sort { |a, b| b <=> a }
4
+ roll.result.sort { |a, b| b <=> a }
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,35 @@
1
+ module Randsum
2
+ class Replacer
3
+ attr_reader :target, :with, :roll
4
+
5
+ def self.for(target:, with:, roll:)
6
+ Randsum::Replacer.new(
7
+ target: target,
8
+ with: with,
9
+ roll: roll,
10
+ )
11
+ end
12
+
13
+ def initialize(target:, with:, roll:)
14
+ @target = ReplacerTarget.for(target, roll: roll)
15
+ @with = ReplacerValue.for(with, roll: roll)
16
+ @roll = roll
17
+ end
18
+
19
+ def filter
20
+ return Randsum::Roll.new(
21
+ die: roll.die,
22
+ quantity: roll.quantity,
23
+ result: result
24
+ )
25
+ end
26
+
27
+ def result
28
+ (roll.result - target.match) + transformed
29
+ end
30
+
31
+ def transformed
32
+ with.transform(target.match)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module Randsum
2
+ class ReplacerTarget
3
+ ALL = :all
4
+ attr_reader :target, :roll
5
+
6
+ def self.for(target, roll:)
7
+ case target
8
+ when ALL
9
+ ReplacerTargetAll
10
+ when nil
11
+ ReplacerTargetAll
12
+ else
13
+ ReplacerTarget
14
+ end.new(target, roll: roll)
15
+ end
16
+
17
+ def initialize(target, roll:)
18
+ @target = target
19
+ @roll = roll
20
+ end
21
+
22
+ def match
23
+ roll.result.select do |r|
24
+ r == target
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module Randsum
2
+ class ReplacerTargetAll < ReplacerTarget
3
+ def match
4
+ roll.result
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Randsum
2
+ class ReplacerDoubleValue < ReplacerValue
3
+ def transform(match)
4
+ match.map { |r| r * 2 }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Randsum
2
+ class ReplacerRerollValue < ReplacerValue
3
+ def transform(match)
4
+ match.map { roll.die.simple_roll }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ module Randsum
2
+ class ReplacerValue
3
+ REROLL = :reroll
4
+ DOUBLE = :double
5
+
6
+ attr_reader :roll, :value
7
+
8
+ def self.for(value, roll:)
9
+ case value
10
+ when REROLL
11
+ ReplacerRerollValue
12
+ when DOUBLE
13
+ ReplacerDoubleValue
14
+ else
15
+ ReplacerValue
16
+ end.new(value, roll: roll)
17
+ end
18
+
19
+ def initialize(value, roll:)
20
+ @value = value
21
+ @roll = roll
22
+ end
23
+
24
+ def transform(match)
25
+ match.map { value }
26
+ end
27
+ end
28
+ end
@@ -7,6 +7,10 @@ module Randsum
7
7
  alias_method :count, :quantity
8
8
  alias_method :rolls, :result
9
9
 
10
+ def self.roll(num, d:)
11
+ new(die: Die.new(d), quantity: num)
12
+ end
13
+
10
14
  def initialize(die:, quantity:, result: nil)
11
15
  @die = die
12
16
  @quantity = quantity
@@ -24,14 +28,44 @@ module Randsum
24
28
  end
25
29
  alias_method :to_i, :total
26
30
 
31
+ def beats?(check_value)
32
+ total > check_value
33
+ end
34
+
35
+ def meets?(meet_value)
36
+ total >= meet_value
37
+ end
38
+
39
+ def replace(target, with:)
40
+ Replacer.for(
41
+ target: target,
42
+ with: with,
43
+ roll: self
44
+ ).filter
45
+ end
46
+
47
+ def double_all(target)
48
+ Replacer.for(
49
+ target: target,
50
+ with: ReplacerValue::DOUBLE,
51
+ roll: self
52
+ ).filter
53
+ end
54
+
55
+ def reroll
56
+ Replacer.for(
57
+ target: ReplacerTarget::ALL,
58
+ with: ReplacerValue::REROLL,
59
+ roll: self
60
+ ).filter
61
+ end
62
+
27
63
  def drop(quantity:,extremity:)
28
- return new_roll_with(
29
- result: Dropper.dropper_for(
30
- quantity: quantity,
31
- extremity: extremity,
32
- rolls: result
33
- ).filter
34
- )
64
+ Dropper.for(
65
+ quantity: quantity,
66
+ extremity: extremity,
67
+ roll: self
68
+ ).filter
35
69
  end
36
70
 
37
71
  def drop_lowest(quantity = 1)
@@ -44,14 +78,6 @@ module Randsum
44
78
 
45
79
  private
46
80
 
47
- def new_roll_with(result: nil)
48
- return Roll.new(
49
- die: die,
50
- quantity: quantity,
51
- result: result
52
- )
53
- end
54
-
55
81
  def roll!
56
82
  (1..quantity).map { die.simple_roll }
57
83
  end
@@ -1,3 +1,3 @@
1
1
  module Randsum
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randsum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Jarvis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-05 00:00:00.000000000 Z
11
+ date: 2016-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,12 @@ files:
94
94
  - lib/randsum/filters/droppers/dropper.rb
95
95
  - lib/randsum/filters/droppers/high_dropper.rb
96
96
  - lib/randsum/filters/droppers/low_dropper.rb
97
- - lib/randsum/filters/filter.rb
97
+ - lib/randsum/filters/replacers/replacer.rb
98
+ - lib/randsum/filters/replacers/replacer_targets/replacer_target.rb
99
+ - lib/randsum/filters/replacers/replacer_targets/replacer_target_all.rb
100
+ - lib/randsum/filters/replacers/replacer_values/replacer_double_value.rb
101
+ - lib/randsum/filters/replacers/replacer_values/replacer_reroll_value.rb
102
+ - lib/randsum/filters/replacers/replacer_values/replacer_value.rb
98
103
  - lib/randsum/metadata.rb
99
104
  - lib/randsum/roll.rb
100
105
  - lib/randsum/version.rb
@@ -1,14 +0,0 @@
1
- module Randsum
2
- class Filter
3
- attr_reader :quantity, :rolls
4
-
5
- def initialize(quantity:, rolls:)
6
- @quantity = quantity
7
- @rolls = rolls
8
- end
9
-
10
- def filter
11
- raise NotImplementedError
12
- end
13
- end
14
- end