hazard 1.1.0 → 1.2.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: f9ae47cda73390cc30cd6d81a594879a0f142308
4
- data.tar.gz: 7eb73440367d32076ddbef3b9d2ded4ecb772fe5
3
+ metadata.gz: 95dbcf77daa7235693220c74d60b47e42ab3e635
4
+ data.tar.gz: 062155ee83ebdddb008e8c7f6f133b4c4d8292bf
5
5
  SHA512:
6
- metadata.gz: 7678295f955f7d2ddc157951b5da6d91746092eaf4f6eaad5c10dfbba7b19505bc676aa3d0baef9be415804e2ee5427d78802d45965ba486d94459d4453ada2d
7
- data.tar.gz: 9c000fc08c2bd6debd467784dd728cd106f87958add39c9d8ab28d4b51d24a3f65930e22b55c9d80dee3e3f6b5f77a865b6a427a966552f3cf51da717ac976bb
6
+ metadata.gz: 8b77918372e932a63734eabf940a5c06fd6a5263df7f9290c26b88a7ee18f80dbdc004d1aa341b96e5c07d3ec864460cf1a8509338af9a76fd7272f3bfbf54b2
7
+ data.tar.gz: da19de999a0edcead798d6ffb9716154fbde19288bcda76219dff6edacb7bf9750a0dbc3d8421ad418a3144ac4cf3ef62e3510f1342c139d186f9bf6bee84249
data/.travis.yml CHANGED
@@ -14,6 +14,7 @@ rvm:
14
14
  - 2.2
15
15
  - 2.1
16
16
  - 2.0
17
+ - 1.9
17
18
 
18
19
  before_script:
19
20
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
data/README.md CHANGED
@@ -16,8 +16,9 @@ Hazard is a very simple dice library for ruby that allows you to :
16
16
  1. [Roll multiple dice](#roll-multiple-dice)
17
17
  1. [Advanced Usage](#advanced-usage)
18
18
  1. [Roll dice and get the details](#roll-dice-and-get-the-details)
19
+ 1. [Roll dice from string input](#roll-dice-from-string-input)
19
20
  1. [Some real cases](#some-real-cases)
20
- 1. [Weighted Tables](#weighted-Tables)
21
+ 1. [Weighted Tables](#weighted-tables)
21
22
  1. [If you have the weights](#if-you-have-the-weights)
22
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
24
  1. [Saving and loading](#saving-and-loading)
@@ -27,7 +28,7 @@ Hazard is a very simple dice library for ruby that allows you to :
27
28
  Add this line to your application's Gemfile:
28
29
 
29
30
  ```ruby
30
- gem 'hazard'
31
+ gem 'hazard', '~> 1.2.0'
31
32
  ```
32
33
 
33
34
  And then execute:
@@ -116,6 +117,16 @@ Examples :
116
117
  >> Hazard.s2d6
117
118
  => #<RolledDice:0x007f62e55a0010 @rolls=[1, 6], @result=7>
118
119
 
120
+ ### Roll dice from string input
121
+
122
+ >> Hazard.from_string( 's<m>d<n>' ) # where m and n are numbers
123
+ => Roll m n-sided dice and return a RolledDice object
124
+
125
+ Examples :
126
+
127
+ >> Hazard.from_string( 'r2d6' )
128
+ => 3
129
+
119
130
  ### Some real cases
120
131
 
121
132
  Assuming you are playing DD Next
data/hazard.gemspec CHANGED
@@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rake', '~> 10.0'
29
29
  spec.add_development_dependency 'minitest', '~> 5.0'
30
30
 
31
+ spec.required_ruby_version = '>= 1.9'
32
+
31
33
  end
@@ -1,3 +1,3 @@
1
1
  class Hazard
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/hazard.rb CHANGED
@@ -4,10 +4,18 @@ require_relative 'weighted_table'
4
4
  class Hazard
5
5
 
6
6
  def self.method_missing( method_name )
7
-
8
7
  # Transform the method_name to string
9
8
  method_name = method_name.to_s
9
+ self.roll_dice( method_name )
10
+ end
11
+
12
+ def self.from_string( dice_string )
13
+ roll_dice( dice_string )
14
+ end
15
+
16
+ private
10
17
 
18
+ def self.roll_dice( method_name )
11
19
  # Parse the method name to get how many dice and what size of dice was required
12
20
  dice_match = method_name.to_s.match( /(d|r|m|s)?(\d*)d(\d+)/ )
13
21
  # Raise an error if match fail
@@ -32,7 +40,6 @@ class Hazard
32
40
 
33
41
  # Return a RolledDice otherwise
34
42
  RolledDice.new( rolls )
35
-
36
43
  end
37
44
 
38
45
  end
@@ -62,15 +62,6 @@ class WeightedTable
62
62
  raise 'Rand not in key range'
63
63
  end
64
64
 
65
- # Load a WeightedTable with data
66
- # Data format must be : [ data, data, data ]
67
- # Example : [ :foo, :foo, :bar ]
68
- #
69
- # @return [WeightedTable] the current WeightedTable
70
- def from_flat_table( table )
71
- from_weighted_table( table.group_by{ |e| e }.map{ |k, v| [ v.count, k ] } )
72
- end
73
-
74
65
  # Save the table to a file
75
66
  # @param filename [String] the filename where to save the table
76
67
  def to_file( filename )
data/test/hazard_test.rb CHANGED
@@ -18,4 +18,21 @@ class HazardTest < Minitest::Test
18
18
 
19
19
  end
20
20
 
21
+ def test_dice_from_string
22
+
23
+ Kernel.stubs( :rand ).returns( 6 )
24
+
25
+ assert_equal 6, Hazard.from_string( 'd6' )
26
+
27
+ assert_equal 12, Hazard.from_string( 'r2d6' )
28
+ assert_equal 12, Hazard.from_string( '_2d6' )
29
+ assert_equal 12, Hazard.from_string( 'm2d6' )
30
+ assert_equal 12, Hazard.from_string( 'd2d6' )
31
+ assert_equal 18, Hazard.from_string( 'd3d6' )
32
+
33
+ assert_equal RolledDice.new([6, 6 ] ), Hazard.from_string( 's2d6' )
34
+
35
+ end
36
+
37
+
21
38
  end
@@ -39,24 +39,38 @@ class HazardTest < Minitest::Test
39
39
  assert_equal :bar, WeightedTable.from_flat_table( [ :foo, :foo, :foo, :bar ] ).sample
40
40
 
41
41
  Kernel.stubs( :rand ).returns( 5 )
42
- assert_raises do
42
+ assert_raises 'Rand not in key range' do
43
43
  WeightedTable.from_weighted_table []
44
44
  end
45
- assert_raises do
45
+ assert_raises 'Rand not in key range' do
46
46
  WeightedTable.from_flat_table []
47
47
  end
48
48
  end
49
49
 
50
- def test_statistics
50
+ def test_three_element_table
51
+
52
+ Kernel.stubs( :rand ).returns( 1 )
53
+ assert_equal :foo, WeightedTable.from_weighted_table( [ [ 3, :foo ], [ 1, :bar ], [ 1, :foobar ] ] ).sample
54
+ assert_equal :foo, WeightedTable.from_flat_table( [ :foo, :foo, :foo, :bar, :foobar ] ).sample
55
+
56
+ Kernel.stubs( :rand ).returns( 5 )
57
+ assert_equal :foobar, WeightedTable.from_weighted_table( [ [ 3, :foo ], [ 1, :bar ], [ 1, :foobar ] ] ).sample
58
+ assert_equal :foobar, WeightedTable.from_flat_table( [ :foo, :foo, :foo, :bar, :foobar ] ).sample
59
+
60
+ end
51
61
 
62
+ def test_statistics
52
63
  wt = WeightedTable.from_weighted_table [ [ 2, :foo ], [ 1, :bar ] ]
53
64
  results = { foo: 0, bar: 0 }
54
- 1.upto(100).each do
65
+
66
+ rounds = 1000
67
+
68
+ 1.upto(rounds).each do
55
69
  results[ wt.sample ] += 1
56
70
  end
57
71
 
58
- assert_in_delta 0.33, (results[:bar]*0.01), 0.1
59
- assert_in_delta 0.66, (results[:foo]*0.01), 0.1
72
+ assert_in_delta 0.33, (results[:bar].to_f/rounds), 0.1
73
+ assert_in_delta 0.66, (results[:foo].to_f/rounds), 0.1
60
74
  end
61
75
 
62
76
  def test_save_and_load
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hazard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric ZUGER
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="
89
89
  - !ruby/object:Gem::Version
90
- version: '0'
90
+ version: '1.9'
91
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - ">="