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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2303467e7858c51a92faeeb841c25048bf7ad744
4
- data.tar.gz: a62fdd87ace512e90d1cf660642482760437ab8c
3
+ metadata.gz: d0f5efd6fa78b6b445096266758357d4a397b6cf
4
+ data.tar.gz: e020ae53ff35f27661b20fc1c7a8be50d251c9db
5
5
  SHA512:
6
- metadata.gz: fb1dcf8a0d11abb17c71a0b973df1af967db9b49db2799ec0634da9c476d9dbeea25f569972d492422b6503a41ab5c3a4eaf54ee2b50955ed7ba7e1b9e93b22e
7
- data.tar.gz: 4b265adcc28d757b34e3d921f6f9f06fcfff498693ef4e192bae04583de43c3dff202a864a299d20fd92f6294a97305a75e343ca1b62334d57c250e6ad574395
6
+ metadata.gz: 4a6b0ba6fd88cc0064534224c3abfaba8a40b7ef056a194431c7b11a31c6fa2a0dfdeebbd2fd3666b346f0a25130ee629967a87fe886f1ebcea73b589699eb22
7
+ data.tar.gz: afefce70890f7c2be9a2c43078e752fc717bbea70864c613d0a372533798025eabc21f4bfb68c229e09aa1609c0f8cb2934af348a64d380374205c5c5898854d
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
- pkg
1
+ /pkg
2
2
  *.swp
3
+ /Gemfile.lock
4
+ /coverage
@@ -0,0 +1,9 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+ Metrics/ModuleLength:
4
+ Enabled: false
5
+ Metrics/BlockLength:
6
+ Enabled: true
7
+ Exclude:
8
+ - res/*.rb
9
+ - spec/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # Copyright 2017 Jamie Hale
2
+ #
3
+ # This file is part of the Tablescript gem.
4
+ #
5
+ # Tablescript 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 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 Tablescript. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ source 'https://rubygems.org'
19
+
20
+ gemspec
data/README.md CHANGED
@@ -1,24 +1,24 @@
1
1
  # Tablescript.rb
2
2
 
3
- This is a Ruby Gem that helps RPG-playing nerds like me generate random things from tables.
3
+ This is a Ruby gem that helps RPG-playing nerds like me generate random things from tables.
4
4
 
5
5
  For example, a Dungeons & Dragons wandering monster table like this:
6
6
 
7
7
  Wandering Monsters (d10):
8
8
 
9
9
  1-5: d6 orcs
10
- 6-9: 3d6 ancient red dragons
11
- 10: a cuddly bunny
10
+ 6-19: 3d6 ancient red dragons
11
+ 20: a cuddly bunny
12
12
 
13
13
  can be automated in Ruby-ish ways like this:
14
14
 
15
15
  table :wandering_monsters do
16
- f( 1..5 ) { "#{roll_dice('d6')} orcs" }
17
- f( 6..9 ) { "#{roll_dice('3d6')} ancient red dragons" }
16
+ f(1..5) { "#{roll('d6')} orcs" }
17
+ f(6..19) { "#{roll('3d6')} ancient red dragons" }
18
18
  f { "a cuddly bunny" }
19
19
  end
20
20
 
21
- puts roll_on( :wandering_monsters )
21
+ puts roll_on(:wandering_monsters)
22
22
 
23
23
  # Syntax
24
24
 
@@ -32,7 +32,7 @@ Define a table as follows:
32
32
 
33
33
  Roll on a table as follows:
34
34
 
35
- roll_on( :table_name )
35
+ roll_on(:table_name)
36
36
 
37
37
  Table entries define blocks that are returned if the die roll matches the entry. Entries can be simple text:
38
38
 
@@ -40,21 +40,21 @@ Table entries define blocks that are returned if the die roll matches the entry.
40
40
 
41
41
  complex/interpolated text:
42
42
 
43
- f { "#{roll_dice('3d6')} cuddly bunnies" }
43
+ f { "#{roll('3d6')} cuddly bunnies" }
44
44
 
45
45
  or arbitrary Ruby code:
46
46
 
47
- f { { :effect => roll_on( :random_limb_loss ), :damage => roll_dice( '4d10' ) }
47
+ f { { effect: roll_on(:random_limb_loss), damage: roll('4d10') }
48
48
 
49
49
  Table entries are either "f" or "d" for "fixed" and "dynamic" respectively.
50
50
 
51
51
  Fixed entries are defined for specify die rolls. For example:
52
52
 
53
- f( 1 ) { ... }
53
+ f(1) { ... }
54
54
 
55
55
  defines the result for the roll of 1.
56
56
 
57
- f( 5..9 ) { ... }
57
+ f(5..9) { ... }
58
58
 
59
59
  defines the result for a roll of 5, 6, 7, 8, or 9.
60
60
 
@@ -62,66 +62,77 @@ defines the result for a roll of 5, 6, 7, 8, or 9.
62
62
 
63
63
  defines the result for the _next_ roll. If it's the first entry, it defaults to 1. Otherwise, it's whatever the previous entry was + 1.
64
64
 
65
- The :wandering_monsters example table above defines 3 entries: 1-5, 6-9, and 10.
65
+ The :wandering\_monsters example table above defines 3 entries: 1-5, 6-9, and 10.
66
66
 
67
67
  Dynamic entries are defined for groups of rolls. For example:
68
68
 
69
- d( 10 ) { ... }
70
- d( 50 ) { ... }
71
- d( 40 ) { ... }
69
+ d(10) { ... }
70
+ d(50) { ... }
71
+ d(40) { ... }
72
72
 
73
73
  defines 3 groups of results. The first is for rolls of 1-10 (i.e. the first 10). The second is for rolls of 11-60 (i.e. the next 50). And the third is for rolls of 51-100 (i.e. the next 40). In this case the total number of entries works out to 100, so the entries are effectively 10%, 50%, and 40%.
74
74
 
75
75
  Entries do not have to total 100. For example:
76
76
 
77
- d( 1 ) { ... }
78
- d( 2 ) { ... }
77
+ d(1) { ... }
78
+ d(2) { ... }
79
79
 
80
80
  defines 2 groups of results where the second has twice the chance of the first. Tablescript will effectively roll a d3.
81
81
 
82
82
  # Reference
83
83
 
84
- Tablescript includes the following global functions:
84
+ Include the Tablescript API into your global namespace as follows:
85
85
 
86
- ## table( name, &blk )
86
+ include Tablescript::Api
87
87
 
88
- Defines a table, as in the above examples.
88
+ The Tablescript API includes the following global functions:
89
89
 
90
- ## roll_on( name )
90
+ ## namespace(name, &blk)
91
91
 
92
- Generates a random number from 1 to the highest defined entry, and returns the corresponding table entry from table named _name_.
92
+ Defines a namespace. Namespaces can contain other namespaces, and tables.
93
93
 
94
- ## roll_on_and_ignore_duplicates( name, times, *args )
94
+ ## table(name, &blk)
95
95
 
96
- Rolls on the _name_ table _times_ times and ignores duplicate rolls.
96
+ Defines a table, as in the above examples. Tables defined inside a namespace are accessible in that namespace, or by providing an absolute (/path/to/table) or relative (path/to/table or ../path/to/table) path. See the examples.
97
97
 
98
- ## lookup( name, roll )
98
+ ## roll\_on(name)
99
99
 
100
- Returns the entry from table _name_ corresponding to the roll _roll_ as if that number had been randomly generated.
100
+ Generates a random number from 1 to the highest defined entry, and returns the corresponding table entry -- evaluated -- from table named _name_.
101
101
 
102
- ## roll_dice( dice )
102
+ ## roll\_on\_and\_ignore(name, \*args)
103
103
 
104
- Returns a random number generated by the dice described by _dice_.
104
+ Rolls on the _name_ table and ignores rolls that match the passed arguments. For example:
105
105
 
106
- The following formats are acceptable:
106
+ roll_on_and_ignore(:wandering_monsters, 1..5)
107
107
 
108
- dX: rolls a single X-sided die
109
- YdX: rolls Y X-sided dice and returns the sum of all rolls
110
- YdXdl[Z]: rolls Y X-sided dice, drops the lowest Z rolls, and returns the sum of the remainder
111
- YdXdh[Z]: rolls Y X-sided dice, drops the highest Z rolls, and returns the sum of the remainder
108
+ will roll until it gets something other than orcs.
112
109
 
113
- ## choose( options )
110
+ roll_on_and_ignore(:wandering_monsters, 1..5, 10)
114
111
 
115
- Returns a random selection from the passed array of options. For example:
112
+ will only return ancient red dragons.
116
113
 
117
- puts choose( [ "red", "green", "blue" ] )
114
+ ## roll\_on\_and\_ignore\_duplicates(name, times)
118
115
 
119
- is equivalent to:
116
+ Rolls on the _name_ table _times_ times and ignores duplicate entries.
120
117
 
121
- table :temporary do
122
- f { "red" }
123
- f { "green" }
124
- f { "blue" }
125
- end
126
-
127
- puts roll_on( :temporary )
118
+ ## lookup(name, roll)
119
+
120
+ Returns the entry from table _name_ corresponding to the roll _roll_ as if that number had been randomly generated.
121
+
122
+ # Installation
123
+
124
+ $> gem install tablescript
125
+
126
+ Include Tablescript:
127
+
128
+ require 'tablescript'
129
+ include Tablescript::Api
130
+
131
+ The include is optional. You can use the Tablescript library directly, or in another namespace if you so choose.
132
+
133
+ # Development
134
+
135
+ Tablescript runs in Ruby 2.4. It hasn't been tested in previous versions.
136
+
137
+ $> rake spec
138
+ $> rake rubocop
data/Rakefile CHANGED
@@ -1,4 +1,28 @@
1
- require "bundler/gem_tasks"
1
+ # Copyright 2017 Jamie Hale
2
+ #
3
+ # This file is part of the Tablescript gem.
4
+ #
5
+ # Tablescript 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 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 Tablescript. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'bundler/gem_tasks'
2
19
  require 'rake/clean'
3
- require 'rubygems'
4
- require 'rubygems/package_task'
20
+ require 'rspec/core/rake_task'
21
+
22
+ RSpec::Core::RakeTask.new(:spec)
23
+
24
+ task default: :spec
25
+
26
+ task :rubocop do
27
+ sh 'bundle exec rubocop'
28
+ end
@@ -0,0 +1,33 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'tablescript'
4
+ include Tablescript::Api
5
+
6
+ namespace :utils do
7
+ namespace :special do
8
+ table :things do
9
+ f { 'lamp' }
10
+ f { 'coin' }
11
+ f { 'tapestry' }
12
+ end
13
+ end
14
+
15
+ table :colours do
16
+ f { 'red' }
17
+ f { 'green' }
18
+ f { 'black' }
19
+ f { 'blue' }
20
+ end
21
+
22
+ table :things do
23
+ f { 'carpet' }
24
+ f { 'chair' }
25
+ f { roll_on('special/things') }
26
+ end
27
+ end
28
+
29
+ table :stuff do
30
+ f { "a #{roll_on('/utils/colours')} #{roll_on('/utils/things')}" }
31
+ end
32
+
33
+ puts roll_on(:stuff)
@@ -0,0 +1,15 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'tablescript'
4
+ include Tablescript::Api
5
+
6
+ require 'rpg_lib'
7
+ include RpgLib::Api
8
+
9
+ table :wandering_monsters do
10
+ f(1..5) { "#{roll('d6')} orcs" }
11
+ f(6..19) { "#{roll('3d6')} ancient red dragons" }
12
+ f(20) { 'a cuddly bunny' }
13
+ end
14
+
15
+ puts roll_on(:wandering_monsters)
@@ -1,81 +1,37 @@
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
-
18
- require 'tablescript/dice_roller'
19
- require 'tablescript/roll_descriptor'
20
- require 'tablescript/table'
21
- require 'tablescript/table_entry'
22
- require 'tablescript/table_entry_environment'
23
-
24
- $LOAD_PATH.push File.expand_path( ENV[ "TS_PATH" ] ) unless ENV[ "TS_PATH" ].nil?
25
-
26
- $all_tables = {}
27
-
28
- def table( name, &blk )
29
- begin
30
- new_table = TableScript::Table.new( name, TableScript::DiceRoller.new )
31
- new_table.build( &blk )
32
- $all_tables[ name ] = new_table
33
- rescue Exception => e
34
- puts e
35
- exit
36
- end
37
- end
16
+ # along with Tablescript. If not, see <http://www.gnu.org/licenses/>.
38
17
 
39
- def roll_on( name )
40
- raise "No table named '#{name}'" if $all_tables[ name ].nil?
41
- $all_tables[ name ].roll
42
- end
18
+ require 'rpg_lib'
43
19
 
44
- def roll_on_and_ignore_duplicates( name, times, *args )
45
- begin
46
- raise "No table named '#{name}'" if $all_tables[ name ].nil?
47
- $all_tables[ name ].roll_and_ignore_duplicates( times, args )
48
- rescue Exception => e
49
- puts e
50
- exit
51
- end
52
- end
53
-
54
- def lookup( name, roll )
55
- begin
56
- raise "No table named '#{name}'" if $all_tables[ name ].nil?
57
- $all_tables[ name ].lookup( roll )
58
- rescue Exception => e
59
- puts e
60
- exit
61
- end
62
- end
63
-
64
- def roll_dice( dice )
65
- begin
66
- TableScript::DiceRoller.new.roll( dice.dup )
67
- rescue Exception => e
68
- puts e
69
- exit
70
- end
71
- end
20
+ require 'tablescript/version'
21
+ require 'tablescript/exception'
22
+ require 'tablescript/roll_strategy'
23
+ require 'tablescript/roll_and_ignore_strategy'
24
+ require 'tablescript/roll_and_ignore_duplicates_strategy'
25
+ require 'tablescript/lookup_strategy'
26
+ require 'tablescript/table_entries'
27
+ require 'tablescript/table'
28
+ require 'tablescript/table_entry'
29
+ require 'tablescript/roll_context'
72
30
 
73
- def choose( options )
74
- options[ TableScript::DiceRoller.new.random_value_in_range( 1..options.size ) - 1 ]
75
- end
31
+ $LOAD_PATH.push File.expand_path(ENV['TS_PATH']) unless ENV['TS_PATH'].nil?
76
32
 
77
- class String
78
- def roll
79
- roll_dice(self)
80
- end
81
- end
33
+ require 'tablescript/namespace_generator'
34
+ require 'tablescript/namespace'
35
+ require 'tablescript/library'
36
+ require 'tablescript/api'
37
+ require 'tablescript/library_dumper'
@@ -0,0 +1,50 @@
1
+ # Copyright 2017 Jamie Hale
2
+ #
3
+ # This file is part of the Tablescript gem.
4
+ #
5
+ # Tablescript 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 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 Tablescript. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ module Tablescript
19
+ ##
20
+ # Api
21
+ #
22
+ module Api
23
+ def namespace(name, &blk)
24
+ generator = NamespaceGenerator.new(Library.instance.root.namespace(name.to_s))
25
+ generator.instance_eval(&blk)
26
+ end
27
+
28
+ def table(name, &blk)
29
+ root_namespace = Library.instance.root
30
+ table = Table.new(name.to_s, root_namespace, &blk)
31
+ root_namespace.add(table)
32
+ end
33
+
34
+ def roll_on(path)
35
+ RollStrategy.new(Library.instance.table(path.to_s)).value
36
+ end
37
+
38
+ def roll_on_and_ignore(path, *args)
39
+ RollAndIgnoreStrategy.new(Library.instance.table(path.to_s), RpgLib::RollSet.new(*args)).value
40
+ end
41
+
42
+ def roll_on_and_ignore_duplicates(path, times)
43
+ RollAndIgnoreDuplicatesStrategy.new(Library.instance.table(path.to_s), times).values
44
+ end
45
+
46
+ def lookup(path, roll)
47
+ LookupStrategy.new(Library.instance.table(path.to_s), roll).value
48
+ end
49
+ end
50
+ end