dicebag 3.3.2 → 3.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/dicebag +30 -2
- data/lib/dicebag/min_max_calc.rb +59 -0
- data/lib/dicebag/roll.rb +17 -4
- data/lib/dicebag/roll_part.rb +17 -5
- data/lib/dicebag/static_part.rb +6 -1
- data/lib/dicebag/systems/dnd.rb +2 -2
- data/lib/dicebag/systems/fudge.rb +1 -3
- data/lib/dicebag.rb +28 -12
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e374abd6451fc02ee28e17e0a87a2833a785643d89962708903717766c8ad8df
|
4
|
+
data.tar.gz: 6e339459b5f5aaa5d2dc5f26f55dc8331e1a976e2da9452f3fc2f2bfd9c9f00f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8f484c21149db59bec30f3c3935aeeb1f39001a4ab3c746dec41d1c87d7bfab2bef4c8c3ba5a70ed2efff6b598cb50811185b393b94941cbcee00d25991f080
|
7
|
+
data.tar.gz: e1c68214264adaf8b3023f4ae34309c862ba4fe707d562971eb73461bd866f1319106c4e9cdc7014f0762c72fff665477d3ad06f2ddec7877ae043fa8c34bf0f
|
data/bin/dicebag
CHANGED
@@ -7,6 +7,7 @@ require_relative '../lib/dicebag'
|
|
7
7
|
# Define the Dicebag CLI app
|
8
8
|
class DiceBagCLI
|
9
9
|
attr_reader :params
|
10
|
+
attr_reader :dstr
|
10
11
|
|
11
12
|
def self.call
|
12
13
|
new.perform
|
@@ -18,7 +19,13 @@ class DiceBagCLI
|
|
18
19
|
|
19
20
|
def opts
|
20
21
|
@opts ||= OptionParser.new do |args|
|
21
|
-
args.banner = 'Usage: dicebag [-n
|
22
|
+
args.banner = 'Usage: dicebag [-n|--notes] [--avg|--max|--min] <string>'
|
23
|
+
|
24
|
+
args.on '--avg', 'Display the average for the roll.'
|
25
|
+
|
26
|
+
args.on '--max', 'Display the maximum for the roll.'
|
27
|
+
|
28
|
+
args.on '--min', 'Display the minimum for the roll.'
|
22
29
|
|
23
30
|
args.on '-n', '--notes', 'Display any notes for the roll.'
|
24
31
|
|
@@ -35,7 +42,28 @@ class DiceBagCLI
|
|
35
42
|
|
36
43
|
nonopts = opts.parse! into: params
|
37
44
|
|
38
|
-
dstr
|
45
|
+
@dstr = nonopts.join(' ')
|
46
|
+
|
47
|
+
return do_average if params[:avg]
|
48
|
+
return do_maximum if params[:max]
|
49
|
+
return do_minimum if params[:min]
|
50
|
+
|
51
|
+
do_roll
|
52
|
+
end
|
53
|
+
|
54
|
+
def do_average
|
55
|
+
puts DiceBag.average dstr
|
56
|
+
end
|
57
|
+
|
58
|
+
def do_maximum
|
59
|
+
puts DiceBag.maximum dstr
|
60
|
+
end
|
61
|
+
|
62
|
+
def do_minimum
|
63
|
+
puts DiceBag.minimum dstr
|
64
|
+
end
|
65
|
+
|
66
|
+
def do_roll
|
39
67
|
roll = DiceBag::Roll.new dstr
|
40
68
|
result = roll.roll
|
41
69
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module DiceBag
|
2
|
+
# This is service class to calculate the Maximum/Minimum for the given
|
3
|
+
# Roll.
|
4
|
+
class MinMaxCalc
|
5
|
+
attr_reader :roll
|
6
|
+
attr_reader :method
|
7
|
+
attr_reader :opposite
|
8
|
+
|
9
|
+
def self.average(roll)
|
10
|
+
(maximum(roll) + minimum(roll)) / 2.0
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.maximum(roll)
|
14
|
+
call roll, :maximum
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.minimum(roll)
|
18
|
+
call roll, :minimum
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.call(roll, method)
|
22
|
+
new(roll, method).perform
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(roll, method)
|
26
|
+
@roll = roll
|
27
|
+
@method = method
|
28
|
+
@opposite = opposite_method
|
29
|
+
@total = 0
|
30
|
+
end
|
31
|
+
|
32
|
+
def perform
|
33
|
+
roll.tree.each do |op, part|
|
34
|
+
next unless part.is_a?(RollPart) || part.is_a?(StaticPart)
|
35
|
+
|
36
|
+
calculate op, part
|
37
|
+
end
|
38
|
+
|
39
|
+
@total
|
40
|
+
end
|
41
|
+
|
42
|
+
# Update the total based on the oper. For 'additive' operations, we
|
43
|
+
# use the method, but if it's 'subtractive' then it uses the
|
44
|
+
# opposite method.
|
45
|
+
def calculate(oper, part)
|
46
|
+
case oper
|
47
|
+
when :start then @total = part.send(method)
|
48
|
+
when :add then @total += part.send(method)
|
49
|
+
when :sub then @total -= part.send(opposite)
|
50
|
+
when :mul then @total *= part.send(method)
|
51
|
+
when :div then @total /= part.send(opposite)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def opposite_method
|
56
|
+
method == :maximum ? :minimum : :maximum
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/dicebag/roll.rb
CHANGED
@@ -20,10 +20,10 @@ module DiceBag
|
|
20
20
|
arr = []
|
21
21
|
fmt = "For %s: %s\n"
|
22
22
|
|
23
|
-
tree.
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
tree.each_value do |part|
|
24
|
+
next unless part.is_a?(RollPart) && !part.notes.empty?
|
25
|
+
|
26
|
+
arr.push format(fmt, part, part.notes)
|
27
27
|
end
|
28
28
|
|
29
29
|
arr
|
@@ -51,12 +51,25 @@ module DiceBag
|
|
51
51
|
@result = Result.new(@label, @total, @sections)
|
52
52
|
end
|
53
53
|
|
54
|
+
def average
|
55
|
+
MinMaxCalc.average self
|
56
|
+
end
|
57
|
+
|
58
|
+
def maximum
|
59
|
+
MinMaxCalc.maximum self
|
60
|
+
end
|
61
|
+
|
62
|
+
def minimum
|
63
|
+
MinMaxCalc.minimum self
|
64
|
+
end
|
65
|
+
|
54
66
|
private
|
55
67
|
|
56
68
|
def handle_tree
|
57
69
|
tree.each do |op, part|
|
58
70
|
if op == :label
|
59
71
|
@label = part.value
|
72
|
+
|
60
73
|
next
|
61
74
|
end
|
62
75
|
|
data/lib/dicebag/roll_part.rb
CHANGED
@@ -12,7 +12,7 @@ module DiceBag
|
|
12
12
|
attr_reader :reroll_count
|
13
13
|
|
14
14
|
def initialize(part)
|
15
|
-
super
|
15
|
+
super(part)
|
16
16
|
|
17
17
|
@total = nil
|
18
18
|
@tally = []
|
@@ -44,7 +44,7 @@ module DiceBag
|
|
44
44
|
|
45
45
|
# Checks to see if this instance has rolled yet or not.
|
46
46
|
def rolled?
|
47
|
-
|
47
|
+
!@total.nil?
|
48
48
|
end
|
49
49
|
|
50
50
|
def roll
|
@@ -87,6 +87,18 @@ module DiceBag
|
|
87
87
|
@total
|
88
88
|
end
|
89
89
|
|
90
|
+
def average
|
91
|
+
(minimum + maximum) / 2.0
|
92
|
+
end
|
93
|
+
|
94
|
+
def maximum
|
95
|
+
count * sides
|
96
|
+
end
|
97
|
+
|
98
|
+
def minimum
|
99
|
+
count
|
100
|
+
end
|
101
|
+
|
90
102
|
def <=>(other)
|
91
103
|
total <=> other.total
|
92
104
|
end
|
@@ -111,10 +123,10 @@ module DiceBag
|
|
111
123
|
def roll_die
|
112
124
|
num = __roll_die
|
113
125
|
|
126
|
+
return num unless options[:reroll].positive?
|
127
|
+
|
114
128
|
# Handle Reroll
|
115
|
-
|
116
|
-
num = __roll_die while num <= @options[:reroll]
|
117
|
-
end
|
129
|
+
num = __roll_die while num <= @options[:reroll]
|
118
130
|
|
119
131
|
num
|
120
132
|
end
|
data/lib/dicebag/static_part.rb
CHANGED
@@ -5,13 +5,18 @@ module DiceBag
|
|
5
5
|
def initialize(num)
|
6
6
|
num = num.to_i if num.is_a?(String)
|
7
7
|
|
8
|
-
super
|
8
|
+
super(num)
|
9
9
|
end
|
10
10
|
|
11
11
|
def total
|
12
12
|
value
|
13
13
|
end
|
14
14
|
|
15
|
+
# These are all just the same for this part.
|
16
|
+
alias average total
|
17
|
+
alias maximum total
|
18
|
+
alias minimum total
|
19
|
+
|
15
20
|
def to_s
|
16
21
|
value.to_s
|
17
22
|
end
|
data/lib/dicebag/systems/dnd.rb
CHANGED
@@ -50,7 +50,7 @@ class D20Advantage < D20
|
|
50
50
|
r1 = @roll.roll
|
51
51
|
r2 = @roll.roll
|
52
52
|
|
53
|
-
r1
|
53
|
+
[r1, r2].max
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -60,6 +60,6 @@ class D20Disadvantage < D20
|
|
60
60
|
r1 = @roll.roll
|
61
61
|
r2 = @roll.roll
|
62
62
|
|
63
|
-
r1
|
63
|
+
[r1, r2].min
|
64
64
|
end
|
65
65
|
end
|
data/lib/dicebag.rb
CHANGED
@@ -15,11 +15,26 @@ module DiceBag
|
|
15
15
|
Normalize.call ast
|
16
16
|
end
|
17
17
|
|
18
|
-
###
|
18
|
+
###
|
19
|
+
# Main Syntatic Sugar Interface Methodds
|
20
|
+
###
|
21
|
+
|
19
22
|
def self.roll(dstr)
|
20
23
|
DiceBag::Roll.new(dstr).roll
|
21
24
|
end
|
22
25
|
|
26
|
+
def self.average(dstr)
|
27
|
+
DiceBag::Roll.new(dstr).average
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.maximum(dstr)
|
31
|
+
DiceBag::Roll.new(dstr).maximum
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.minimum(dstr)
|
35
|
+
DiceBag::Roll.new(dstr).minimum
|
36
|
+
end
|
37
|
+
|
23
38
|
# The default roll if one is not given.
|
24
39
|
def self.default_roll
|
25
40
|
'1d6'
|
@@ -27,14 +42,15 @@ module DiceBag
|
|
27
42
|
end
|
28
43
|
|
29
44
|
# Our sub-modules.
|
30
|
-
require_relative '
|
31
|
-
require_relative '
|
32
|
-
require_relative '
|
33
|
-
require_relative '
|
34
|
-
require_relative '
|
35
|
-
require_relative '
|
36
|
-
require_relative '
|
37
|
-
require_relative '
|
38
|
-
require_relative '
|
39
|
-
require_relative '
|
40
|
-
require_relative '
|
45
|
+
require_relative 'dicebag/normalize'
|
46
|
+
require_relative 'dicebag/min_max_calc'
|
47
|
+
require_relative 'dicebag/roll_string'
|
48
|
+
require_relative 'dicebag/roll_part_string'
|
49
|
+
require_relative 'dicebag/parser'
|
50
|
+
require_relative 'dicebag/transform'
|
51
|
+
require_relative 'dicebag/simple_part'
|
52
|
+
require_relative 'dicebag/label_part'
|
53
|
+
require_relative 'dicebag/static_part'
|
54
|
+
require_relative 'dicebag/roll_part'
|
55
|
+
require_relative 'dicebag/roll'
|
56
|
+
require_relative 'dicebag/result'
|
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.3.
|
4
|
+
version: 3.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Randy "syntruth" Carnahan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- bin/dicebag
|
41
41
|
- lib/dicebag.rb
|
42
42
|
- lib/dicebag/label_part.rb
|
43
|
+
- lib/dicebag/min_max_calc.rb
|
43
44
|
- lib/dicebag/normalize.rb
|
44
45
|
- lib/dicebag/parser.rb
|
45
46
|
- lib/dicebag/result.rb
|
@@ -59,7 +60,8 @@ files:
|
|
59
60
|
homepage: https://github.com/syntruth/Dice-Bag
|
60
61
|
licenses:
|
61
62
|
- MIT
|
62
|
-
metadata:
|
63
|
+
metadata:
|
64
|
+
rubygems_mfa_required: 'true'
|
63
65
|
post_install_message:
|
64
66
|
rdoc_options: []
|
65
67
|
require_paths:
|