nerd_dice 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NerdDice
4
+ # The NerdDice::SetsRandomizationTechnique is a module mixin that can be included
5
+ # in classes. It provides an attribute reader and writer for randomization_technique
6
+ # and checks against the NerdDice::RANDOMIZATION_TECHNIQUES constant to make sure the
7
+ # input provided is valid
8
+ module SetsRandomizationTechnique
9
+ attr_reader :randomization_technique
10
+
11
+ def randomization_technique=(new_value)
12
+ unless RANDOMIZATION_TECHNIQUES.include?(new_value) || new_value.nil?
13
+ raise NerdDice::Error, "randomization_technique must be one of #{NerdDice::RANDOMIZATION_TECHNIQUES.join(', ')}"
14
+ end
15
+
16
+ @randomization_technique = new_value
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NerdDice
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/nerd_dice.rb CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  require "nerd_dice/version"
4
4
  require "nerd_dice/configuration"
5
+ require "nerd_dice/sets_randomization_technique"
6
+ require "nerd_dice/die"
7
+ require "nerd_dice/dice_set"
8
+ require "nerd_dice/class_methods"
5
9
  require "securerandom"
6
10
  # Nerd dice allows you to roll polyhedral dice and add bonuses as you would in
7
11
  # a tabletop roleplaying game. You can choose to roll multiple dice and keep a
@@ -9,168 +13,14 @@ require "securerandom"
9
13
  # ability scores or rolling with advantage and disadvantage if those mechanics
10
14
  # exist in your game.
11
15
  #
12
- # Usage:
13
- # Right now there is only a single class method :total_dice
16
+ # This module is broken down into multiple source files:
17
+ # The class_methods file has all of the module_level methods called by NerdDice.method_name
14
18
  #
15
- # If you wanted to roll a single d4, you would execute:
16
- # <tt>NerdDice.total_dice(4)</tt>
17
- #
18
- # If you wanted to roll 3d6, you would execute
19
- # <tt>NerdDice.total_dice(6, 3)</tt>
20
- #
21
- # If you wanted to roll a d20 and add 5 to the value, you would execute
22
- # <tt>NerdDice.total_dice(20, 1, { bonus: 5 })</tt>
23
- #
24
- # The bonus in the options hash must be an Integer or it will be ignored
19
+ # See the README for overall usage for the module
25
20
  module NerdDice
26
21
  class Error < StandardError; end
27
22
 
28
23
  RANDOMIZATION_TECHNIQUES = %i[securerandom random_rand random_object randomized].freeze
