tablescript 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rubocop.yml +9 -0
  4. data/Gemfile +20 -0
  5. data/README.md +55 -44
  6. data/Rakefile +27 -3
  7. data/examples/namespaces.rb +33 -0
  8. data/examples/wandering_monsters.rb +15 -0
  9. data/lib/tablescript.rb +22 -66
  10. data/lib/tablescript/api.rb +50 -0
  11. data/lib/tablescript/exception.rb +24 -0
  12. data/lib/tablescript/library.rb +44 -0
  13. data/lib/tablescript/library_dumper.rb +42 -0
  14. data/lib/tablescript/lookup_strategy.rb +41 -0
  15. data/lib/tablescript/namespace.rb +85 -0
  16. data/lib/tablescript/namespace_generator.rb +20 -0
  17. data/lib/tablescript/roll_and_ignore_duplicates_strategy.rb +69 -0
  18. data/lib/tablescript/roll_and_ignore_strategy.rb +49 -0
  19. data/lib/tablescript/roll_context.rb +51 -0
  20. data/lib/tablescript/roll_strategy.rb +48 -0
  21. data/lib/tablescript/table.rb +54 -32
  22. data/lib/tablescript/table_entries.rb +66 -0
  23. data/lib/tablescript/table_entry.rb +18 -14
  24. data/lib/tablescript/version.rb +8 -8
  25. data/spec/spec_helper.rb +4 -0
  26. data/spec/tablescript/api_spec.rb +66 -0
  27. data/spec/tablescript/library_spec.rb +13 -0
  28. data/spec/tablescript/lookup_strategy_spec.rb +35 -0
  29. data/spec/tablescript/namespace_spec.rb +25 -0
  30. data/spec/tablescript/roll_and_ignore_strategy_spec.rb +57 -0
  31. data/spec/tablescript/roll_strategy_spec.rb +56 -0
  32. data/spec/tablescript/table_entry_spec.rb +42 -0
  33. data/spec/tablescript/table_spec.rb +88 -0
  34. data/spec/tablescript_spec.rb +7 -0
  35. data/tablescript.gemspec +26 -20
  36. metadata +112 -5
  37. data/lib/tablescript/dice_roller.rb +0 -94
  38. data/lib/tablescript/roll_descriptor.rb +0 -55
  39. data/lib/tablescript/table_entry_environment.rb +0 -124
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Tablescript
4
+ describe RollStrategy do
5
+ it 'can be created' do
6
+ expect { RollStrategy.new(:table) }.not_to raise_error
7
+ end
8
+
9
+ describe 'evaluate' do
10
+ let(:dice) { 'd20' }
11
+ let(:table) { double('table', dice_to_roll: dice) }
12
+ let(:roll) { 6 }
13
+ let(:value) { 'Vorpal sword' }
14
+ let(:roller) { RpgLib::DiceRoller.clone.instance }
15
+ let(:strategy) { RollStrategy.new(table, roller) }
16
+
17
+ before(:each) do
18
+ allow(roller).to receive(:roll) { roll }
19
+ allow(table).to receive(:evaluate) { value }
20
+ @value = strategy.value
21
+ end
22
+
23
+ it 'defers to the roller' do
24
+ expect(roller).to have_received(:roll).with(dice)
25
+ end
26
+
27
+ it 'defers to the table' do
28
+ expect(table).to have_received(:evaluate).with(roll)
29
+ end
30
+
31
+ describe 'value' do
32
+ it 'provides the tables result' do
33
+ expect(@value).to eq(value)
34
+ end
35
+
36
+ it 'only evaluates once' do
37
+ strategy.value
38
+ strategy.value
39
+ expect(table).to have_received(:evaluate).once
40
+ end
41
+ end
42
+
43
+ describe 'roll' do
44
+ it 'knows its roll' do
45
+ expect(strategy.roll).to eq(roll)
46
+ end
47
+
48
+ it 'only evaluates once' do
49
+ strategy.roll
50
+ strategy.roll
51
+ expect(table).to have_received(:evaluate).once
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ module Tablescript
4
+ describe TableEntry do
5
+ it 'can be created' do
6
+ expect { TableEntry.new(:id, :roll, :blk) }.not_to raise_error
7
+ end
8
+
9
+ describe 'once created' do
10
+ let(:entry) { TableEntry.new(:id, :roll, :blk) }
11
+
12
+ it 'knows its id' do
13
+ expect(entry.id).to eq(:id)
14
+ end
15
+
16
+ it 'knows its roll' do
17
+ expect(entry.roll).to eq(:roll)
18
+ end
19
+
20
+ it 'knows its blk' do
21
+ expect(entry.blk).to eq(:blk)
22
+ end
23
+ end
24
+
25
+ describe 'evaluate' do
26
+ let(:roll) { 5 }
27
+ let(:blk) { proc { 99 } }
28
+ let(:table) { double('table') }
29
+ let(:environment) { double('environment') }
30
+ let(:entry) { TableEntry.new(:id, roll, blk) }
31
+
32
+ before(:each) do
33
+ allow(environment).to receive(:instance_eval) { 99 }
34
+ end
35
+
36
+ it 'defers to the environment for evaluation' do
37
+ entry.evaluate(roll, table, environment)
38
+ expect(environment).to have_received(:instance_eval)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Tablescript
4
+ describe Table do
5
+ it 'can be created' do
6
+ expect { Table.new(:name, :namespace, :entries) }.not_to raise_error
7
+ end
8
+
9
+ describe 'dice_to_roll' do
10
+ describe 'small table' do
11
+ let(:table) { Table.new(:name, :namespace, [:entry] * 5) }
12
+
13
+ it 'knows its size' do
14
+ expect(table.size).to be 5
15
+ end
16
+
17
+ it 'knows what to roll' do
18
+ expect(table.dice_to_roll).to eq('d5')
19
+ end
20
+ end
21
+
22
+ describe 'large table' do
23
+ let(:table) { Table.new(:name, :namespace, [:entry] * 100) }
24
+
25
+ it 'knows its size' do
26
+ expect(table.size).to be 100
27
+ end
28
+
29
+ it 'knows what to roll' do
30
+ expect(table.dice_to_roll).to eq('d100')
31
+ end
32
+ end
33
+ end
34
+
35
+ describe 'entries' do
36
+ let(:entry1) { double('entry1') }
37
+ let(:entry2) { double('entry2') }
38
+ let(:entry3) { double('entry3') }
39
+ let(:entry4) { double('entry4') }
40
+ let(:entries) { [entry1, entry2, entry3, entry4] }
41
+ let(:table) { Table.new(:name, :namespace, entries) }
42
+
43
+ describe 'lookup' do
44
+ it 'knows its entries by roll' do
45
+ 1.upto(4) do |i|
46
+ expect(table.lookup(i)).to eq entries[i - 1]
47
+ end
48
+ end
49
+
50
+ it 'throws for an unset roll' do
51
+ expect { table.lookup(100) }.to raise_error Exception
52
+ end
53
+ end
54
+
55
+ describe 'entry' do
56
+ it 'knows its entries by index' do
57
+ 0.upto(3) do |i|
58
+ expect(table.entry(i)).to eq entries[i]
59
+ end
60
+ end
61
+
62
+ it 'returns nil for an unset index' do
63
+ expect(table.entry(100)).to be_nil
64
+ end
65
+ end
66
+ end
67
+
68
+ describe 'evaluate' do
69
+ let(:entry) { double('entry') }
70
+ let(:entries) { [entry] }
71
+ let(:value) { 9 }
72
+ let(:table) { Table.new(:name, :namespace, entries) }
73
+
74
+ before(:each) do
75
+ allow(entry).to receive(:evaluate) { value }
76
+ @value = table.evaluate(1)
77
+ end
78
+
79
+ it 'defers to the entry' do
80
+ expect(entry).to have_received(:evaluate).with(1, table)
81
+ end
82
+
83
+ it 'returns the value returned by the entry' do
84
+ expect(@value).to eq(value)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tablescript do
4
+ it 'has a version number' do
5
+ expect(Tablescript::VERSION).not_to be nil
6
+ end
7
+ end
@@ -1,38 +1,44 @@
1
- # Copyright 2015 Jamie Hale
1
+ # Copyright 2017 Jamie Hale
2
2
  #
