Class: Gamefic::World::Playbook
Overview
A collection of rules for performing commands.
Instance Attribute Summary collapse
-
#syntaxes ⇒ Array<Gamefic::Syntax>
readonly
An array of available syntaxes.
-
#validators ⇒ Array<Proc>
readonly
An array of defined validators.
Instance Method Summary collapse
-
#actions ⇒ Array<Gamefic::Action>
An array of available actions.
-
#actions_for(verb) ⇒ Array<Class<Action>>
Get an Array of all Actions associated with the specified verb.
-
#disambiguate(&block) ⇒ Object
Set the action for handling ambiguous entity references.
-
#disambiguator ⇒ Object
Get the action for handling ambiguous entity references.
-
#dispatch(actor, *command) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified command, that the actor can potentially execute.
-
#dispatch_from_params(actor, verb, params) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified verb and params, that the actor can potentially execute.
-
#dispatch_from_string(actor, text) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified command, that the actor can potentially execute.
-
#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) {|| ... } ⇒ Class<Gamefic::Action>
Create a Meta Action that responds to a command.
-
#respond(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>
Create an Action that responds to a command.
-
#validate(&block) ⇒ Object
Add a block that determines whether an action can be executed.
-
#verbs ⇒ Array<Symbol>
An array of recognized verbs.
Constructor Details
#initialize(commands: {}, syntaxes: [], validators: [], disambiguator: nil) ⇒ Playbook
Returns a new instance of Playbook.
16 17 18 19 20 21 |
# File 'lib/gamefic/world/playbook.rb', line 16 def initialize commands: {}, syntaxes: [], validators: [], disambiguator: nil @commands = commands @syntaxes = syntaxes @validators = validators @disambiguator = disambiguator end |
Instance Attribute Details
#syntaxes ⇒ Array<Gamefic::Syntax> (readonly)
An array of available syntaxes.
9 10 11 |
# File 'lib/gamefic/world/playbook.rb', line 9 def syntaxes @syntaxes end |
#validators ⇒ Array<Proc> (readonly)
An array of defined validators.
14 15 16 |
# File 'lib/gamefic/world/playbook.rb', line 14 def validators @validators end |
Instance Method Details
#actions ⇒ Array<Gamefic::Action>
An array of available actions.
26 27 28 |
# File 'lib/gamefic/world/playbook.rb', line 26 def actions @commands.values.flatten end |
#actions_for(verb) ⇒ Array<Class<Action>>
Get an Array of all Actions associated with the specified verb.
66 67 68 |
# File 'lib/gamefic/world/playbook.rb', line 66 def actions_for verb @commands[verb] || [] end |
#disambiguate(&block) ⇒ Object
Set the action for handling ambiguous entity references.
51 52 53 54 |
# File 'lib/gamefic/world/playbook.rb', line 51 def disambiguate &block @disambiguator = Action.subclass(nil, Query::Base.new, meta: true, &block) @disambiguator end |
#disambiguator ⇒ Object
Get the action for handling ambiguous entity references.
39 40 41 42 43 44 45 46 47 |
# File 'lib/gamefic/world/playbook.rb', line 39 def disambiguator @disambiguator ||= Action.subclass(nil, Query::Base.new) do |actor, entities| definites = [] entities.each do |entity| definites.push entity.definitely end actor.tell "I don't know which you mean: #{definites.join_or}." end end |
#dispatch(actor, *command) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified command, that the actor can potentially execute. The command can either be a single string (e.g., “examine book”) or a list of tokens (e.g., :examine, @book).
147 148 149 150 151 152 |
# File 'lib/gamefic/world/playbook.rb', line 147 def dispatch(actor, *command) result = [] result.concat dispatch_from_params(actor, command[0], command[1..-1]) if command.length > 1 result.concat dispatch_from_string(actor, command.join(' ')) if result.empty? result end |
#dispatch_from_params(actor, verb, params) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified verb and params, that the actor can potentially execute.
176 177 178 179 180 181 182 183 |
# File 'lib/gamefic/world/playbook.rb', line 176 def dispatch_from_params actor, verb, params result = [] available = actions_for(verb) available.each do |a| result.unshift a.new(actor, params) if a.valid?(actor, params) end sort_and_reduce_actions result end |
#dispatch_from_string(actor, text) ⇒ Array<Gamefic::Action>
Get an array of actions, derived from the specified command, that the actor can potentially execute. The command should be a plain-text string, e.g., “examine the book.”
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/gamefic/world/playbook.rb', line 159 def dispatch_from_string actor, text result = [] commands = Syntax.tokenize(text, actor.syntaxes) commands.each do |c| actions_for(c.verb).each do |a| next if a.hidden? o = a.attempt(actor, c.arguments) result.unshift o unless o.nil? end end 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.
190 191 192 |
# File 'lib/gamefic/world/playbook.rb', line 190 def dup Playbook.new commands: @commands.dup, syntaxes: @syntaxes.dup end |
#freeze ⇒ Object
194 195 196 197 |
# File 'lib/gamefic/world/playbook.rb', line 194 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.
135 136 137 138 139 |
# File 'lib/gamefic/world/playbook.rb', line 135 def interpret(input, translation) syn = Syntax.new(input, translation) add_syntax syn syn end |
#meta(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>
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.
115 116 117 118 119 |
# File 'lib/gamefic/world/playbook.rb', line 115 def (verb, *queries, &proc) act = Action.subclass verb, *queries, meta: true, &proc add_action act act end |
#respond(verb, *queries) {|| ... } ⇒ Class<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).
93 94 95 96 97 |
# File 'lib/gamefic/world/playbook.rb', line 93 def respond(verb, *queries, &proc) act = Action.subclass verb, *queries, &proc add_action act act end |
#validate(&block) ⇒ Object
Add a block that determines whether an action can be executed.
58 59 60 |
# File 'lib/gamefic/world/playbook.rb', line 58 def validate &block @validators.push block end |