class Gamefic::Rulebook::Calls

A collection of responses and syntaxes that constitute the actions available to actors.

Attributes

synonym_syntax_map[R]
verb_response_map[R]

Public Class Methods

new() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 9
def initialize
  @verb_response_map = Hash.new { |hash, key| hash[key] = [] }
  @synonym_syntax_map = Hash.new { |hash, key| hash[key] = [] }
end

Public Instance Methods

add_response(response) click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 47
def add_response response
  verb_response_map[response.verb].unshift response
  sort_responses_for response.verb
  add_syntax response.syntax unless response.verb.to_s.start_with?('_')
  response
end
add_syntax(syntax) click to toggle source

@param syntax [Syntax] @return [Syntax]

# File lib/gamefic/rulebook/calls.rb, line 56
def add_syntax syntax
  raise "No responses exist for \"#{syntax.verb}\"" unless verb_response_map.key?(syntax.verb)

  return if synonym_syntax_map[syntax.synonym].include?(syntax)

  synonym_syntax_map[syntax.synonym].unshift syntax
  sort_syntaxes_for syntax.synonym
  syntax
end
empty?() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 66
def empty?
  verb_response_map.empty? && synonym_syntax_map.empty?
end
freeze() click to toggle source
Calls superclass method
# File lib/gamefic/rulebook/calls.rb, line 14
def freeze
  super
  @verb_response_map.freeze
  @verb_response_map.values.map(&:freeze)
  @synonym_syntax_map.freeze
  @synonym_syntax_map.values.map(&:freeze)
  self
end
responses() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 31
def responses
  verb_response_map.values.flatten
end
responses_for(*verbs) click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 39
def responses_for *verbs
  verbs.flat_map { |verb| verb_response_map.fetch(verb, []) }
end
synonyms() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 27
def synonyms
  synonym_syntax_map.keys.compact.sort
end
syntaxes() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 23
def syntaxes
  synonym_syntax_map.values.flatten
end
syntaxes_for(*synonyms) click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 43
def syntaxes_for *synonyms
  synonyms.flat_map { |syn| synonym_syntax_map.fetch(syn, []) }
end
verbs() click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 35
def verbs
  verb_response_map.keys.compact.sort
end

Private Instance Methods

sort_responses_for(verb) click to toggle source

@param responses [Array<Response>]

# File lib/gamefic/rulebook/calls.rb, line 77
def sort_responses_for verb
  verb_response_map[verb].sort_by!.with_index { |a, i| [a.precision, -i] }.reverse!
end
sort_syntaxes_for(synonym) click to toggle source
# File lib/gamefic/rulebook/calls.rb, line 81
def sort_syntaxes_for synonym
  synonym_syntax_map[synonym].sort! { |a, b| a.compare b }
end