Class: TFClient::Models::Status

Inherits:
Response
  • Object
show all
Defined in:
lib/textflight-client/models/status.rb

Instance Attribute Summary collapse

Attributes inherited from Response

#lines, #response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines:) ⇒ Status

Returns a new instance of Status.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/textflight-client/models/status.rb', line 64

def initialize(lines:)
  super(lines: lines)

  @states = {}

  @status_report = Models::StatusReport.new(lines: lines)
  @mass = @status_report.hash[:mass].to_i
  @total_outfit_space = @status_report.hash[:total_outfit_space].to_i

  outfit_space_line = lines.detect do |line|
    line.strip.start_with?("Outfit space")
  end

  hash = ResponseParser.hash_from_line_values(line: outfit_space_line)
  @used_outfit_space = @total_outfit_space - hash[:space].to_i

  # Cooling
  @states[:cooling], @cooling_status = Status.status_from_lines(
    lines: lines,
    start_with: "Cooling status")
  @heat = @status_report.hash[:heat].to_i
  @max_heat = @status_report.hash[:max_heat].to_i
  @heat_rate = @status_report.hash[:heat_rate].to_f

  # Energy / Power
  @states[:power], @power_status = Status.status_from_lines(
    lines: lines,
    start_with: "Power status"
  )

  @energy = @status_report.hash[:energy].to_i
  @max_energy = @status_report.hash[:max_energy].to_i
  @energy_rate = @status_report.hash[:energy_rate].to_f

  # Antigravity
  antigravity_line = lines.detect do |line|
    line.strip.start_with?("Antigravity engines")
  end

  if antigravity_line
    @states[:antigravity], @antigravity_engine_status =
      Status.status_from_lines(lines: lines, start_with: "Antigravity engines")
    @antigravity = @status_report.hash[:antigravity].to_i
  else
    # Needs translation
    @states[:antigravity] = "Offline"
    @antigravity = "Antigravity engines: Offline"
  end



  # Mining
  mining_progress_line = lines.detect do |line|
    line.strip.start_with?("Mining progress")
  end

  if mining_progress_line
    @states[:mining], @mining_status =
      Status.status_from_lines(lines: lines,
                               start_with: "Mining progress")
    hash = ResponseParser.hash_from_line_values(line: mining_progress_line)
    @mining_interval = hash[:interval].to_f
    @mining_power = @status_report.hash[:mining_power].to_f
  else
    @mining_interval = nil
    @mining_power = nil
    # TODO needs a translation
    @states[:mining] = "Offline"
    @mining_status = "Offline"
  end

  # Warp
  warp_line = lines.detect do |line|
    line.strip.start_with?("Warp engines")
  end
  if warp_line
    @states[:warp], @engine_status =
      Status.status_from_lines(lines: lines,
                               start_with: "Warp engines")
    @warp_charge = @status_report.hash[:warp_charge].to_f
  else
    @states[:warp] = "Offline"
    @warp_charge = 0.0
  end

  # Shield
  shield_status_line = lines.detect do |line|
    line.strip.start_with?("Shields")
  end

  if shield_status_line
    @states[:shields], @shield_status =
      Status.status_from_lines(lines: lines,
                               start_with: "Shields")
    @shield_charge_rate = @status_report.hash[:shield_rate].to_f
    @shield_max = @status_report.hash[:max_shield].to_f
    @shield = @status_report.hash[:shield].to_f
  else
    # TODO Need translation
    @shield_status = "Offline"
    @states[:shields] = "Offline"
    @shield_charge_rate = nil
    @shield_max = @status_report.hash[:max_shield].to_f
    @shield = 0
  end

  # Colonists
  colonists_line = lines.detect do |line|
    line.strip.start_with?("Colonists")
  end

  if colonists_line
    # TODO Need translation
    @states[:colonists] = "Crewed"
    @colonists_status =
      ResponseParser.substitute_line_values(line: colonists_line)
    hash = ResponseParser.hash_from_line_values(line: colonists_line)
    @colonists = hash[:crew].to_i
  else
    # TODO Need translation
    @states[:colonists] = "Unmanned"
    @colonists_status = "Unmanned"
    @colonists = 0
  end
end

Instance Attribute Details

#antigravityObject (readonly)

Returns the value of attribute antigravity.



58
59
60
# File 'lib/textflight-client/models/status.rb', line 58

def antigravity
  @antigravity
end

#antigravity_engine_statusObject (readonly)

Returns the value of attribute antigravity_engine_status.



58
59
60
# File 'lib/textflight-client/models/status.rb', line 58

def antigravity_engine_status
  @antigravity_engine_status
end

#colonistsObject (readonly)

Returns the value of attribute colonists.



62
63
64
# File 'lib/textflight-client/models/status.rb', line 62

def colonists
  @colonists
end

#colonists_statusObject (readonly)

Returns the value of attribute colonists_status.



62
63
64
# File 'lib/textflight-client/models/status.rb', line 62

def colonists_status
  @colonists_status
end

#cooling_statusObject (readonly)

Returns the value of attribute cooling_status.



56
57
58
# File 'lib/textflight-client/models/status.rb', line 56

def cooling_status
  @cooling_status
end

#energyObject (readonly)

Returns the value of attribute energy.



57
58
59
# File 'lib/textflight-client/models/status.rb', line 57

def energy
  @energy
end

#energy_rateObject (readonly)

Returns the value of attribute energy_rate.



57
58
59
# File 'lib/textflight-client/models/status.rb', line 57

def energy_rate
  @energy_rate
end

#engine_statusObject (readonly)

Returns the value of attribute engine_status.



60
61
62
# File 'lib/textflight-client/models/status.rb', line 60

def engine_status
  @engine_status
end

#heatObject (readonly)

Returns the value of attribute heat.



56
57
58
# File 'lib/textflight-client/models/status.rb', line 56

def heat
  @heat
end

#heat_rateObject (readonly)

Returns the value of attribute heat_rate.



56
57
58
# File 'lib/textflight-client/models/status.rb', line 56

def heat_rate
  @heat_rate
end

#massObject (readonly)

Returns the value of attribute mass.



55
56
57
# File 'lib/textflight-client/models/status.rb', line 55

def mass
  @mass
end

#max_energyObject (readonly)

Returns the value of attribute max_energy.



57
58
59
# File 'lib/textflight-client/models/status.rb', line 57

def max_energy
  @max_energy
end

#max_heatObject (readonly)

Returns the value of attribute max_heat.



56
57
58
# File 'lib/textflight-client/models/status.rb', line 56

def max_heat
  @max_heat
end

#mining_intervalObject (readonly)

Returns the value of attribute mining_interval.



59
60
61
# File 'lib/textflight-client/models/status.rb', line 59

def mining_interval
  @mining_interval
end

#mining_powerObject (readonly)

Returns the value of attribute mining_power.



59
60
61
# File 'lib/textflight-client/models/status.rb', line 59

def mining_power
  @mining_power
end

#mining_statusObject (readonly)

Returns the value of attribute mining_status.



59
60
61
# File 'lib/textflight-client/models/status.rb', line 59

def mining_status
  @mining_status
end

#power_statusObject (readonly)

Returns the value of attribute power_status.



57
58
59
# File 'lib/textflight-client/models/status.rb', line 57

def power_status
  @power_status
end

#shieldObject (readonly)

Returns the value of attribute shield.



61
62
63
# File 'lib/textflight-client/models/status.rb', line 61

def shield
  @shield
end

#shield_charge_rateObject (readonly)

Returns the value of attribute shield_charge_rate.



61
62
63
# File 'lib/textflight-client/models/status.rb', line 61

def shield_charge_rate
  @shield_charge_rate
end

#shield_maxObject (readonly)

Returns the value of attribute shield_max.



61
62
63
# File 'lib/textflight-client/models/status.rb', line 61

def shield_max
  @shield_max
end

#shield_statusObject (readonly)

Returns the value of attribute shield_status.



61
62
63
# File 'lib/textflight-client/models/status.rb', line 61

def shield_status
  @shield_status
end

#statesObject (readonly)

Returns the value of attribute states.



53
54
55
# File 'lib/textflight-client/models/status.rb', line 53

def states
  @states
end

#status_reportObject (readonly)

Returns the value of attribute status_report.



54
55
56
# File 'lib/textflight-client/models/status.rb', line 54

def status_report
  @status_report
end

#total_outfit_spaceObject (readonly)

Returns the value of attribute total_outfit_space.



55
56
57
# File 'lib/textflight-client/models/status.rb', line 55

def total_outfit_space
  @total_outfit_space
end

#used_outfit_spaceObject (readonly)

Returns the value of attribute used_outfit_space.



55
56
57
# File 'lib/textflight-client/models/status.rb', line 55

def used_outfit_space
  @used_outfit_space
end

#warp_chargeObject (readonly)

Returns the value of attribute warp_charge.



60
61
62
# File 'lib/textflight-client/models/status.rb', line 60

def warp_charge
  @warp_charge
end

Class Method Details

.status_from_lines(lines:, start_with:) ⇒ Object

returns 2 values



8
9
10
11
12
13
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
42
43
44
45
46
47
48
49
50
51
# File 'lib/textflight-client/models/status.rb', line 8

def self.status_from_lines(lines:, start_with:)
  stripped = lines.map { |line| line.strip }
  prefix = start_with.strip
  line, _ = ResponseParser.line_and_index_for_beginning_with(lines: stripped,
                                                             string: prefix)
  if !lines || !line.start_with?(prefix)
    raise "expected line to be a status line for #{prefix} in #{lines}"
  end

  tokens = ResponseParser.tokenize_line(line: line)

  status = tokens[0].split(": ").last

  case status
  when "Overheat in {remaining} seconds!"
    status = "Overheating"
  when "OVERHEATED"
    status = "Overheated"
  when "Ready to engage"
    status = "Ready"
  when "Charging ({charge}%)"
    status = "Charging"
  when "FAILED"
    status = "Failed"
  when "BROWNOUT"
    status = "Brownout"
  when "OVERLOADED"
    status = "Overloaded"
  when "Brownout in {remaining} seconds!"
    status = "Overloaded"
  when "Regenerating at {rate}/s ({shield}/{max})"
    status = "Regenerating"
  when "{progress}% ({interval} second interval)"
    status = "Online"
  end

  translation = tokens[1]

  return status, translation if tokens.size == 2

  translation = ResponseParser.substitute_line_values(line: line)

  return status, translation.strip
end