Module: Alchemist::ServerHandler::Methods
- Includes:
- EventMachine::Protocols::LineProtocol
- Defined in:
- lib/alchemist-server/server_handler.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #post_init ⇒ Object
- #process_line(line) ⇒ Object
- #receive_line(line) ⇒ Object
- #run_command_nearby(command) ⇒ Object
- #run_unrecorded_command(command) ⇒ Object
- #send_line(data) ⇒ Object
- #show_error(e) ⇒ Object
- #unbind ⇒ Object
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
46 47 48 |
# File 'lib/alchemist-server/server_handler.rb', line 46 def name @name end |
Instance Method Details
#post_init ⇒ Object
48 49 50 51 |
# File 'lib/alchemist-server/server_handler.rb', line 48 def post_init connections << self send_line "Welcome alchemical friend. What is your name?" end |
#process_line(line) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/alchemist-server/server_handler.rb', line 69 def process_line(line) if name command = "#{name} #{line.chomp}" outcome = Alchemist::Server.run_append command, world_file, history self.history = outcome.new_history if outcome.new_history if command = outcome.nearby_avatar_command run_command_nearby command end outcome.response else possible_name = line.strip.split(' ').first if possible_name && possible_name.length > 0 @name = possible_name "hello #{name}" else "Please tell me your name." end end rescue => e show_error e "error #{e}" end |
#receive_line(line) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/alchemist-server/server_handler.rb', line 57 def receive_line(line) t = Benchmark.realtime do response = process_line line if response send_line response end end puts "#{Time.now} #{line.split(' ').first} #{(t*1000).round}" end |
#run_command_nearby(command) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/alchemist-server/server_handler.rb', line 98 def run_command_nearby(command) new_nearby = history.world.nearby_avatar_names name old_nearby = begin history.prior_world.try :nearby_avatar_names, name rescue # bail out if user wasn't in prior world [] end nearby = new_nearby | (old_nearby || []) connections.select do |c| begin if nearby.include? c.name c.run_unrecorded_command command end rescue => e show_error e end end end |
#run_unrecorded_command(command) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/alchemist-server/server_handler.rb', line 125 def run_unrecorded_command(command) outcome = command.run name, history if r = outcome.response send_line r end end |
#send_line(data) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/alchemist-server/server_handler.rb', line 133 def send_line(data) send_data data if data[-1] != "\n" send_data "\n" end end |
#show_error(e) ⇒ Object
120 121 122 123 |
# File 'lib/alchemist-server/server_handler.rb', line 120 def show_error(e) $stderr.puts "#{e.class}: #{e.}" $stderr.puts e.backtrace end |
#unbind ⇒ Object
53 54 55 |
# File 'lib/alchemist-server/server_handler.rb', line 53 def unbind connections.delete self end |