29
-
30
- class << self
31
- attr_reader :count_since_last_refresh
32
-
33
- ############################
34
- # configure class method
35
- ############################
36
- # Arguments: None
37
- # Expects and yields to a block where configuration is specified.
38
- # See README and NerdDice::Configuration class for config options
39
- # Return (NerdDice::Configuration) the Configuration object tied to the
40
- # @configuration class instance variable
41
- def configure
42
- yield configuration
43
- configuration
44
- end
45
-
46
- ############################
47
- # configuration class method
48
- ############################
49
- # Arguments: None
50
- # Provides the lazy-loaded class instance variable @configuration
51
- # Return (NerdDice::Configuration) the Configuration object tied to the
52
- # @configuration class instance variable
53
- def configuration
54
- @configuration ||= Configuration.new
55
- end
56
-
57
- ############################
58
- # total_dice class method
59
- ############################
60
- # Arguments:
61
- # number_of_sides (Integer) => the number of sides of the dice to roll
62
- # number_of_dice (Integer, DEFAULT: 1) => the quantity to roll of the type
63
- # of die specified in the number_of_sides argument.
64
- # options (Hash, DEFAULT: {}) any additional options you wish to include
65
- # :bonus (Integer) => The total bonus (positive integer) or penalty
66
- # (negative integer) to modify the total by. Is added to the total of
67
- # all dice after they are totaled, not to each die rolled
68
- #
69
- # Return (Integer) => Total of the dice rolled, plus modifier if applicable
70
- def total_dice(number_of_sides, number_of_dice = 1, **opts)
71
- total = 0
72
- number_of_dice.times do
73
- total += execute_die_roll(number_of_sides, opts[:randomization_technique])
74
- end
75
- begin
76
- total += opts[:bonus].to_i
77
- rescue NoMethodError
78
- raise ArgumentError, "Bonus must be a value that responds to :to_i"
79
- end
80
- total
81
- end
82
-
83
- ############################
84
- # execute_die_roll class method
85
- ############################
86
- # Arguments:
87
- # number_of_sides (Integer) => the number of sides of the die to roll
88
- # using_generator (Symbol) => must be one of the symbols in
89
- # RANDOMIZATION_TECHNIQUES or nil
90
- #
91
- # Return (Integer) => Value of the single die rolled
92
- def execute_die_roll(number_of_sides, using_generator = nil)
93
- @count_since_last_refresh ||= 0
94
- gen = get_number_generator(using_generator)
95
- result = gen.rand(number_of_sides) + 1
96
- increment_and_evalutate_refresh_seed
97
- result
98
- end
99
-
100
- ############################
101
- # refresh_seed! class method
102
- ############################
103
- # Options: (none required)
104
- # randomization_technique (Symbol) => must be one of the symbols in
105
- # RANDOMIZATION_TECHNIQUES if specified
106
- # random_rand_seed (Integer) => Seed to set for Random
107
- # random_object_seed (Integer) => Seed to set for new Random object
108
- # Return (Hash or nil) => Previous values of generator seeds that were refreshed
109
- def refresh_seed!(**opts)
110
- technique, random_rand_new_seed, random_object_new_seed = parse_refresh_options(opts)
111
- @count_since_last_refresh = 0
112
- return nil if technique == :securerandom
113
-
114
- reset_appropriate_seeds!(technique, random_rand_new_seed, random_object_new_seed)
115
- end
116
-
117
- private
118
-
119
- def get_number_generator(using_generator = nil)
120
- using_generator ||= configuration.randomization_technique
121
- case using_generator
122
- when :securerandom then SecureRandom
123
- when :random_rand then Random
124
- when :random_object then @random_object ||= Random.new
125
- when :randomized then random_generator
126
- else raise ArgumentError, "Unrecognized generator. Must be one of #{RANDOMIZATION_TECHNIQUES.join(', ')}"
127
- end
128
- end
129
-
130
- def random_generator
131
- gen = RANDOMIZATION_TECHNIQUES.reject { |el| el == :randomized }.sample
132
- get_number_generator(gen)
133
- end
134
-
135
- def refresh_random_rand_seed!(new_seed)
136
- new_seed ? Random.srand(new_seed) : Random.srand
137
- end
138
-
139
- def refresh_random_object_seed!(new_seed)
140
- old_seed = @random_object&.seed
141
- @random_object = new_seed ? Random.new(new_seed) : Random.new
142
- old_seed
143
- end
144
-
145
- def parse_refresh_options(opts)
146
- [
147
- opts[:randomization_technique] || configuration.randomization_technique,
148
- opts[:random_rand_seed],
149
- opts[:random_object_seed]
150
- ]
151
- end
152
-
153
- # rubocop:disable Metrics/MethodLength
154
- def reset_appropriate_seeds!(technique, random_rand_new_seed, random_object_new_seed)
155
- return_hash = {}
156
- case technique
157
- when :random_rand
158
- return_hash[:random_rand_prior_seed] = refresh_random_rand_seed!(random_rand_new_seed)
159
- when :random_object
160
- return_hash[:random_object_prior_seed] = refresh_random_object_seed!(random_object_new_seed)
161
- when :randomized
162
- return_hash[:random_rand_prior_seed] = refresh_random_rand_seed!(random_rand_new_seed)
163
- return_hash[:random_object_prior_seed] = refresh_random_object_seed!(random_object_new_seed)
164
- end
165
- return_hash
166
- end
167
- # rubocop:enable Metrics/MethodLength
168
-
169
- def increment_and_evalutate_refresh_seed
170
- @count_since_last_refresh += 1
171
- return unless configuration.refresh_seed_interval
172
-
173
- refresh_seed! if @count_since_last_refresh >= configuration.refresh_seed_interval
174
- end
175
- end
24
+ ABILITY_SCORE_KEYS = %i[ability_score_array_size ability_score_number_of_sides ability_score_dice_rolled
25
+ ability_score_dice_kept].freeze
176
26
  end
data/nerd_dice.gemspec CHANGED
@@ -46,8 +46,10 @@ Gem::Specification.new do |spec|
46
46
  spec.add_dependency "securerandom", "~> 0.1", ">= 0.1.0"
47
47
 
48
48
  # Development Dependencies