3
- # This file is part of the Tablescript.rb gem.
3
+ # This file is part of the Tablescript gem.
4
4
  #
5
- # Tablescript.rb is free software: you can redistribute it and/or modify
5
+ # Tablescript is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU General Public License as published by
7
7
  # the Free Software Foundation, either version 3 of the License, or
8
8
  # (at your option) any later version.
9
9
  #
10
- # Tablescript.rb is distributed in the hope that it will be useful,
10
+ # Tablescript is distributed in the hope that it will be useful,
11
11
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
13
  # GNU General Public License for more details.
14
14
  #
15
15
  # You should have received a copy of the GNU General Public License
16
- # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
17
-
16
+ # along with Tablescript. If not, see <http://www.gnu.org/licenses/>.
17
+
18
18
  # coding: utf-8
19
19
  lib = File.expand_path('../lib', __FILE__)
20
20
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
21
21
 
22
22
  require 'tablescript/version'
23
23
 
24
- Gem::Specification.new do |s|
25
- s.name = 'tablescript'
26
- s.version = TableScript::VERSION
27
- s.date = '2015-05-05'
28
- s.summary = "TableScript RPG Tool"
29
- s.description = "Library for creating role-playing game random tables."
30
- s.authors = [ 'Jamie Hale' ]
31
- s.email = [ 'jamie@smallarmyofnerds.com' ]
32
- s.homepage = 'http://smallarmyofnerds.com/tablescript'
33
- s.license = "GPL-3.0"
34
- s.platform = Gem::Platform::RUBY
35
- s.files = `git ls-files -z`.split( "\x0" )
36
- s.require_paths = [ 'lib' ]
37
- s.metadata['allowed_push_host'] = 'https://rubygems.org'
24
+ Gem::Specification.new do |spec|
25
+ spec.name = 'tablescript'
26
+ spec.version = Tablescript::VERSION
27
+ spec.date = '2015-05-05'
28
+ spec.summary = 'TableScript RPG Tool'
29
+ spec.description = 'Library for creating role-playing game random tables.'
30
+ spec.authors = ['Jamie Hale']
31
+ spec.email = ['jamie@smallarmyofnerds.com']
32
+ spec.homepage = 'http://smallarmyofnerds.com/tablescript'
33
+ spec.license = 'GPL-3.0'
34
+ spec.platform = Gem::Platform::RUBY
35
+ spec.files = `git ls-files -z`.split("\x0")
36
+ spec.require_paths = ['lib']
37
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
38
+ spec.add_dependency 'rpg_lib', '~> 1.0'
39
+ spec.add_development_dependency 'bundler', '~> 1.14'
40
+ spec.add_development_dependency 'rake', '~> 12.0'
41
+ spec.add_development_dependency 'rspec', '~> 3.5'
42
+ spec.add_development_dependency 'simplecov', '~> 0.13'
43
+ spec.add_development_dependency 'rubocop', '~> 0.47'
38
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablescript
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Hale
@@ -9,7 +9,91 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-05-05 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rpg_lib
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.47'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.47'
13
97
  description: Library for creating role-playing game random tables.
