class MudServer::AbstractController

Transfers input/output from the Telnet world into the Ruby world while abstracting away all the rough spots. All controllers should inherit from here

Attributes

params[RW]
session[RW]

Public Class Methods

new(session) click to toggle source

Instantiates a new controller instance. It is highly recomended that derived controller classes either not modify this method, call super, or proxy via a helper method (such as create() or buil(), etc).

  • session - (Session) The connection session that the controller will

communicate with.

# File lib/abstract_controller.rb, line 14
def initialize(session)
  @session = session
  on_start
  true
end

Public Instance Methods

allowed_methods() click to toggle source

Dynamically generated list of user accesible controller methods. By default (and when used in conjugation with super in derived classes), will return

‘quit’

as its only accessible method. Returns Array of Strings.

# File lib/abstract_controller.rb, line 93
def allowed_methods
  ['quit']
end
funny_responses() click to toggle source

Parameterless method used for generating quirky messages when the user attempts to use a non-existant / forbidden controller action. Returns String.

# File lib/abstract_controller.rb, line 81
def funny_responses
  [
    'Huh?',
    "What's that you say?",
    'come again?',
    "Sorry, I don't know that command."
  ]
end
get_text(command) click to toggle source

Parses arbitrary user input into a format usable by the interpreter. Strips all input after initial command and stores it in ‘params`.

  • command - (String) A string of space seperated commands and arguments

    parse_command('login user_x password123')
    # => True
    params() # Note: side effects
    # => 'user_x password123'
    
# File lib/abstract_controller.rb, line 45
def get_text(command)
  command = command.split(' ')
  head    = command.shift
  @params = command.join(' ')
  interpret_command head.to_s.downcase
end
interpret_command(head) click to toggle source

Interprets a controller command (first argument of user input)

  • head - (String) The command to execute. Will only execute the command if

it is within the ‘allowed_methods’ dynamic array.

interpret_command('add') # Assumes the current controller has
                         # an add() method defined and within
# => 10                  # the allowed_methods Array.
# File lib/abstract_controller.rb, line 60
def interpret_command(head)
  if allowed_methods.include? head
    self.send(head)
  else
    send_error
  end
rescue => error
    send_text 'You just broke something. Please tell the admins about this.'
    send_text error.message
end
on_start() click to toggle source

User definable callback called after instantiation.

# File lib/abstract_controller.rb, line 21
def on_start
end
quit() click to toggle source

Void paramaterless controller action that closes TCP socket to client. You must whitelist this command in allowed_methods() in order for it to be usable by players.

# File lib/abstract_controller.rb, line 32
def quit
  session.connection.close
end
send_error() click to toggle source

A whimsical way of telling the user they input an unknown / unauthorized command. Override this if you want a ‘404 page’ on your controller

# File lib/abstract_controller.rb, line 74
def send_error
  send_text funny_responses.sample
end
send_text(command) click to toggle source

Sends a string to remote client over the TCP socket connection.

# File lib/abstract_controller.rb, line 25
def send_text(command)
  @session.connection.puts(command)
end
transfer_to(controller_name) click to toggle source

Transfers control from on controller to another. Eg: Move from login controler to main game.

# File lib/abstract_controller.rb, line 99
def transfer_to(controller_name)
  @session.controller = controller_name.new(@session)
end