Class: ATCTools::VRC

Inherits:
Object
  • Object
show all
Defined in:
lib/atc-tools/vrc.rb

Overview

A class for interfacing with the Virtual Radar Client application. www1.metacraft.com/VRC/

Methods with a bang (!) at the end manipulate the window and could affect the user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kvargs) ⇒ VRC

Params:

:aclog_path - Location of the VRC log dump for the aircraft info query.


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/atc-tools/vrc.rb', line 19

def initialize(**kvargs)
  @aclog_path = kvargs.fetch :aclog_path, File.expand_path('Documents/VRC/acinfo.txt', '~')
  
  @selected_aircraft = ''
  
  #Window handles.
  @vrc_win         = RAutomation::Window.new title: /VRC.*Connected to server:/
  @flight_plan_win = RAutomation::Window.new title: /Flight Plan - /
  @terminal_win    = RAutomation::Window.new title: /\\cmd.exe/
  
  @command_win     = @vrc_win.text_fields[0]
  @callsign_win    = @vrc_win.text_fields[1]
end

Instance Attribute Details

#aclog_pathObject

VRC log path for exporting aircraft info.



15
16
17
# File 'lib/atc-tools/vrc.rb', line 15

def aclog_path
  @aclog_path
end

Instance Method Details

#activate_terminal_window!Object

Activate the terminal (command line) window, bringing it to the foreground.



160
161
162
# File 'lib/atc-tools/vrc.rb', line 160

def activate_terminal_window!
  @terminal_win.activate
end

#activate_vrc_window!Object

Activate the VRC window, bringing it to the foreground.



154
155
156
# File 'lib/atc-tools/vrc.rb', line 154

def activate_vrc_window!
  @vrc_win.activate
end

#aircraft_selected?Boolean

True if an aircraft is selected.

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/atc-tools/vrc.rb', line 71

def aircraft_selected?
  selection_empty = selected_aircraft.empty?
  @selected_aircraft = '' if selection_empty
  not selection_empty
end

#command_lineObject

Extract the text from the command line.



134
135
136
# File 'lib/atc-tools/vrc.rb', line 134

def command_line
  @command_win.value
end

#execute_command(cmd) ⇒ Object

Execute a command in the VRC client. Preserves partially-entered commands in the text box.



140
141
142
143
144
# File 'lib/atc-tools/vrc.rb', line 140

def execute_command(cmd)
  old_cmd = command_line
  execute_command! cmd
  @command_win.set old_cmd
end

#execute_command!(cmd) ⇒ Object

Execute a command in the VRC client, overwriting any partially-entered command in the text box.



148
149
150
151
# File 'lib/atc-tools/vrc.rb', line 148

def execute_command!(cmd)
  @command_win.set cmd
  @command_win.send_keys :return
end

#flight_plan_titleObject

Title of the VRC flight plan window. This includes the pilot’s callsign and real name.



35
36
37
# File 'lib/atc-tools/vrc.rb', line 35

def flight_plan_title
  @flight_plan_win.title
end

#selected_aircraftObject

Extracts the callsign of the selected aircraft. Returns an empty string if no aircraft is selected.



66
67
68
# File 'lib/atc-tools/vrc.rb', line 66

def selected_aircraft
  @selected_aircraft = @callsign_win.value.strip
end

#selected_aircraft_info!Object

Extracts the aircraft info for the selected aircraft.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/atc-tools/vrc.rb', line 78

def selected_aircraft_info!
  # --------------------------------------------------------------------------
  # TODO: Prompt for an aircraft model as the input and use .typeinfo instead.
  #       This way it can return info for cached aircraft.
  # --------------------------------------------------------------------------
  
  raise ATCTools::NoAircraftSelectedError, "No aircraft selected." \
    unless aircraft_selected?
  
  # ---------------------------
  # TODO: 
  # ---------------------------
  
  # Retrieve the aircraft info.
  execute_command ".acinfo #{@selected_aircraft}"

  # Dump the aircraft info to a log for processing.
  execute_command ".log #{File.basename @aclog_path}"
  
  # Process aircraft info.
  aclog = ''
  result = ''
  attempts = 0
  
  while attempts < 5
    aclog_exists = File.exists? @aclog_path
    break if aclog_exists
    
    attempts += 1
    sleep 0.5
  end
  
  if aclog_exists
    aclog = File.open(@aclog_path).read
    
    # Only keep the last few lines.
    # Reverse the lines so the latest one is first.
    aclog = aclog.lines[-6..-1].reverse.join
    
    aclog.each_line do |line|
      result = line.gsub /.*\s*(Aircraft info for \w*:\s*)/, '' if line.include? "Aircraft info for #{@selected_aircraft}"
      break if result
    end
    
    # ---------------
    # TODO: Implement
    # ---------------
    result = "Aircraft type code not found in database." if result.empty?
    
    # File.delete @aclog_path
  end
  
  result
end

#selected_flight_plan!Object

Extracts the flight plan of the selected aircraft.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/atc-tools/vrc.rb', line 40

def selected_flight_plan!
  raise ATCTools::NoAircraftSelectedError, "No aircraft selected." \
    unless aircraft_selected?
  
  execute_command ".ss #{@selected_aircraft}"
  
  flight_plan = ATCTools::FlightPlan.new \
    callsign:   @selected_aircraft,
    aircraft:   ATCTools::Aircraft.new(
                  @flight_plan_win.text_fields[1].value,
                  info: selected_aircraft_info!
                ),
    rules:      (RAutomation::Adapter::Win32::SelectList.new @flight_plan_win, index: 0)
                  .value.upcase.to_sym,
    depart:     ATCTools::Airport.new(@flight_plan_win.text_fields[2].value),
    arrive:     ATCTools::Airport.new(@flight_plan_win.text_fields[3].value),
    alternate:  ATCTools::Airport.new(@flight_plan_win.text_fields[4].value),
    cruise:     @flight_plan_win.text_fields[5].value.to_i,
    scratchpad: @flight_plan_win.text_fields[6].value.strip,
    squawk:     @flight_plan_win.text_fields[7].value.strip,
    route:      @flight_plan_win.text_fields[8].value.strip,
    remarks:    @flight_plan_win.text_fields[9].value.strip
end