14
98
  email:
15
99
  - jamie@smallarmyofnerds.com
@@ -18,16 +102,39 @@ extensions: []
18
102
  extra_rdoc_files: []
19
103
  files:
20
104
  - ".gitignore"
105
+ - ".rubocop.yml"
21
106
  - COPYING
107
+ - Gemfile
22
108
  - README.md
23
109
  - Rakefile
110
+ - examples/namespaces.rb
111
+ - examples/wandering_monsters.rb
24
112
  - lib/tablescript.rb
25
- - lib/tablescript/dice_roller.rb
26
- - lib/tablescript/roll_descriptor.rb
113
+ - lib/tablescript/api.rb
114
+ - lib/tablescript/exception.rb
115
+ - lib/tablescript/library.rb
116
+ - lib/tablescript/library_dumper.rb
117
+ - lib/tablescript/lookup_strategy.rb
118
+ - lib/tablescript/namespace.rb
119
+ - lib/tablescript/namespace_generator.rb
120
+ - lib/tablescript/roll_and_ignore_duplicates_strategy.rb
121
+ - lib/tablescript/roll_and_ignore_strategy.rb
122
+ - lib/tablescript/roll_context.rb
123
+ - lib/tablescript/roll_strategy.rb
27
124
  - lib/tablescript/table.rb
