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 +4 -4
- data/.travis.yml +1 -0
- data/Gemfile +1 -3
- data/README.md +22 -6
- data/hazard.gemspec +1 -1
- data/lib/hazard/version.rb +1 -1
- data/lib/hazard.rb +33 -12
- data/lib/rolled_dice.rb +2 -2
- data/test/hazard_test.rb +21 -0
- data/test/test_helper.rb +1 -1
- data/test/weighted_table_test.rb +6 -0
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1e25f4c424287018d994674779d8d269c7fa6b4
|
4
|
+
data.tar.gz: 47c5f83f912a690514abdf59f6529412945ef82e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04f3188932596ec902ab3cd2fb30bceaa197cdc4f3f01284b2bfd9b84150080686d7478cf586202c7295b3bce11196feade4a6872e9066aed94e7f7a58237069
|
7
|
+
data.tar.gz: e370ad454d0865bf28ba73831695ffb19605a0b3a60b2c6e0c5a8194438fb34bdea7ba3fa321c38e31807cbda6ca2a6f0ea7d0ac8155241ba808d0ec4476bb7c
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
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
|
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
|
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.
|
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
|
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.
|
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
|
|
data/lib/hazard/version.rb
CHANGED
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
|
-
|
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
|
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
|
-
|
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
|
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(
|
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 ==(
|
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
data/test/weighted_table_test.rb
CHANGED
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.
|
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:
|
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
|