Class: Process::Roulette::Controller::CommandHandler
- Inherits:
-
Object
- Object
- Process::Roulette::Controller::CommandHandler
- Defined in:
- lib/process/roulette/controller/command_handler.rb
Overview
Handles the COMMAND state of the controller state machine. Listens to both STDIN (unless we’re a spectator) and the socket and acts accordingly.
Instance Method Summary collapse
- #_handle_exit ⇒ Object
- #_handle_go ⇒ Object
- #_handle_unexpected(packet) ⇒ Object
- #_handle_update(msg) ⇒ Object
- #_invoke_error(text) ⇒ Object
- #_invoke_exit ⇒ Object
- #_invoke_go ⇒ Object
- #_invoke_help ⇒ Object
- #_process_croupier_input ⇒ Object
- #_process_ready_socket(io) ⇒ Object
- #_process_user_input ⇒ Object
- #_say(message, update_prompt = true) ⇒ Object
- #_wait_for_input ⇒ Object
-
#initialize(driver) ⇒ CommandHandler
constructor
A new instance of CommandHandler.
- #run ⇒ Object
Constructor Details
#initialize(driver) ⇒ CommandHandler
Returns a new instance of CommandHandler.
12 13 14 15 |
# File 'lib/process/roulette/controller/command_handler.rb', line 12 def initialize(driver) @driver = driver @next_handler = nil end |
Instance Method Details
#_handle_exit ⇒ Object
102 103 104 105 |
# File 'lib/process/roulette/controller/command_handler.rb', line 102 def _handle_exit _say 'croupier is terminating. bye!', false @next_handler = Controller::FinishHandler end |
#_handle_go ⇒ Object
107 108 109 110 |
# File 'lib/process/roulette/controller/command_handler.rb', line 107 def _handle_go _say nil, false @next_handler = Controller::GameHandler end |
#_handle_unexpected(packet) ⇒ Object
116 117 118 |
# File 'lib/process/roulette/controller/command_handler.rb', line 116 def _handle_unexpected(packet) _say "unexpected message from croupier: #{packet.inspect}" end |
#_handle_update(msg) ⇒ Object
112 113 114 |
# File 'lib/process/roulette/controller/command_handler.rb', line 112 def _handle_update(msg) _say msg end |
#_invoke_error(text) ⇒ Object
97 98 99 100 |
# File 'lib/process/roulette/controller/command_handler.rb', line 97 def _invoke_error(text) _say "#{text.inspect} is not understood", false _invoke_help end |
#_invoke_exit ⇒ Object
67 68 69 70 |
# File 'lib/process/roulette/controller/command_handler.rb', line 67 def _invoke_exit _say 'telling croupier to terminate' @driver.socket.send_packet('EXIT') end |
#_invoke_go ⇒ Object
72 73 74 75 |
# File 'lib/process/roulette/controller/command_handler.rb', line 72 def _invoke_go _say 'telling croupier to start game' @driver.socket.send_packet('GO') end |
#_invoke_help ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/process/roulette/controller/command_handler.rb', line 77 def _invoke_help puts 'Ok. I understand these commands:' puts ' - EXIT (terminates the croupier)' puts ' - QUIT (terminates just this controller)' puts ' - GO (starts the bout)' puts ' - HELP (this page)' _say nil end |
#_process_croupier_input ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/process/roulette/controller/command_handler.rb', line 86 def _process_croupier_input packet = @driver.socket.read_packet case packet when nil, 'EXIT' then _handle_exit when 'GO' then _handle_go when /^UPDATE:(.*)/ then _handle_update(Regexp.last_match(1)) else _handle_unexpected(packet) end end |
#_process_ready_socket(io) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/process/roulette/controller/command_handler.rb', line 47 def _process_ready_socket(io) if io == STDIN _process_user_input elsif io == @driver.socket _process_croupier_input end end |
#_process_user_input ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/process/roulette/controller/command_handler.rb', line 55 def _process_user_input command = (STDIN.gets || '').strip.upcase case command when '', 'QUIT' then @next_handler = :quit when 'EXIT' then _invoke_exit when 'GO' then _invoke_go when 'HELP', '?' then _invoke_help else _invoke_error(command) end end |
#_say(message, update_prompt = true) ⇒ Object
33 34 35 36 37 |
# File 'lib/process/roulette/controller/command_handler.rb', line 33 def _say(, update_prompt = true) puts unless @driver.spectator? puts if print 'controller> ' if update_prompt && !@driver.spectator? end |
#_wait_for_input ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/process/roulette/controller/command_handler.rb', line 39 def _wait_for_input ios = [ @driver.socket ] ios << STDIN unless @driver.spectator? ready, = IO.select(ios, [], [], 0.2) ready || [] end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/process/roulette/controller/command_handler.rb', line 17 def run STDOUT.sync = true _say 'waiting for bout to begin' while @next_handler.nil? ready = _wait_for_input @driver.socket.send_packet('PING') ready.each do |io| _process_ready_socket(io) end end @next_handler == :quit ? nil : @next_handler end |