Class: MudServer::AbstractController
- Inherits:
-
Object
- Object
- MudServer::AbstractController
- Defined in:
- lib/abstract_controller.rb
Overview
Transfers input/output from the Telnet world into the Ruby world while abstracting away all the rough spots. All controllers should inherit from here
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#allowed_methods ⇒ Object
Dynamically generated list of user accesible controller methods.
-
#funny_responses ⇒ Object
Parameterless method used for generating quirky messages when the user attempts to use a non-existant / forbidden controller action.
-
#get_text(command) ⇒ Object
Parses arbitrary user input into a format usable by the interpreter.
-
#initialize(session) ⇒ AbstractController
constructor
Instantiates a new controller instance.
-
#interpret_command(head) ⇒ Object
Interprets a controller command (first argument of user input).
-
#on_start ⇒ Object
User definable callback called after instantiation.
-
#quit ⇒ Object
Void paramaterless controller action that closes TCP socket to client.
-
#send_error ⇒ Object
A whimsical way of telling the user they input an unknown / unauthorized command.
-
#send_text(command) ⇒ Object
Sends a string to remote client over the TCP socket connection.
-
#transfer_to(controller_name) ⇒ Object
Transfers control from on controller to another.
Constructor Details
#initialize(session) ⇒ AbstractController
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.
14 15 16 17 18 |
# File 'lib/abstract_controller.rb', line 14 def initialize(session) @session = session on_start true end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/abstract_controller.rb', line 6 def params @params end |
#session ⇒ Object
Returns the value of attribute session.
6 7 8 |
# File 'lib/abstract_controller.rb', line 6 def session @session end |
Instance Method Details
#allowed_methods ⇒ Object
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.
93 94 95 |
# File 'lib/abstract_controller.rb', line 93 def allowed_methods ['quit'] end |
#funny_responses ⇒ Object
Parameterless method used for generating quirky messages when the user attempts to use a non-existant / forbidden controller action. Returns String.
81 82 83 84 85 86 87 88 |
# 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) ⇒ Object
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 argumentsparse_command('login user_x password123') # => True params() # Note: side effects # => 'user_x password123'
45 46 47 48 49 50 |
# 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) ⇒ Object
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.
60 61 62 63 64 65 66 67 68 69 |
# 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. end |
#on_start ⇒ Object
User definable callback called after instantiation.
21 22 |
# File 'lib/abstract_controller.rb', line 21 def on_start end |
#quit ⇒ Object
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.
32 33 34 |
# File 'lib/abstract_controller.rb', line 32 def quit session.connection.close end |
#send_error ⇒ Object
A whimsical way of telling the user they input an unknown / unauthorized command. Override this if you want a ‘404 page’ on your controller
74 75 76 |
# File 'lib/abstract_controller.rb', line 74 def send_error send_text funny_responses.sample end |
#send_text(command) ⇒ Object
Sends a string to remote client over the TCP socket connection.
25 26 27 |
# File 'lib/abstract_controller.rb', line 25 def send_text(command) @session.connection.puts(command) end |
#transfer_to(controller_name) ⇒ Object
Transfers control from on controller to another. Eg: Move from login controler to main game.
99 100 101 |
# File 'lib/abstract_controller.rb', line 99 def transfer_to(controller_name) @session.controller = controller_name.new(@session) end |