Class: Gamefic::Plot::Playbook

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/plot/playbook.rb

Instance Method Summary collapse

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

#actionsObject



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.

Parameters:

  • verb (Symbol)

    The Symbol for the verb (e.g., :go or :look)

Returns:



52
53
54
# File 'lib/gamefic/plot/playbook.rb', line 52

def actions_for verb
  @commands[verb] || []
end

#disambiguatorObject



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
136
137
138
139
140
141
142
# 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.sort! { |a,b|
    if a.rank == b.rank
      b.order_key <=> a.order_key
    else
      b.rank <=> a.rank
    end
  }
  result.uniq{|a| a.class}
end

#dispatch_from_params(actor, verb, params) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/gamefic/plot/playbook.rb', line 157

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)
  }
  result
end

#dispatch_from_string(actor, text) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gamefic/plot/playbook.rb', line 144

def dispatch_from_string actor, text
  result = []
  commands = Syntax.tokenize(text, syntaxes)
  commands.each { |c|
    available = actions_for(c.verb)
    available.each { |a|
      o = a.attempt(actor, c.arguments)
      result.unshift o unless o.nil?
    }
  }
  result
end

#dupPlaybook

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.

Returns:



171
172
173
# File 'lib/gamefic/plot/playbook.rb', line 171

def dup
  Playbook.new commands: @commands.dup, syntaxes: @syntaxes.dup
end

#freezeObject



175
176
177
178
# File 'lib/gamefic/plot/playbook.rb', line 175

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.

Examples:

Create a synonym for the Inventory Action.

interpret "catalogue", "inventory"
# The command "catalogue" will be translated to "inventory"

Create a parameterized synonym for the Look Action.

interpret "scrutinize :entity", "look :entity"
# The command "scrutinize chair" will be translated to "look chair"

Parameters:

  • input (String)

    The format of the original command

  • translation (String)

    The format of the translated command

Returns:

  • (Syntax)

    the Syntax object



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.

Examples:

A simple Meta Action

meta :credits do |actor|
  actor.tell "This game was written by John Smith."
end

Parameters:

  • verb (Symbol)

    An imperative verb for the command

  • queries (Array<Query::Base>)

    Filters for the command’s tokens

Yield Parameters:



100
101
102
103
104
# File 'lib/gamefic/plot/playbook.rb', line 100

def meta(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).

Examples:

A simple Action.

respond :salute do |actor|
  actor.tell "Hello, sir!"
end
# The command "salute" will respond "Hello, sir!"

An Action that accepts a Character

respond :salute, Use.visible(Character) do |actor, character|
  actor.tell "#{The character} returns your salute."
end

Parameters:

  • verb (Symbol)

    An imperative verb for the command

  • queries (Array<Query::Base>)

    Filters for the command’s tokens

Yield Parameters:

Returns:



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

#syntaxesObject



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.meta = true
@disambiguator

end



44
45
46
# File 'lib/gamefic/plot/playbook.rb', line 44

def validate &block
  @validators.push block
end

#validatorsObject



24
25
26
# File 'lib/gamefic/plot/playbook.rb', line 24

def validators
  @validators
end

#verbsObject



20
21
22
# File 'lib/gamefic/plot/playbook.rb', line 20

def verbs
  @commands.keys
end