class Gamefic::Dispatcher

The action executor for character commands.

Attributes

actor[R]

@return [Actor]

command[R]

@return [Command]

Public Class Methods

dispatch(actor, input) click to toggle source

@param actor [Active] @param input [String] @return [Dispatcher]

# File lib/gamefic/dispatcher.rb, line 50
def self.dispatch actor, input
  # expressions = Syntax.tokenize(input, actor.epic.syntaxes)
  # new(actor, Command.compose(actor, expressions))
  new(actor, Command.compose(actor, input))
end
dispatch_from_params(actor, verb, params) click to toggle source

@param actor [Active] @param verb [Symbol] @param params [Array<Object>] @return [Dispatcher]

# File lib/gamefic/dispatcher.rb, line 60
def self.dispatch_from_params actor, verb, params
  command = Command.new(verb, params)
  new(actor, command)
end
new(actor, command) click to toggle source

@param actor [Actor] @param command [Command]

# File lib/gamefic/dispatcher.rb, line 11
def initialize actor, command
  @actor = actor
  @command = command
  @executed = false
  Gamefic.logger.info "Dispatching #{command.inspect}"
end

Public Instance Methods

execute() click to toggle source

Run the dispatcher.

@return [Action, nil]

# File lib/gamefic/dispatcher.rb, line 21
def execute
  return if @executed

  @executed = true
  action = next_action
  return unless action

  actor.epic.rulebooks.flat_map { |rlbk| rlbk.run_before_actions action }
  return if action.cancelled?

  action.execute
  actor.epic.rulebooks.flat_map { |rlbk| rlbk.run_after_actions action }
  action
end
proceed() click to toggle source

Execute the next available action.

Actors should run execute first.

@return [Action, nil]

# File lib/gamefic/dispatcher.rb, line 41
def proceed
  return unless @executed

  next_action&.execute
end

Protected Instance Methods

responses() click to toggle source

@return [Array<Response>]

# File lib/gamefic/dispatcher.rb, line 74
def responses
  @responses ||= actor.epic.responses_for(command.verb)
end

Private Instance Methods

next_action() click to toggle source

@return [Action, nil]

# File lib/gamefic/dispatcher.rb, line 81
def next_action
  while (response = responses.shift)
    next if response.queries.length < @command.arguments.length

    return Action.new(actor, @command.arguments, response) if response.accept?(actor, @command)
  end
end