Class: Gamefic::Plot::Playbook
- Inherits:
-
Object
- Object
- Gamefic::Plot::Playbook
- Defined in:
- lib/gamefic/plot/playbook.rb
Instance Method Summary collapse
- #actions ⇒ Object
-
#actions_for(verb) ⇒ Array<Action>
Get an Array of all Actions associated with the specified verb.
- #disambiguator ⇒ Object
- #dispatch(actor, *command) ⇒ Object
- #dispatch_from_params(actor, verb, params) ⇒ Object
- #dispatch_from_string(actor, text) ⇒ Object
-
#dup ⇒ Playbook
Duplicate the playbook.
- #freeze ⇒ Object
-
#initialize(commands: {}, syntaxes: [], validators: [], disambiguator: nil) ⇒ Playbook
constructor
A new instance of Playbook.
-
#interpret(input, translation) ⇒ Syntax
Create an alternate Syntax for an Action.
-
#meta(verb, *queries) {|| ... } ⇒ Object
Create a Meta Action that responds to a command.
-
#respond(verb, *queries) {|| ... } ⇒ Gamefic::Action
Create an Action that responds to a command.
- #syntaxes ⇒ Object
-
#validate(&block) ⇒ Object
def disambiguate &block @disambiguator = Action.new(nil, Query::Base.new, &block) @disambiguator.meta = true @disambiguator end.
- #validators ⇒ Object
- #verbs ⇒ Object
Constructor Details
#initialize(commands: {}, syntaxes: [], validators: [], disambiguator: nil) ⇒ Playbook
Returns a new instance of Playbook.
5 6 7 8 9 10 |
# File 'lib/gamefic/plot/playbook.rb', line 5 def initialize commands: {}, syntaxes: [], validators: [], disambiguator: nil @commands = commands @syntaxes = syntaxes @validators = validators @disambiguator = disambiguator end |
Instance Method Details
#actions ⇒ Object
16 17 18 |
# File 'lib/gamefic/plot/playbook.rb', line 16 def actions @commands.values.flatten end |
#actions_for(verb) ⇒ Array<Action>
Get an Array of all Actions associated with the specified verb.
52 53 54 |
# File 'lib/gamefic/plot/playbook.rb', line 52 def actions_for verb @commands[verb] || [] end |
#disambiguator ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/gamefic/plot/playbook.rb', line 28 def disambiguator @disambiguator ||= Action.new(nil, Query::Base.new) do |actor, entities| definites = [] entities.each { |entity| definites.push entity.definitely } actor.tell "I don't know which you mean: #{definites.join_or}." end end |
#dispatch(actor, *command) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gamefic/plot/playbook.rb', line 126 def dispatch(actor, *command) result = [] if command.length > 1 result.concat dispatch_from_params(actor, command[0], command[1..-1]) end if result.empty? result.concat dispatch_from_string(actor, command.join(' ')) end result end |
#dispatch_from_params(actor, verb, params) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/gamefic/plot/playbook.rb', line 151 def dispatch_from_params actor, verb, params result = [] available = actions_for(verb) available.each { |a| result.unshift a.new(actor, params) if a.valid?(actor, params) } sort_and_reduce_actions result end |
#dispatch_from_string(actor, text) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gamefic/plot/playbook.rb', line 137 def dispatch_from_string actor, text result = [] commands = Syntax.tokenize(text, syntaxes) commands.each { |c| available = actions_for(c.verb) available.each { |a| next if a.hidden? o = a.attempt(actor, c.arguments) result.unshift o unless o.nil? } } sort_and_reduce_actions result end |
#dup ⇒ Playbook
Duplicate the playbook. This method will duplicate the commands hash and the syntax array so the new playbook can be modified without affecting the original.
165 166 167 |
# File 'lib/gamefic/plot/playbook.rb', line 165 def dup Playbook.new commands: @commands.dup, syntaxes: @syntaxes.dup end |
#freeze ⇒ Object
169 170 171 172 |
# File 'lib/gamefic/plot/playbook.rb', line 169 def freeze @commands.freeze @syntaxes.freeze end |
#interpret(input, translation) ⇒ Syntax
Create an alternate Syntax for an Action. The command and its translation can be parameterized.
120 121 122 123 124 |
# File 'lib/gamefic/plot/playbook.rb', line 120 def interpret(input, translation) syn = Syntax.new(input, translation) add_syntax syn syn end |
#meta(verb, *queries) {|| ... } ⇒ Object
Create a Meta Action that responds to a command. Meta Actions are very similar to standard Actions, except the Plot understands them to be commands that operate above and/or outside of the actual game world. Examples of Meta Actions are commands that report the player’s current score, save and restore saved games, or list the game’s credits.
100 101 102 103 104 |
# File 'lib/gamefic/plot/playbook.rb', line 100 def (verb, *queries, &proc) act = Action.subclass verb, *queries, meta: true, &proc add_action act act end |
#respond(verb, *queries) {|| ... } ⇒ Gamefic::Action
Create an Action that responds to a command. An Action uses the command argument to identify the imperative verb that triggers the action. It can also accept queries to tokenize the remainder of the input and filter for particular entities or properties. The block argument contains the code to be executed when the input matches all of the Action’s criteria (i.e., verb and queries).
79 80 81 82 83 |
# File 'lib/gamefic/plot/playbook.rb', line 79 def respond(verb, *queries, &proc) act = Action.subclass verb, *queries, order_key: raise_order_key, &proc add_action act act end |
#syntaxes ⇒ Object
12 13 14 |
# File 'lib/gamefic/plot/playbook.rb', line 12 def syntaxes @syntaxes end |
#validate(&block) ⇒ Object
def disambiguate &block
@disambiguator = Action.new(nil, Query::Base.new, &block)
@disambiguator. = true
@disambiguator
end
44 45 46 |
# File 'lib/gamefic/plot/playbook.rb', line 44 def validate &block @validators.push block end |
#validators ⇒ Object
24 25 26 |
# File 'lib/gamefic/plot/playbook.rb', line 24 def validators @validators end |
#verbs ⇒ Object
20 21 22 |
# File 'lib/gamefic/plot/playbook.rb', line 20 def verbs @commands.keys end |