125
+ - lib/tablescript/table_entries.rb
28
126
  - lib/tablescript/table_entry.rb
29
- - lib/tablescript/table_entry_environment.rb
30
127
  - lib/tablescript/version.rb
128
+ - spec/spec_helper.rb
129
+ - spec/tablescript/api_spec.rb
130
+ - spec/tablescript/library_spec.rb
131
+ - spec/tablescript/lookup_strategy_spec.rb
132
+ - spec/tablescript/namespace_spec.rb
133
+ - spec/tablescript/roll_and_ignore_strategy_spec.rb
134
+ - spec/tablescript/roll_strategy_spec.rb
135
+ - spec/tablescript/table_entry_spec.rb
136
+ - spec/tablescript/table_spec.rb
137
+ - spec/tablescript_spec.rb
31
138
  - tablescript.gemspec
32
139
  homepage: http://smallarmyofnerds.com/tablescript
33
140
  licenses:
@@ -1,94 +0,0 @@
1
- # Copyright 2015 Jamie Hale
2
- #
3
- # This file is part of the Tablescript.rb gem.
4
- #
5
- # Tablescript.rb is free software: you can redistribute it and/or modify
6
- # it under the terms of the GNU General Public License as published by
7
- # the Free Software Foundation, either version 3 of the License, or
8
- # (at your option) any later version.
9
- #
10
- # Tablescript.rb is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- # GNU General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU General Public License
16
- # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- module TableScript
19
-
20
- class DiceRoller
21
-
22
- @@DICE_REGEXP = /(\d*)d(\d+)((dl)(\d*)|(dh)(\d*))?/
23
-
24
- def random_value_in_range( range )
25
- rand( range )
26
- end
27
-
28
- def roll_dice( roll_descriptor )
29
- rolled_values = roll_all_dice_from_descriptor( roll_descriptor )
30
- drop_lowest( rolled_values, roll_descriptor )
31
- drop_highest( rolled_values, roll_descriptor )
32
- total( rolled_values )
33
- end
34
-
35
- def roll( dice )
36
- while m = dice.downcase.match( @@DICE_REGEXP ) do
37
- rolled_value = roll_dice( RollDescriptor.new( m ) )
38
- dice[ m.begin( 0 )...m.end( 0 ) ] = rolled_value.to_s
39
- end
40
- eval( dice )
41
- end
42
-
43
- def roll_and_ignore( dice, args )
44
- ignored_values = collect_ignored_values( args )
45
- rolled_value = nil
46
- while rolled_value.nil? do
47
- rolled_value = roll( dice )
48
- if ignored_values.include? rolled_value
49
- rolled_value = nil
50
- end
51
- end
52
- rolled_value
53
- end
54
-
55
- private
56
-
57
- def roll_all_dice_from_descriptor( roll_descriptor )
58
- rolled_values = []
59
- 1.upto roll_descriptor.count do
60
- rolled_values << random_value_in_range( 1..roll_descriptor.die )
61
- end
62
- rolled_values.sort
63
- end
64
-
65
- def drop_lowest( rolled_values, roll_descriptor )
66
- rolled_values.slice!( 0, roll_descriptor.drop_lowest )
67
- end
68
-
69
- def drop_highest( rolled_values, roll_descriptor )
70
- rolled_values.slice!( rolled_values.size - roll_descriptor.drop_highest, roll_descriptor.drop_highest )
71
- end
72
-
73
- def total( rolled_values )
74
- rolled_values.inject( :+ )
75
- end
76
-
77
- def collect_ignored_values( args )
78
- ignored_values = []
79
- until args.empty? do
80
- value = args.shift
81
- if value.is_a?(Integer)
82
- ignored_values << value
83
- elsif value.class == Range
84
- value.each do |i|
85
- ignored_values << i
86
- end
87
- end
88
- end
89
- ignored_values.uniq
90
- end
91
-
92
- end
93
-
94
- end