Class: Termfront::Network::WavesfightClient

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/network/wavesfight_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(stdout) ⇒ WavesfightClient

Returns a new instance of WavesfightClient.



6
7
8
9
10
11
12
# File 'lib/termfront/network/wavesfight_client.rb', line 6

def initialize(stdout)
  @stdout = stdout
  @conn = Connection.new
  @input = Input.new
  @renderer = Renderer.new(stdout)
  @audio = AudioManager.new
end

Instance Method Details

#run(mission_id:, difficulty:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/termfront/network/wavesfight_client.rb', line 14

def run(mission_id:, difficulty:)
  @queue_mission_id = mission_id
  @queue_difficulty = difficulty
  addr = prompt_address
  return unless addr

  host, port = addr.include?(":") ? addr.split(":", 2).then { |h, p| [h, p.to_i] } : [addr, Config::PVP_PORT]
  begin
    @conn.connect(host, port)
    @conn.send_msg({ t: "queue", mode: "wavesfight", mission_id: mission_id, difficulty: difficulty })
  rescue StandardError => e
    show_error("Connection failed: #{e.message}")
    return
  end

  begin
    unless wait_for_start
      @conn.close
      return
    end
    run_game_loop
  rescue StandardError => e
    show_error("Error: #{e.message}")
  ensure
    @audio.close
    @conn.close
  end
end