RpgTools 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e72bdc2ab301bc360b174d27c1ce2b22ed44b135
4
- data.tar.gz: 6c3f36a0c7c460733b9e7eb3bb340843032b0e5c
3
+ metadata.gz: 427e48bcf495c311c8ce3d2e0e052bffaebf85e5
4
+ data.tar.gz: ceb0ae54214c6802528beebd14390d40045a86df
5
5
  SHA512:
6
- metadata.gz: 67c945260be17a4e607431d0dea36add3789aa32a3cf17aa1a6152d9260918d011125df9d000a390a1c1a9304e40ace94cf78313b6d62d48b2ddde4265dcbf20
7
- data.tar.gz: 4a2b45e6b52ed24102e20b6dc843368a2dcc4d13ce73b8f4c6925e44b0e4acce17473dcf4a6f14fe0c896639543ee41dcd59aa2d923139a3c114124b820c5cc1
6
+ metadata.gz: a9156b8611ac596ab1b87c522c91317c901f3f3c2b71fbe94681c1f1083ad5d072619ac21dd50661b6f62b3ee65880687be922b2f58fde8709b824d5ca1d7926
7
+ data.tar.gz: fdfa3ec4e3ce917b2ca97464de5368a74dd630c927223be6459a183ffb83cda3466dbd28f05d14d03d9edba4fb47dd04ebeffa750ada125c02fa99a85a04a241
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- RpgTools
1
+ RpgTools [![Dependency Status](https://gemnasium.com/Yinfei/rpg_tools.svg)](https://gemnasium.com/Yinfei/rpg_tools)
2
2
  =======
3
3
 
4
4
  A gem that gives you tools for your RPGs : dices, coins, cards !
5
5
 
6
- # Usage
6
+ ## Usage
7
7
 
8
8
  * <tt>RpgTools::Coin.flip</tt> returns a "heads" or "tails" string.
9
9
  * <tt>RpgTools::Dice.rool(arg)</tt> returns an array of dices of your choice (arg format : "1d6" or "4D10" for example).
@@ -1,22 +1,54 @@
1
1
  module RpgTools
2
2
  class Dice
3
3
  class << self
4
- def roll(arg)
5
- dices_num = arg.split(/d/i).first.to_i
6
- max_value = arg.split(/d/i).last.to_i
4
+ def roll(dice)
5
+ dices = dice.split(/d/i).first.to_i
6
+ max_value = dice.split(/d/i).last.to_i
7
7
 
8
8
  # I mean, seriously ?
9
- return true if dices_num.zero?
9
+ return true if dices.zero?
10
10
 
11
11
  # We don't roll d0 and d1 for obvious reasons.
12
12
  return true if [0, 1].include?(max_value)
13
13
 
14
+ # We do a normal roll if no bonus/malus is set on the dice
15
+ return standard_roll(dices, max_value) if dice =~ /^\d+[Dd]\d+$/
16
+
17
+ # We apply bonuses/maluses properly if any
18
+ if dice =~ /^\d+d\d+(\+)(\d+)/
19
+ bonus = dice.gsub(/^\d+d\d+(\+)/, '').to_i
20
+ roll_with_bonus(dices, max_value, bonus)
21
+ elsif dice =~ /^\d+d\d+(\-)(\d+)/
22
+ malus = dice.gsub(/^\d+d\d+(\-)/, '').to_i
23
+ roll_with_malus(dices, max_value, malus)
24
+ else
25
+ true
26
+ end
27
+ end
28
+
29
+ def standard_roll(dices, max_value)
14
30
  [].tap do |dice_set|
15
- dices_num.times do
31
+ dices.times do
16
32
  dice_set << rand(1..max_value)
17
33
  end
18
34
  end
19
35
  end
36
+
37
+ def roll_with_bonus(dices, max_value, bonus)
38
+ [].tap do |dice_set|
39
+ dices.times do
40
+ dice_set << (rand(1..max_value) + bonus)
41
+ end
42
+ end
43
+ end
44
+
45
+ def roll_with_malus(dices, max_value, malus)
46
+ [].tap do |dice_set|
47
+ dices.times do
48
+ dice_set << (rand(1..max_value) - malus)
49
+ end
50
+ end
51
+ end
20
52
  end
21
53
  end
22
54
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{RpgTools}
3
- s.version = '0.2.0'
3
+ s.version = '0.3.0'
4
4
  s.author = 'Yohan Piron'
5
5
  s.email = 'yinfei84@gmail.com'
6
6
  s.date = %q{2014-12-18}
@@ -11,6 +11,6 @@ Gem::Specification.new do |s|
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.test_files = s.files.grep(%r{^(spec)/})
13
13
  s.require_paths = ['lib']
14
- s.add_development_dependency 'rake', '~> 3.1'
14
+ s.add_development_dependency 'rake', '~> 10.4'
15
15
  s.add_development_dependency 'rspec', '~> 3.0'
16
16
  end
@@ -22,4 +22,16 @@ describe RpgTools::Dice do
22
22
  expect(described_class.roll('1d6').count).to eq(1)
23
23
  expect(described_class.roll('1D6').count).to eq(1)
24
24
  end
25
+
26
+ it 'rolls a dice with a bonus' do
27
+ expect(described_class.roll('1d6+2').count).to eq(1)
28
+ end
29
+
30
+ it 'rolls a dice with a malus' do
31
+ expect(described_class.roll('1d6-2').count).to eq(1)
32
+ end
33
+
34
+ it "doesn't roll a dice with an odd symbol" do
35
+ expect(described_class.roll('1d6%2')).to eq(true)
36
+ end
25
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RpgTools
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
  - Yohan Piron
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '10.4'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '10.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement