tablescript 0.0.4 → 1.0.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 +4 -4
- data/.gitignore +3 -1
- data/.rubocop.yml +9 -0
- data/Gemfile +20 -0
- data/README.md +55 -44
- data/Rakefile +27 -3
- data/examples/namespaces.rb +33 -0
- data/examples/wandering_monsters.rb +15 -0
- data/lib/tablescript.rb +22 -66
- data/lib/tablescript/api.rb +50 -0
- data/lib/tablescript/exception.rb +24 -0
- data/lib/tablescript/library.rb +44 -0
- data/lib/tablescript/library_dumper.rb +42 -0
- data/lib/tablescript/lookup_strategy.rb +41 -0
- data/lib/tablescript/namespace.rb +85 -0
- data/lib/tablescript/namespace_generator.rb +20 -0
- data/lib/tablescript/roll_and_ignore_duplicates_strategy.rb +69 -0
- data/lib/tablescript/roll_and_ignore_strategy.rb +49 -0
- data/lib/tablescript/roll_context.rb +51 -0
- data/lib/tablescript/roll_strategy.rb +48 -0
- data/lib/tablescript/table.rb +54 -32
- data/lib/tablescript/table_entries.rb +66 -0
- data/lib/tablescript/table_entry.rb +18 -14
- data/lib/tablescript/version.rb +8 -8
- data/spec/spec_helper.rb +4 -0
- data/spec/tablescript/api_spec.rb +66 -0
- data/spec/tablescript/library_spec.rb +13 -0
- data/spec/tablescript/lookup_strategy_spec.rb +35 -0
- data/spec/tablescript/namespace_spec.rb +25 -0
- data/spec/tablescript/roll_and_ignore_strategy_spec.rb +57 -0
- data/spec/tablescript/roll_strategy_spec.rb +56 -0
- data/spec/tablescript/table_entry_spec.rb +42 -0
- data/spec/tablescript/table_spec.rb +88 -0
- data/spec/tablescript_spec.rb +7 -0
- data/tablescript.gemspec +26 -20
- metadata +112 -5
- data/lib/tablescript/dice_roller.rb +0 -94
- data/lib/tablescript/roll_descriptor.rb +0 -55
- data/lib/tablescript/table_entry_environment.rb +0 -124
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0f5efd6fa78b6b445096266758357d4a397b6cf
|
4
|
+
data.tar.gz: e020ae53ff35f27661b20fc1c7a8be50d251c9db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a6b0ba6fd88cc0064534224c3abfaba8a40b7ef056a194431c7b11a31c6fa2a0dfdeebbd2fd3666b346f0a25130ee629967a87fe886f1ebcea73b589699eb22
|
7
|
+
data.tar.gz: afefce70890f7c2be9a2c43078e752fc717bbea70864c613d0a372533798025eabc21f4bfb68c229e09aa1609c0f8cb2934af348a64d380374205c5c5898854d
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
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
|
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-
|
11
|
-
|
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(
|
17
|
-
f(
|
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(
|
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(
|
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 { "#{
|
43
|
+
f { "#{roll('3d6')} cuddly bunnies" }
|
44
44
|
|
45
45
|
or arbitrary Ruby code:
|
46
46
|
|
47
|
-
f { { :
|
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(
|
53
|
+
f(1) { ... }
|
54
54
|
|
55
55
|
defines the result for the roll of 1.
|
56
56
|
|
57
|
-
f(
|
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 :
|
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(
|
70
|
-
d(
|
71
|
-
d(
|
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(
|
78
|
-
d(
|
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
|
84
|
+
Include the Tablescript API into your global namespace as follows:
|
85
85
|
|
86
|
-
|
86
|
+
include Tablescript::Api
|
87
87
|
|
88
|
-
|
88
|
+
The Tablescript API includes the following global functions:
|
89
89
|
|
90
|
-
##
|
90
|
+
## namespace(name, &blk)
|
91
91
|
|
92
|
-
|
92
|
+
Defines a namespace. Namespaces can contain other namespaces, and tables.
|
93
93
|
|
94
|
-
##
|
94
|
+
## table(name, &blk)
|
95
95
|
|
96
|
-
|
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
|
-
##
|
98
|
+
## roll\_on(name)
|
99
99
|
|
100
|
-
|
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
|
-
##
|
102
|
+
## roll\_on\_and\_ignore(name, \*args)
|
103
103
|
|
104
|
-
|
104
|
+
Rolls on the _name_ table and ignores rolls that match the passed arguments. For example:
|
105
105
|
|
106
|
-
|
106
|
+
roll_on_and_ignore(:wandering_monsters, 1..5)
|
107
107
|
|
108
|
-
|
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
|
-
|
110
|
+
roll_on_and_ignore(:wandering_monsters, 1..5, 10)
|
114
111
|
|
115
|
-
|
112
|
+
will only return ancient red dragons.
|
116
113
|
|
117
|
-
|
114
|
+
## roll\_on\_and\_ignore\_duplicates(name, times)
|
118
115
|
|
119
|
-
|
116
|
+
Rolls on the _name_ table _times_ times and ignores duplicate entries.
|
120
117
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
-
|
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 '
|
4
|
-
|
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)
|
data/lib/tablescript.rb
CHANGED
@@ -1,81 +1,37 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2017 Jamie Hale
|
2
2
|
#
|
3
|
-
# This file is part of the Tablescript
|
3
|
+
# This file is part of the Tablescript gem.
|
4
4
|
#
|
5
|
-
# Tablescript
|
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
|
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
|
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
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
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
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|