hazard 1.2.1 → 1.3.0

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: '08657cc882b26b86eb7897b444e125cb51799659'
4
- data.tar.gz: b4e26bda3ada9032117860ef51f41706478f1502
3
+ metadata.gz: e1e25f4c424287018d994674779d8d269c7fa6b4
4
+ data.tar.gz: 47c5f83f912a690514abdf59f6529412945ef82e
5
5
  SHA512:
6
- metadata.gz: 23ef045fd2f8ec1548bd8e24efa5a64b2db98967d5b3c27c34676d8be30908f47ef7cd84b66111749665302927c9fd645ed2308a9464567fdfd92ead6a298570
7
- data.tar.gz: d5715f5e44b94c8a8f6555dd4212ef94d7b5b3ee84bc78a8aef894ca9e0905beb748b920ccddcd323f7e5920e168493bac98b634b123bba48ebea179ba168c78
6
+ metadata.gz: 04f3188932596ec902ab3cd2fb30bceaa197cdc4f3f01284b2bfd9b84150080686d7478cf586202c7295b3bce11196feade4a6872e9066aed94e7f7a58237069
7
+ data.tar.gz: e370ad454d0865bf28ba73831695ffb19605a0b3a60b2c6e0c5a8194438fb34bdea7ba3fa321c38e31807cbda6ca2a6f0ea7d0ac8155241ba808d0ec4476bb7c
data/.travis.yml CHANGED
@@ -8,6 +8,7 @@ env:
8
8
 
9
9
  language: ruby
10
10
  rvm:
11
+ - 2.6
11
12
  - 2.5
12
13
  - 2.4
13
14
  - 2.3
data/Gemfile CHANGED
@@ -3,10 +3,8 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in hazard.gemspec
4
4
  gemspec
5
5
 
6
- gem 'mocha', group: 'test'
7
-
8
6
  group :test do
9
- gem 'simplecov'
10
7
  gem 'codeclimate-test-reporter', '~> 1.0.0'
11
8
  gem 'mocha'
9
+ gem 'simplecov'
12
10
  end
