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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f932f637949ae902d7a548f3f840bbac83eb889986468e02c149b23800e00b04
4
- data.tar.gz: 21e477659ac97f48eba24053a5a2e07852dbac9f2220a84474d04b4624e96395
3
+ metadata.gz: e374abd6451fc02ee28e17e0a87a2833a785643d89962708903717766c8ad8df
4
+ data.tar.gz: 6e339459b5f5aaa5d2dc5f26f55dc8331e1a976e2da9452f3fc2f2bfd9c9f00f
5
5
  SHA512:
6
- metadata.gz: c7ee0173f907cac2f49d5f7e04750e0ea29b88d1a85ad21adbfa57217c1ab85fd731693e085b8265cdae78b18c1deb96776841ed66f98e90baaef4efce91a7ea
7
- data.tar.gz: a7a1e68d305dcf024506262e63d1f08f12e9042280dadd80f711e6e25b2ec2edb685d6b04f103b464fd5bd5db7a242384d753c2d444b8efb615f6834da3bd53b
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 | --notes] <dice string>'
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 = nonopts.join(' ')
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.each do |_op, part|
24
- if part.is_a?(RollPart) && !part.notes.empty?
25
- arr.push format(fmt, part, part.notes)
26
- end
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
 
@@ -12,7 +12,7 @@ module DiceBag
12
12
  attr_reader :reroll_count
13
13
 
14
14
  def initialize(part)
15
- super part
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
- @total.nil? ? false : true
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
- if options[:reroll].positive?
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
@@ -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 num
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
@@ -50,7 +50,7 @@ class D20Advantage < D20
50
50
  r1 = @roll.roll
51
51
  r2 = @roll.roll
52
52
 
53
- r1 > r2 ? r1 : r2
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 < r2 ? r1 : r2
63
+ [r1, r2].min
64
64
  end
65
65
  end
@@ -46,9 +46,7 @@ module Fudge
46
46
  def to_s
47
47
  base = "#{@number}dF"
48
48
 
49
- "#{base} #{tally_to_s} => #{@total}" if @total
50
-
51
- base
49
+ @total ? "#{base} #{tally_to_s} => #{@total}" : base
52
50
  end
53
51
 
54
52
  private
data/lib/dicebag.rb CHANGED
@@ -15,11 +15,26 @@ module DiceBag
15
15
  Normalize.call ast
16
16
  end
17
17
 
18
- ### Main Syntatic Sugar Interface
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 './dicebag/normalize'
31
- require_relative './dicebag/roll_string'
32
- require_relative './dicebag/roll_part_string'
33
- require_relative './dicebag/parser'
34
- require_relative './dicebag/transform'
35
- require_relative './dicebag/simple_part'
36
- require_relative './dicebag/label_part'
37
- require_relative './dicebag/static_part'
38
- require_relative './dicebag/roll_part'
39
- require_relative './dicebag/roll'
40
- require_relative './dicebag/result'
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.2
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: 2016-04-25 00:00:00.000000000 Z
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: