RpgTools 0.3.1 → 0.4.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: 76b4b5396a8ebfd678ca5ac5dbf115c3d0eeb028
4
- data.tar.gz: dcf5233ee0231ad7b1977c4800ac857472f13150
3
+ metadata.gz: 437924d49047a2130a12dee90acaea5c446ea0cb
4
+ data.tar.gz: 7440243205aa3f8c74d52188fc7bcabb0a0cef94
5
5
  SHA512:
6
- metadata.gz: a80e08868aee9e9ab8861b8a71c0593020923db1458e9cab1a55f63869a32b2744a013f9a31d91af467c6e9aa3f9febb0a4d94685a1127f02a9492f1aab2c4b1
7
- data.tar.gz: cdc14519c78f42c845654783a6f2c39753b8f4572900b84daec2f4a4babddb1e4ddbc19dc99498bdff749f491e9a47427d4447f000db1bc030aa3a88fd9c2b19
6
+ metadata.gz: ecdea0456bd828ed25aac3c7b8504e4359ec835fb382e325e8655985a5dd8ec1b60f2a5f73fabe1231c3e49407de65a5fe94e98bf29cc7ee46af03787781b4b3
7
+ data.tar.gz: 6134672b09c2228c941e65a023232ae775f613218d0f91954b38e3b8f7be783fa4069b12630e46fdb57e93279ecff737cf0ce32b4d28194268f09474d3ee5579
data/README.md CHANGED
@@ -1,13 +1,48 @@
1
1
  RpgTools [![Dependency Status](https://gemnasium.com/Yinfei/rpg_tools.svg)](https://gemnasium.com/Yinfei/rpg_tools)
2
2
  =======
3
3
 
4
- A gem that gives you tools for your RPGs : dices, coins, cards !
4
+ A gem that gives you tools for your RPGs : dices, coins and playing cards !
5
5
 
6
6
  ## Usage
7
7
 
8
- * <tt>RpgTools::Coin.flip</tt> returns a "heads" or "tails" string.
9
- * <tt>RpgTools::Dice.roll(arg)</tt> returns an array of dices of your choice (arg format : "1d6", "4D10", "1d6+3" or "4D6-1" for example).
10
- * <tt>RpgTools::Card.pick</tt> returns a card of a 52 cards deck + 2 jokers.
8
+ ### Coins
9
+
10
+ `RpgTools::Coin.new`
11
+
12
+ * The `flip` method set its value to a "heads" or "tails" string.
13
+
14
+ ### Dices
15
+
16
+ `RpgTools::Coin.new(arg)` <i>(`arg` is case insensitive)</i>.
17
+
18
+ * It can be a stardard dice like `"dX"` for a X sided dice,
19
+ it also accepts modifiers like '+X' or '-X' to alter the roll result.
20
+
21
+ `RpgTools::Dice.new("d6+2")` will create a 6 sided dice that adds 2 to each
22
+ roll result.
23
+
24
+ * It can also be a fudge dice `"df"` : a three sided dice (-1, 0, +1).
25
+
26
+ * The `roll` method rolls it and set its value according to the result.
27
+
28
+ ### Dice Bags
29
+
30
+ `RpgTools::DiceBag.new(arg)` <i>(`arg` is case insensitive)</i>.
31
+
32
+ * You can only create standard dice and fudge dice bags.
33
+
34
+ `RpgTools::DiceBag.new("4d6-2)` will create a bag of four 6 sided dices with a -2 modifier.
35
+
36
+ * The `roll` method rolls each dice of the bag.
37
+ * The `total` attribute stores the sum of each dice result.
38
+ * The `set` attribute stores an array of each dice result.
39
+
40
+ ### Playing card deck
41
+ `RpgTools::PlayingCardDeck.new(arg)` <i>(`arg` can only be 32 or 52)</i>.
42
+
43
+ * The `card` method returns a card of a 32 or 52 cards deck + 2 jokers, depending
44
+ of `arg` value.
45
+ * The `hand` method return an array of 5 `card` calls.
11
46
 
12
47
  ## Contributing
13
48
 
data/lib/rpg_tools.rb CHANGED
@@ -1,3 +1,4 @@
1
- require './lib/rpg_tools/card'
2
- require './lib/rpg_tools/coin'
3
- require './lib/rpg_tools/dice'
1
+ require 'rpg_tools/coin'
2
+ require 'rpg_tools/dice'
3
+ require 'rpg_tools/dice_bag'
4
+ require 'rpg_tools/playing_card_deck'
@@ -1,9 +1,20 @@
1
1
  module RpgTools
2
2
  class Coin
3
- class << self
4
- def flip
5
- rand(2) == 1 ? 'heads' : 'tails'
6
- end
3
+ attr_accessor :value, :flips, :heads, :tails
4
+
5
+ def initialize
6
+ @value = nil
7
+ @flips = 0
8
+ @heads = 0
9
+ @tails = 0
10
+ end
11
+
12
+ def flip
13
+ @flips += 1
14
+ @value = rand(2) == 1 ? 'Heads' : 'Tails'
15
+ @heads += 1 if @value == 'Heads'
16
+ @tails += 1 if @value == 'Tails'
17
+ @value
7
18
  end
8
19
  end
9
20
  end
@@ -1,54 +1,69 @@
1
1
  module RpgTools
2
2
  class Dice
3
- class << self
4
- def roll(dice)
5
- dices = dice.split(/d/i).first.to_i
6
- max_value = dice.split(/d/i).last.to_i
7
-
8
- # I mean, seriously ?
9
- return true if dices.zero?
10
-
11
- # We don't roll d0 and d1 for obvious reasons.
12
- return true if [0, 1].include?(max_value)
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+[Dd]\d+(\+)(\d+)/
19
- bonus = dice.gsub(/^\d+[Dd]\d+(\+)/, '').to_i
20
- roll_with_bonus(dices, max_value, bonus)
21
- elsif dice =~ /^\d+[Dd]\d+(\-)(\d+)/
22
- malus = dice.gsub(/^\d+[Dd]\d+(\-)/, '').to_i
23
- roll_with_malus(dices, max_value, malus)
24
- else
25
- true
26
- end
27
- end
3
+ attr_accessor :base, :sides, :value, :type, :rolls, :bonus, :malus
4
+
5
+ def initialize(base)
6
+ @base = base
7
+ base_check
8
+ @value = nil
9
+ @type = (@base =~ /^.[fF]$/).nil? ? 'Standard' : 'Fudge'
10
+ @sides = @type == 'Fudge' ? 3 : base.gsub(/^./, '').to_i
11
+ sides_check
12
+ @rolls = 0
13
+ set_modifiers if @type == 'Standard'
14
+ end
15
+
16
+ def roll
17
+ return fudge_roll if @type == 'Fudge'
18
+ return standard_roll if @bonus.nil? && @malus.nil?
19
+ return roll_with_bonus if @malus.nil?
20
+ return roll_with_malus if @bonus.nil?
21
+ end
28
22
 
29
- def standard_roll(dices, max_value)
30
- [].tap do |dice_set|
31
- dices.times do
32
- dice_set << rand(1..max_value)
33
- end
34
- end
23
+ private
24
+
25
+ def modifier_check
26
+ unless @base.gsub(/^[dD]{1}[fF]?\d+|[+-]\d+/, '').empty?
27
+ raise ArgumentError.new("You can only use + or - as modifiers.")
35
28
  end
29
+ end
30
+
31
+ def set_modifiers
32
+ modifier_check
33
+ @bonus = (@base =~ /[Dd]\d+\+(\d+)/).nil? ? nil : @base.gsub(/[Dd]\d+\+/, '').to_i
34
+ @malus = (@base =~ /[Dd]\d+\-(\d+)/).nil? ? nil : @base.gsub(/[Dd]\d+\-/, '').to_i
35
+ end
36
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
37
+ def base_check
38
+ unless ['d', 'D', 'f', 'F'].include?(@base[0])
39
+ raise ArgumentError.new("You can only create strandard and fudge dices.")
43
40
  end
41
+ end
44
42
 
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
43
+ def sides_check
44
+ if [0, 1].include?(@sides)
45
+ raise ArgumentError.new("You can't create dices with less than 2 sides.")
51
46
  end
52
47
  end
48
+
49
+ def fudge_roll
50
+ @rolls += 1
51
+ @value = (rand(1..3) - 2)
52
+ end
53
+
54
+ def standard_roll
55
+ @rolls += 1
56
+ @value = rand(1..@sides)
57
+ end
58
+
59
+ def roll_with_bonus
60
+ @rolls += 1
61
+ @value = rand(1..@sides) + @bonus
62
+ end
63
+
64
+ def roll_with_malus
65
+ @rolls += 1
66
+ @value = rand(1..@sides) - @malus
67
+ end
53
68
  end
54
69
  end
@@ -0,0 +1,44 @@
1
+ module RpgTools
2
+ class DiceBag
3
+ attr_accessor :base, :dices, :rolls, :set, :total
4
+
5
+ def initialize(base)
6
+ @base = base
7
+ @dices = set_bag_content(@base.gsub(/[dD].+/, '').to_i)
8
+ @rolls = 0
9
+ @set = nil
10
+ @total = 0
11
+ end
12
+
13
+ def roll
14
+ @dices.each do |dice|
15
+ dice.roll
16
+ end
17
+
18
+ @set = [].tap do |response|
19
+ @dices.each do |dice|
20
+ response << dice.value
21
+ end
22
+ end
23
+ @total = @set.inject{ |sum, x| sum + x }
24
+ @set
25
+ end
26
+
27
+ private
28
+
29
+ def empty_bag_check(dice_number)
30
+ if dice_number.zero?
31
+ raise ArgumentError.new("You can't create an empty bag.")
32
+ end
33
+ end
34
+
35
+ def set_bag_content(dice_number)
36
+ empty_bag_check(dice_number)
37
+ [].tap do |bag|
38
+ dice_number.times do |dice|
39
+ bag << Dice.new(@base.gsub(/^\d+/, ''))
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ module RpgTools
2
+ class PlayingCardDeck
3
+ attr_accessor :type, :card_picks, :value
4
+
5
+ def initialize(type)
6
+ @type = type
7
+ type_check
8
+ @card_picks = 0
9
+ @value = nil
10
+ end
11
+
12
+ def card
13
+ @card_picks += 1
14
+ @value = joker_pick_check == true ? 'Joker' : standard_card
15
+ end
16
+
17
+ def hand
18
+ [].tap do |hand|
19
+ until hand.count == 5
20
+ card = standard_card
21
+ hand << card unless hand.include?(card) || hand.count('Joker') == 2
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def type_check
29
+ unless [32, 52].include?(@type)
30
+ raise ArgumentError.new('A playing card deck contains 32 or 52 cards only.')
31
+ end
32
+ end
33
+
34
+ def joker_pick_check
35
+ [0, 1].include?((0..54).to_a.sample(1).first)
36
+ end
37
+
38
+ def number
39
+ if @type == 32
40
+ [(7..10).to_a, 'Jack', 'Queen', 'King'].flatten.sample(1)
41
+ else
42
+ [(1..10).to_a, 'Jack', 'Queen', 'King'].flatten.sample(1)
43
+ end
44
+ end
45
+
46
+ def color
47
+ ['Clubs', 'Hearts', 'Diamonds', 'Spades'].sample(1)
48
+ end
49
+
50
+ def standard_card
51
+ @card_picks += 1
52
+ @value = [number, color].join(' of ')
53
+ end
54
+ end
55
+ end
data/rpg_tools.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{RpgTools}
3
- s.version = '0.3.1'
3
+ s.version = '0.4.0'
4
4
  s.author = 'Yohan Piron'
5
5
  s.email = 'yinfei84@gmail.com'
6
- s.date = %q{2015-01-07}
6
+ s.date = %q{2015-01-11}
7
7
  s.summary = %q{RpgTools is a toolbox for tabletop games and RPGs}
8
8
  s.description = 'RpgTools gives you tools for your RPGs : dices, coins, cards...'
9
9
  s.homepage = 'https://github.com/Yinfei/rpg_tools.git'
data/spec/coin_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'rpg_tools'
2
2
  require 'spec_helper'
3
3
 
4
4
  describe RpgTools::Coin do
5
- it "can't return anything else than 'heads' or 'tails'" do
6
- expect(['heads', 'tails'].include?(described_class.flip)).to eq(true)
5
+ it "can't return anything else than 'Heads' or 'Tails'" do
6
+ expect(['Heads', 'Tails'].include?(described_class.new.flip)).to eq(true)
7
7
  end
8
8
  end
@@ -0,0 +1,20 @@
1
+ require 'rpg_tools'
2
+ require 'spec_helper'
3
+
4
+ describe RpgTools::DiceBag do
5
+ it 'creates a bag with the right number of dices' do
6
+ expect(described_class.new('4d6').dices.count).to eq(4)
7
+ end
8
+
9
+ it "doesn't roll the dices inside the bag at creation" do
10
+ expect(described_class.new('2df').dices.first.value).to eq(nil)
11
+ end
12
+
13
+ it "doesn't create a bag with odd modifier dices" do
14
+ expect { described_class.new('4d1%4') }.to raise_error(ArgumentError)
15
+ end
16
+
17
+ it "doesn't create empty bag" do
18
+ expect { described_class.new('0D10') }.to raise_error(ArgumentError)
19
+ end
20
+ end
data/spec/dice_spec.rb CHANGED
@@ -2,36 +2,37 @@ require 'rpg_tools'
2
2
  require 'spec_helper'
3
3
 
4
4
  describe RpgTools::Dice do
5
- it "doesn't roll a '0d' dice" do
6
- expect(described_class.roll('0d6')).to eq(true)
5
+ it "doesn't create a 'd0' dice" do
6
+ expect { described_class.new('d0') }.to raise_error(ArgumentError)
7
7
  end
8
8
 
9
- it "doesn't roll a 'd0' dice" do
10
- expect(described_class.roll('1d0')).to eq(true)
9
+ it "doesn't create a 'd1' dice" do
10
+ expect { described_class.new('d1') }.to raise_error(ArgumentError)
11
11
  end
12
12
 
13
- it "doesn't roll a 'd1' dice" do
14
- expect(described_class.roll('1d1')).to eq(true)
13
+ it 'creates fudge dices' do
14
+ expect { described_class.new('df') }.not_to raise_error
15
15
  end
16
16
 
17
- it 'returns the right number of dices' do
18
- expect(described_class.roll('2d6').count).to eq(2)
17
+ it "creates a dice may the 'd' char be capitalized or not" do
18
+ expect { described_class.new('d6') }.not_to raise_error
19
+ expect { described_class.new('D6') }.not_to raise_error
19
20
  end
20
21
 
21
- it "rolls a dice may the 'd' char be capitalized or not" do
22
- expect(described_class.roll('1d6').count).to eq(1)
23
- expect(described_class.roll('1D6').count).to eq(1)
22
+ it "creates a dice may the 'f' char be capitalized or not" do
23
+ expect { described_class.new('dF') }.not_to raise_error
24
+ expect { described_class.new('Df') }.not_to raise_error
24
25
  end
25
26
 
26
- it 'rolls a dice with a bonus' do
27
- expect(described_class.roll('1d6+2').count).to eq(1)
27
+ it 'creates a dice with a bonus' do
28
+ expect { described_class.new('d6+2') }.not_to raise_error
28
29
  end
29
30
 
30
- it 'rolls a dice with a malus' do
31
- expect(described_class.roll('1D6-2').count).to eq(1)
31
+ it 'creates a dice with a malus' do
32
+ expect { described_class.new('D6-2') }.not_to raise_error
32
33
  end
33
34
 
34
- it "doesn't roll a dice with an odd symbol" do
35
- expect(described_class.roll('1d6%2')).to eq(true)
35
+ it "doesn't create a dice with an odd symbol" do
36
+ expect { described_class.new('d6%2') }.to raise_error(ArgumentError)
36
37
  end
37
38
  end
@@ -0,0 +1,28 @@
1
+ require 'rpg_tools'
2
+ require 'spec_helper'
3
+
4
+ describe RpgTools::PlayingCardDeck do
5
+ it 'must return a card name' do
6
+ expect(described_class.new(32).card).not_to eq(true)
7
+ expect(described_class.new(32).card).not_to eq(false)
8
+ expect(described_class.new(32).card).not_to eq(nil)
9
+ expect(described_class.new(52).card).not_to eq(true)
10
+ expect(described_class.new(52).card).not_to eq(false)
11
+ expect(described_class.new(52).card).not_to eq(nil)
12
+ end
13
+
14
+ it 'must return 5 cards for a full hand' do
15
+ expect(described_class.new(32).hand.count).to eq(5)
16
+ expect(described_class.new(52).hand.count).to eq(5)
17
+ end
18
+
19
+ it "can't pick the same card twice" do
20
+ expect(described_class.new(32).hand.uniq.count).to eq(5)
21
+ expect(described_class.new(52).hand.uniq.count).to eq(5)
22
+ end
23
+
24
+ it "can't use anything else than a 32 or 52 cards deck" do
25
+ expect { described_class.new(42) }.to raise_error(ArgumentError)
26
+ expect { described_class.new(666) }.to raise_error(ArgumentError)
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RpgTools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yohan Piron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -48,13 +48,15 @@ files:
48
48
  - README.md
49
49
  - Rakefile
50
50
  - lib/rpg_tools.rb
51
- - lib/rpg_tools/card.rb
52
51
  - lib/rpg_tools/coin.rb
53
52
  - lib/rpg_tools/dice.rb
53
+ - lib/rpg_tools/dice_bag.rb
54
+ - lib/rpg_tools/playing_card_deck.rb
54
55
  - rpg_tools.gemspec
55
- - spec/card_spec.rb
56
56
  - spec/coin_spec.rb
57
+ - spec/dice_bag_spec.rb
57
58
  - spec/dice_spec.rb
59
+ - spec/playing_card_deck_spec.rb
58
60
  - spec/spec_helper.rb
59
61
  homepage: https://github.com/Yinfei/rpg_tools.git
60
62
  licenses:
@@ -81,7 +83,8 @@ signing_key:
81
83
  specification_version: 4
82
84
  summary: RpgTools is a toolbox for tabletop games and RPGs
83
85
  test_files:
84
- - spec/card_spec.rb
85
86
  - spec/coin_spec.rb
87
+ - spec/dice_bag_spec.rb
86
88
  - spec/dice_spec.rb
89
+ - spec/playing_card_deck_spec.rb
87
90
  - spec/spec_helper.rb
@@ -1,17 +0,0 @@
1
- module RpgTools
2
- class Card
3
- def self.pick
4
- joker_pick_check == true ? 'Joker' : standard_card
5
- end
6
-
7
- def self.joker_pick_check
8
- [0, 1].include?((0..54).to_a.sample(1).first)
9
- end
10
-
11
- def self.standard_card
12
- number = [(1..10).to_a, 'Jack', 'Queen', 'King'].flatten.sample(1)
13
- color = ['clubs', 'hearts', 'diamonds', 'spades'].sample(1)
14
- [number, color].join(' of ')
15
- end
16
- end
17
- end
data/spec/card_spec.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'rpg_tools'
2
- require 'spec_helper'
3
-
4
- describe RpgTools::Card do
5
- it 'must return a card name' do
6
- expect(described_class.pick).not_to eq(true)
7
- expect(described_class.pick).not_to eq(false)
8
- expect(described_class.pick).not_to eq(nil)
9
- end
10
- end