49
- spec.add_development_dependency "rubocop", "~> 1.8", ">= 1.8.1"
50
- spec.add_development_dependency "rubocop-performance", "~> 1.9", ">= 1.9.1"
51
- spec.add_development_dependency "rubocop-rake", "~> 0.5", ">= 0.5.1"
52
- spec.add_development_dependency "rubocop-rspec", "~> 2.1", ">= 2.1.0"
49
+ spec.add_development_dependency "coveralls_reborn", "~> 0.22.0"
50
+ spec.add_development_dependency "rubocop", "~> 1.20", ">= 1.20.0"
51
+ spec.add_development_dependency "rubocop-performance", "~> 1.11", ">= 1.11.5"
52
+ spec.add_development_dependency "rubocop-rake", "~> 0.6", ">= 0.6.0"
53
+ spec.add_development_dependency "rubocop-rspec", "~> 2.4", ">= 2.4.0"
54
+ spec.add_development_dependency "simplecov-lcov", "~> 0.8.0"
53
55
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nerd_dice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Duchemin
@@ -35,7 +35,7 @@ cert_chain:
35
35
  WQ4faXJSevxT+x9TgyUNJINPkz/KqreClzdL83cwxPzFFQto7zF6zMCsj0slqJjW
36
36
  EQ==
37
37
  -----END CERTIFICATE-----
38
- date: 2021-01-28 00:00:00.000000000 Z
38
+ date: 2021-09-11 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: securerandom
@@ -57,86 +57,114 @@ dependencies:
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
59
  version: 0.1.0
60
+ - !ruby/object:Gem::Dependency
61
+ name: coveralls_reborn
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: 0.22.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 0.22.0
60
74
  - !ruby/object:Gem::Dependency
61
75
  name: rubocop
62
76
  requirement: !ruby/object:Gem::Requirement
63
77
  requirements:
64
78
  - - "~>"
65
79
  - !ruby/object:Gem::Version
66
- version: '1.8'
80
+ version: '1.20'
67
81
  - - ">="
68
82
  - !ruby/object:Gem::Version
69
- version: 1.8.1
83
+ version: 1.20.0
70
84
  type: :development
71
85
  prerelease: false
72
86
  version_requirements: !ruby/object:Gem::Requirement
73
87
  requirements:
74
88
  - - "~>"
75
89
  - !ruby/object:Gem::Version
76
- version: '1.8'
90
+ version: '1.20'
77
91
  - - ">="
78
92
  - !ruby/object:Gem::Version
79
- version: 1.8.1
93
+ version: 1.20.0
80
94
  - !ruby/object:Gem::Dependency
81
95
  name: rubocop-performance
82
96
  requirement: !ruby/object:Gem::Requirement
83
97
  requirements:
84
98
  - - "~>"
85
99
  - !ruby/object:Gem::Version
86
- version: '1.9'
100
+ version: '1.11'
87
101
  - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: 1.9.1
103
+ version: 1.11.5
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: '1.9'
110
+ version: '1.11'
97
111
  - - ">="
98
112
  - !ruby/object:Gem::Version
99
- version: 1.9.1
113
+ version: 1.11.5
100
114
  - !ruby/object:Gem::Dependency
101
115
  name: rubocop-rake
102
116
  requirement: !ruby/object:Gem::Requirement
103
117
  requirements:
104
118
  - - "~>"
105
119
  - !ruby/object:Gem::Version
106
- version: '0.5'
120
+ version: '0.6'
107
121
  - - ">="
108
122
  - !ruby/object:Gem::Version
109
- version: 0.5.1
123
+ version: 0.6.0
110
124
  type: :development
111
125
  prerelease: false
112
126
  version_requirements: !ruby/object:Gem::Requirement
113
127
  requirements:
114
128
  - - "~>"
115
129
  - !ruby/object:Gem::Version
116
- version: '0.5'
130
+ version: '0.6'
117
131
  - - ">="
118
132
  - !ruby/object:Gem::Version
119
- version: 0.5.1
133
+ version: 0.6.0
120
134
  - !ruby/object:Gem::Dependency
121
135
  name: rubocop-rspec
122
136
  requirement: !ruby/object:Gem::Requirement
123
137
  requirements:
124
138
  - - "~>"
125
139
  - !ruby/object:Gem::Version
126
- version: '2.1'
140
+ version: '2.4'
127
141
  - - ">="
128
142
  - !ruby/object:Gem::Version
129
- version: 2.1.0
143
+ version: 2.4.0
130
144
  type: :development
131
145
  prerelease: false
132
146
  version_requirements: !ruby/object:Gem::Requirement
133
147
  requirements:
134
148
  - - "~>"
135
149
  - !ruby/object:Gem::Version
136
- version: '2.1'
150
+ version: '2.4'
137
151
  - - ">="