data/README.md CHANGED
@@ -20,7 +20,7 @@ Hazard is a very simple dice library for ruby that allows you to :
20
20
  1. [Some real cases](#some-real-cases)
21
21
  1. [Weighted Tables](#weighted-tables)
22
22
  1. [If you have the weights](#if-you-have-the-weights)
23
- 1. [If you don't have the weights (or are to lazy to get them)](#if-you-dont-have-the-weights-or-are-to-lazy-to-get-them)
23
+ 1. [If you don't have the weights (or are too lazy to get them)](#if-you-dont-have-the-weights-or-are-too-lazy-to-get-them)
24
24
  1. [Saving and loading](#saving-and-loading)
25
25
 
26
26
  ## Installation
@@ -28,7 +28,7 @@ Hazard is a very simple dice library for ruby that allows you to :
28
28
  Add this line to your application's Gemfile:
29
29
 
30
30
  ```ruby
31
- gem 'hazard', '~> 1.2.0'
31
+ gem 'hazard', '~> 1.2'
32
32
  ```
33
33
 
34
34
  And then execute:
@@ -88,6 +88,20 @@ Examples :
88
88
  >> Hazard.d2d6
89
89
  => 8
90
90
 
91
+ ### Lucky
92
+
93
+ Some times, you just need to test an unlikely case. Lucky roll a die and return true if you rolled the highest value.
94
+
95
+ Examples :
96
+
97
+ >> Hazard.lucky?( 6 )
98
+ => true
99
+ # Means that you rolled 6
100
+
101
+ >> Hazard.lucky?( 6 )
102
+ => false
103
+ # Means that you rolled something else than 6
104
+
91
105
  ## Advanced Usage
92
106
 
93
107
  ### Roll dice and get the details
@@ -163,12 +177,12 @@ Weighted tables are object that allow to get weighted random.
163
177
 
164
178
  Examples :
165
179
 
166
- >> wt = WeightedTable.new.from_weighted_table( [ 2, :foo ], [ 1, :bar ] ]
180
+ >> wt = WeightedTable.from_weighted_table( [ 2, :foo ], [ 1, :bar ] ]
167
181
  >> wt.sample
168
182
  # This ensure that you will get 66% foo and 33% bar
169
183
 
170
184
 
171
- ### If you don't have the weights (or are to lazy to get them)
185
+ ### If you don't have the weights (or are too lazy to get them)
172
186
 
173
187
  >> wt = WeightedTable.from_flat_table( <object1>, <object1>, <object2>, ... ]
174
188
  # Create a weighted table storing objects computing the weight of the objects according to theire occurences
@@ -178,7 +192,7 @@ Examples :
178
192
 
179
193
  Examples :
180
194
 
181
- >> wt = WeightedTable.new.from_flat_table( :foo, :foo, :bar ]
195
+ >> wt = WeightedTable.from_flat_table( :foo, :foo, :bar ]
182
196
  >> wt.sample
183
197
  # This ensure that you will get 66% foo and 33% bar
184
198
 
@@ -188,7 +202,9 @@ Examples :
188
202
  >> wt.to_file( filename )
189
203
 
190
204
  # And load it
191
- >> wt = WeightedTable.from_file( filename )
205
+ >> wt = WeightedTable.from_file( filename )
206
+
207
+ # Note : backup format is YAML
192
208
 
193
209
  ## Contributing
194
210
 
data/hazard.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ['lib']
26
26
 
27
- spec.add_development_dependency 'bundler', '~> 1.13'
27
+ # spec.add_development_dependency 'bundler', '~> 1.13'
28
28
  spec.add_development_dependency 'rake', '~> 10.0'
29
29
  spec.add_development_dependency 'minitest', '~> 5.0'
30
30
 
@@ -1,3 +1,3 @@
1
1
  class Hazard
2
- VERSION = '1.2.1'
2
+ VERSION = '1.3.0'
3
3
  end
data/lib/hazard.rb CHANGED
@@ -6,15 +6,21 @@ require_relative 'weighted_table'
6
6
  # @author Cédric ZUGER
7
7
  class Hazard
8
8
 
9
+ DICE_NAME_REGEXP = /(d|r|m|s)?(\d*)d(\d+)/.freeze
10
+
9
11
  # Regular entry point. This is where you will go when you call Hazard.d6 for instance.
10
12
  #
11
13
  # @param method_name [String] the description of the dice. See help for detail.
12
14
  #
13
15
  # @return [Object] if detail has been asked, it will return a [RolledDice] object, otherwise it will return an [Integer] containing the sum of the dice.
14
16
  def self.method_missing( method_name )
15
- # Transform the method_name to string
16
17
  method_name = method_name.to_s
17
- self.roll_dice( method_name )
18
+
19
+ if method_name =~ DICE_NAME_REGEXP
20
+ roll_dice( method_name )
21
+ else
22
+ super
23
+ end
18
24
  end
19
25
 
20
26
  # From string entry point, in case you have your dice description in a database for instance.
@@ -26,6 +32,20 @@ class Hazard
26
32
  roll_dice( dice_string )
27
33
  end
28
34
 
35
+ # Hook the method_missing
36
+ def self.respond_to_missing?(method_name, include_private = false)
37
+ method_name.to_s =~ DICE_NAME_REGEXP || super
38
+ end
39
+
40
+ # Roll a dice and return true if you rolled the highest number
41
+ #
42
+ # @param dice [Integer] the dice you want to roll.
43
+ #
44
+ # @return [Boolean] true if you scored the highest number, false otherwise.
45
+ def self.lucky?( dice )
46
+ roll_dice( "d#{dice}" ) == dice
47
+ end
48
+
29
49
  private
30
50
 
31
51
  # Roll dice according to method name
@@ -35,29 +55,30 @@ class Hazard
35
55
  # @return [Object] if detail has been asked, it will return a [RolledDice] object, otherwise it will return an [Integer] containing the sum of the dice.
36
56
  def self.roll_dice( method_name )
37
57
  # Parse the method name to get how many dice and what size of dice was required
38
- dice_match = method_name.to_s.match( /(d|r|m|s)?(\d*)d(\d+)/ )
39
- # Raise an error if match fail
40
- raise "Method mising : #{method_name}" unless dice_match
58
+ dice_match = method_name.to_s.match DICE_NAME_REGEXP
41
59
 
42
60
  # Get the roll type
43
61
  roll_type = dice_match[1]
44
- splited_result = true if roll_type == 's'
62
+ splitted_result = true if roll_type == 's'
45
63
 
46
- # Get the dice amount
47
- dice_amount = dice_match[2].to_i
48
- # If no amount is given then the amount is 1
49
- dice_amount = 1 if dice_amount == 0
50
64
  # Get the type of dice
51
65
  dice_type = dice_match[3].to_i
52
66
 
53
67
  # Rolls the dice
54
- rolls = ( 1..dice_amount ).map{ Kernel.rand( 1..dice_type ) }
68
+ rolls = ( 1..dice_amount(dice_match) ).map{ Kernel.rand( 1..dice_type ) }
55
69
 
56
70
  # Unless splitted_result was requested, return the sum of the rolled dice
57
- return rolls.reduce(:+) unless splited_result
71
+ return rolls.reduce(:+) unless splitted_result
58
72
 
59
73
  # Return a RolledDice otherwise
60
74
  RolledDice.new( rolls )
61
75
  end
62
76
 
77
+ def self.dice_amount( dice_match )
78
+ # Get the dice amount
79
+ dice_amount = dice_match[2].to_i
80
+ # If no amount is given then the amount is 1
81
+ dice_amount.zero? ? 1 : dice_amount
82
+ end
83
+
63
84
  end
data/lib/rolled_dice.rb CHANGED
@@ -8,7 +8,7 @@ class RolledDice
8
8
  # Create a RolledDice object
9
9
  #
10
10
  # @param rolls [Array] an array of integer containing the dice rolls.
11
- def initialize( rolls )
11
+ def initialize(rolls)
12
12
  @rolls = rolls
13
13
  @result = rolls.reduce(:+)
14
14
  end
@@ -18,7 +18,7 @@ class RolledDice
18
18
  # @param rolled_dice [RolledDice] the other RolledDice to compare
19
19
  #
20
20
  # @return [Boolean] the result of the comparison
21
- def ==( rolled_dice )
21
+ def ==(rolled_dice)
22
22
  @rolls == rolled_dice.rolls && @result == rolled_dice.result
23
23
  end
24
24
 
data/test/hazard_test.rb CHANGED
@@ -34,5 +34,26 @@ class HazardTest < Minitest::Test
34
34
 
35
35
  end
36
36
 
37
+ def test_lucky
38
+ total = 0
39
+ hits = 0
40
+
41
+ 1.upto(1000).each do
42
+ total += 1
43
+ hits += 1 if Hazard.lucky?( 6 )
44
+ end
45
+
46
+ assert_in_delta hits.to_f/total, 1.0/6, 1.0/10
47
+ end
48
+
49
+ def test_method_missing
50
+ assert Hazard.respond_to?(:d6)
51
+ refute Hazard.respond_to?(:foo)
52
+
53
+ assert_raises do
54
+ Hazard.foo
55
+ end
56
+ end
57
+
37
58
 
38
59
  end
data/test/test_helper.rb CHANGED
@@ -6,6 +6,6 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
6
6
  require 'hazard'
7
7
 
8
8
  require 'minitest/autorun'
9
- require 'mocha/mini_test'
9
+ require 'mocha/minitest'
10
10
  require 'pp'
11
11
 
@@ -84,4 +84,10 @@ class HazardTest < Minitest::Test
84
84
  `rm wt.yaml`
85
85
  end
86
86
 
87
+ def test_out_of_range
88
+ Kernel.stubs( :rand ).returns( 99 )
89
+ assert_raises do
90
+ WeightedTable.from_weighted_table( [ [ 3, :foo ], [ 1, :bar ], [ 1, :foobar ] ] ).sample
91
+ end
92
+ end
87
93
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hazard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric ZUGER
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-24 00:00:00.000000000 Z
11
+ date: 2019-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.13'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.13'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement