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.
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
@@ -1,55 +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 RollDescriptor
21
-
22
- attr_reader :die, :count, :drop_lowest, :drop_highest
23
-
24
- def initialize( match )
25
- initialize_die( match )
26
- initialize_count( match )
27
- initialize_dropped_dice( match )
28
- end
29
-
30
- private
31
-
32
- def initialize_die( match )
33
- @die = match[ 2 ].to_i
34
- end
35
-
36
- def initialize_count( match )
37
- @count = 1
38
- @count = match[ 1 ].to_i unless match[ 1 ].empty?
39
- end
40
-
41
- def initialize_dropped_dice( match )
42
- @drop_lowest = 0
43
- @drop_highest = 0
44
- if match[ 4 ] == "dl"
45
- @drop_lowest = 1
46
- @drop_lowest = match[ 5 ].to_i unless match[ 5 ].empty?
47
- elsif match[ 6 ] == "dh"
48
- @drop_highest = 1
49
- @drop_highest = match[ 7 ].to_i unless match[ 7 ].empty?
50
- end
51
- end
52
-
53
- end
54
-
55
- end
@@ -1,124 +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 TableEntryEnvironment
21
-
22
- def initialize( table_name, roller )
23
- @table_name = table_name
24
- @roller = roller
25
- @entries = []
26
- end
27
-
28
- def fixed( *args, &blk )
29
- if args.empty?
30
- @entries << TableEntry.new( blk )
31
- else
32
- roll = args.shift
33
- if roll.is_a?(Integer)
34
- @entries[ roll - 1 ] = TableEntry.new( blk )
35
- elsif roll.class == Range
36
- entry = TableEntry.new( blk )
37
- roll.each do |i|
38
- @entries[ i - 1 ] = entry
39
- end
40
- end
41
- raise "Too many parameters for f in table #{@table_name}" unless args.empty?
42
- end
43
- end
44
-
45
- alias :f :fixed
46
-
47
- def dynamic( *args, &blk )
48
- if args.empty?
49
- @entries << TableEntry.new( blk )
50
- else
51
- count = args.shift
52
- if count.is_a?(Integer)
53
- entry = TableEntry.new( blk )
54
- 1.upto count do
55
- @entries << entry
56
- end
57
- end
58
- raise "Too many parameters for d in table #{@table_name}" unless args.empty?
59
- end
60
- end
61
-
62
- alias :d :dynamic
63
-
64
- def dice_to_roll
65
- "d#{@entries.size}"
66
- end
67
-
68
- def lookup( index )
69
- raise "No table entry for a roll of #{index}." if ( index <= 0 or index > @entries.size )
70
- @entries[ index - 1 ].evaluate( index )
71
- end
72
-
73
- def reroll
74
- rolled_value = @roller.roll( dice_to_roll )
75
- lookup( rolled_value )
76
- end
77
-
78
- def reroll_and_ignore( *args )
79
- validate_ignored_values( args )
80
- rolled_value = @roller.roll_and_ignore( dice_to_roll, args )
81
- lookup( rolled_value )
82
- end
83
-
84
- def reroll_and_ignore_duplicates( times, *args )
85
- ignored_entries = entries_from_ignored_values( args )
86
- evaluated_rolled_entries = []
87
- until evaluated_rolled_entries.size == times do
88
- rolled_value = @roller.roll( dice_to_roll )
89
- rolled_entry = @entries[ rolled_value - 1 ]
90
- unless ignored_entries.include? rolled_entry
91
- ignored_entries << rolled_entry
92
- evaluated_rolled_entries << rolled_entry.evaluate( rolled_value )
93
- end
94
- end
95
- evaluated_rolled_entries
96
- end
97
-
98
- def method_missing( method_id, *args )
99
- raise "Undefined command '#{method_id}' in table #{@table_name}"
100
- end
101
-
102
- private
103
-
104
- def entries_from_ignored_values( args )
105
- entries = []
106
- args.each do |arg|
107
- if arg.is_a?(Integer)
108
- entries << lookup( arg )
109
- elsif arg.class == Range
110
- arg.each do |i|
111
- entries << lookup( arg )
112
- end
113
- end
114
- end
115
- entries.uniq
116
- end
117
-
118
- def validate_ignored_values( args )
119
- raise "No ignored values specified" if args.empty?
120
- end
121
-
122
- end
123
-
124
- end