Class: Process::Roulette::Controller::GameHandler
- Inherits:
-
Object
- Object
- Process::Roulette::Controller::GameHandler
- Defined in:
- lib/process/roulette/controller/game_handler.rb
Overview
Handles the GAME state of the controller state machine.
Instance Method Summary collapse
- #_display_scoreboard(scoreboard) ⇒ Object
- #_finish_bout ⇒ Object
- #_handle_unexpected(packet) ⇒ Object
- #_process_input ⇒ Object
- #_report_update(message) ⇒ Object
- #_scoreboard_header ⇒ Object
- #_start_next_round ⇒ Object
- #_wait_for_input ⇒ Object
-
#initialize(driver) ⇒ GameHandler
constructor
A new instance of GameHandler.
- #run ⇒ Object
Constructor Details
#initialize(driver) ⇒ GameHandler
Returns a new instance of GameHandler.
9 10 11 |
# File 'lib/process/roulette/controller/game_handler.rb', line 9 def initialize(driver) @driver = driver end |
Instance Method Details
#_display_scoreboard(scoreboard) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/process/roulette/controller/game_handler.rb', line 67 def _display_scoreboard(scoreboard) _scoreboard_header scoreboard.each.with_index do |player, index| puts format('%2d | %-10s | %-10s | %5d', index + 1, player[:name], player[:killer], player[:victims].length) end end |
#_finish_bout ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/process/roulette/controller/game_handler.rb', line 49 def _finish_bout puts 'BOUT FINISHED' @bout_active = false scoreboard = @driver.socket.read_packet _display_scoreboard(scoreboard) puts end |
#_handle_unexpected(packet) ⇒ Object
63 64 65 |
# File 'lib/process/roulette/controller/game_handler.rb', line 63 def _handle_unexpected(packet) puts "- unexpected message from croupier (#{packet.inspect})" end |
#_process_input ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/process/roulette/controller/game_handler.rb', line 32 def _process_input packet = @driver.socket.read_packet case packet when nil then abort 'disconnected!' when 'GO' then _start_next_round when 'DONE' then _finish_bout when /^UPDATE:(.*)/ then _report_update(Regexp.last_match(1)) else _handle_unexpected(packet) end end |
#_report_update(message) ⇒ Object
59 60 61 |
# File 'lib/process/roulette/controller/game_handler.rb', line 59 def _report_update() puts "- #{message}" end |
#_scoreboard_header ⇒ Object
76 77 78 79 |
# File 'lib/process/roulette/controller/game_handler.rb', line 76 def _scoreboard_header puts format(' | %-10s | %-10s | %-6s', 'Name', 'Killer', 'Rounds') puts format('---+-%10s-+-%10s-+-%6s', '-' * 10, '-' * 10, '-' * 6) end |
#_start_next_round ⇒ Object
44 45 46 47 |
# File 'lib/process/roulette/controller/game_handler.rb', line 44 def _start_next_round @round += 1 puts "- ROUND #{@round}" end |
#_wait_for_input ⇒ Object
27 28 29 30 |
# File 'lib/process/roulette/controller/game_handler.rb', line 27 def _wait_for_input ready, = IO.select([@driver.socket], [], [], 0.2) ready ? ready.first : nil end |
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/process/roulette/controller/game_handler.rb', line 13 def run puts 'BOUT BEGINS' @bout_active = true @round = 1 while @bout_active _process_input if _wait_for_input @driver.socket.send_packet('PING') end Controller::CommandHandler end |