class Process::Roulette::Controller::CommandHandler

Handles the COMMAND state of the controller state machine. Listens to both STDIN (unless we're a spectator) and the socket and acts accordingly.

Public Class Methods

new(driver) click to toggle source
# File lib/process/roulette/controller/command_handler.rb, line 12
def initialize(driver)
  @driver = driver
  @next_handler = nil
end

Public Instance Methods

_handle_exit() click to toggle source
# 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() click to toggle source
# File lib/process/roulette/controller/command_handler.rb, line 107
def _handle_go
  _say nil, false
  @next_handler = Controller::GameHandler
end
_handle_unexpected(packet) click to toggle source
# 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) click to toggle source
# File lib/process/roulette/controller/command_handler.rb, line 112
def _handle_update(msg)
  _say msg
end
_invoke_error(text) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/process/roulette/controller/command_handler.rb, line 33
def _say(message, update_prompt = true)
  puts unless @driver.spectator?
  puts message if message
  print 'controller> ' if update_prompt && !@driver.spectator?
end
_wait_for_input() click to toggle source
# 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() click to toggle source
# 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