138
152
  - !ruby/object:Gem::Version
139
- version: 2.1.0
153
+ version: 2.4.0
154
+ - !ruby/object:Gem::Dependency
155
+ name: simplecov-lcov
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 0.8.0
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 0.8.0
140
168
  description: |2
141
169
  Nerd dice allows you to roll polyhedral dice and add bonuses as you would in
142
170
  a tabletop roleplaying game. You can choose to roll multiple dice and keep a
@@ -148,14 +176,15 @@ email:
148
176
  executables:
149
177
  - console
150
178
  - generate_checksums
179
+ - nerd_dice_benchmark
151
180
  - setup
152
181
  extensions: []
153
182
  extra_rdoc_files: []
154
183
  files:
184
+ - ".github/workflows/main.yml"
155
185
  - ".gitignore"
156
186
  - ".rspec"
157
187
  - ".rubocop.yml"
158
- - ".travis.yml"
159
188
  - BURN_THE_CONTRIBUTOR_COVENANT_WITH_FIRE.md
160
189
  - CHANGELOG.md
161
190
  - Gemfile
@@ -166,14 +195,29 @@ files:
166
195
  - UNLICENSE.txt
167
196
  - bin/console
168
197
  - bin/generate_checksums
198
+ - bin/nerd_dice_benchmark
169
199
  - bin/setup
170
200
  - certs/msducheminjr.pem
171
201
  - checksum/nerd_dice-0.1.0.gem.sha256
172
202
  - checksum/nerd_dice-0.1.0.gem.sha512
173
203
  - checksum/nerd_dice-0.1.1.gem.sha256
174
204
  - checksum/nerd_dice-0.1.1.gem.sha512
205
+ - checksum/nerd_dice-0.2.0.gem.sha256
206
+ - checksum/nerd_dice-0.2.0.gem.sha512
175
207
  - lib/nerd_dice.rb
208
+ - lib/nerd_dice/class_methods.rb
209
+ - lib/nerd_dice/class_methods/configure.rb
210
+ - lib/nerd_dice/class_methods/execute_die_roll.rb
211
+ - lib/nerd_dice/class_methods/harvest_totals.rb
212
+ - lib/nerd_dice/class_methods/refresh_seed.rb
213
+ - lib/nerd_dice/class_methods/roll_ability_scores.rb
214
+ - lib/nerd_dice/class_methods/roll_dice.rb
215
+ - lib/nerd_dice/class_methods/total_ability_scores.rb
216
+ - lib/nerd_dice/class_methods/total_dice.rb
176
217
  - lib/nerd_dice/configuration.rb
218
+ - lib/nerd_dice/dice_set.rb
219
+ - lib/nerd_dice/die.rb
220
+ - lib/nerd_dice/sets_randomization_technique.rb
177
221
  - lib/nerd_dice/version.rb
178
222
  - nerd_dice.gemspec
179
223
  homepage: https://github.com/statelesscode/nerd_dice
@@ -203,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
247
  - !ruby/object:Gem::Version
204
248
  version: '0'
205
249
  requirements: []
206
- rubygems_version: 3.2.3
250
+ rubygems_version: 3.2.22
207
251
  signing_key:
208
252
  specification_version: 4
209
253
  summary: A Ruby Gem for rolling polyhedral dice.
metadata.gz.sig CHANGED
@@ -1,4 +1,3 @@
1
- K�B�p���:��'� �9��~��"�Y!W-��0��Fvj ~���UyyN�|=&�w��h��z�,�H)�+
2
- ��S�y`�/4��O?1:
3
- cR
4
- �`�#�;���t���W�Q��[�!�cN������U�jf '�Q� QJ���Ƿt��p�ڝu0ռ[�Rj�"���vOY���m��!� n6�9�PV�k��ԗJG/|�ѓk�VN��꿫_�+
1
+ C����w
2
+ �g4���]�T۞~����s�����ul�!��o��D���#����`t��r��r̋8l��������M]�Tu���M���X�Ʌe/���ϧ9��.�>�G��B��'��R��� �۽/r"wa�����P��p�0���pd޼�v�,�N�_㼨�����krd��PT������͋)V�c�"#RG�З{��|�nP 9�X�s�p�i���>�?^�6LXQ�.4 �O�ء6怳�`ݣ�D.����ڠ|�S��:a�P��j���"��1ȩF�*V7��
3
+ �-�wr���[ �5�7��,�4���_��}�SJ:
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.2
6
- before_install: gem install bundler -v 2.1.4