Class: QStat::ServerInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-qstat.rb

Defined Under Namespace

Classes: Rule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ ServerInfo

Returns a new instance of ServerInfo.



116
117
118
119
120
# File 'lib/ruby-qstat.rb', line 116

def initialize(data=nil)
  if data
    parse_qstat_query_player(data)
  end
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



60
61
62
# File 'lib/ruby-qstat.rb', line 60

def addr
  @addr
end

#game_typeObject

Returns the value of attribute game_type.



67
68
69
# File 'lib/ruby-qstat.rb', line 67

def game_type
  @game_type
end

#mapObject

Returns the value of attribute map.



62
63
64
# File 'lib/ruby-qstat.rb', line 62

def map
  @map
end

#num_playersObject

Returns the value of attribute num_players.



61
62
63
# File 'lib/ruby-qstat.rb', line 61

def num_players
  @num_players
end

#number_of_max_playersObject

Returns the value of attribute number_of_max_players.



71
72
73
# File 'lib/ruby-qstat.rb', line 71

def number_of_max_players
  @number_of_max_players
end

#number_of_playersObject

Returns the value of attribute number_of_players.



72
73
74
# File 'lib/ruby-qstat.rb', line 72

def number_of_players
  @number_of_players
end

#pingObject

Returns the value of attribute ping.



69
70
71
# File 'lib/ruby-qstat.rb', line 69

def ping
  @ping
end

#playersObject

Returns the value of attribute players.



66
67
68
# File 'lib/ruby-qstat.rb', line 66

def players
  @players
end

#responseObject

Returns the value of attribute response.



63
64
65
# File 'lib/ruby-qstat.rb', line 63

def response
  @response
end

#retriesObject

Returns the value of attribute retries.



70
71
72
# File 'lib/ruby-qstat.rb', line 70

def retries
  @retries
end

#rulesObject

Returns the value of attribute rules.



75
76
77
# File 'lib/ruby-qstat.rb', line 75

def rules
  @rules
end

#server_nameObject

Returns the value of attribute server_name.



65
66
67
# File 'lib/ruby-qstat.rb', line 65

def server_name
  @server_name
end

#statusObject

Returns the value of attribute status.



73
74
75
# File 'lib/ruby-qstat.rb', line 73

def status
  @status
end

#timeObject

Returns the value of attribute time.



64
65
66
# File 'lib/ruby-qstat.rb', line 64

def time
  @time
end

Class Method Details

.create_from_xml(doc) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby-qstat.rb', line 122

def self.create_from_xml(doc)
  info = self.new
  info.addr = doc.search("/server/hostname").text
  info.server_name = doc.search("/server/name").text
  info.game_type = doc.search("/server/gametype").text
  info.map = doc.search("/server/map").text
  info.num_players = doc.search("/server/numplayers").text
  info.number_of_max_players = doc.search("/server/maxplayers").text
  info.ping = doc.search("/server/ping").text
  info.retries = doc.search("/server/retries").text
  info.rules = []
  doc.search("/server/rules").each{ |rule|
    info.rules << Rule.create_from_xml(rule)
  }
  info.players = []
  doc.search("/server/players/player").each{ |player|
    info.players << PlayerInfo.create_from_xml(player)
  }
  info.number_of_players = [info.players.size, info.number_of_max_players].join(" / ")
  info.status = doc.search("/server").first.attributes["status"].value
  info
end

Instance Method Details

#down?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/ruby-qstat.rb', line 208

def down?
  @header =~ /^(\d+(?:.\d+){3}:\d+)\s+DOWN$/
end

#empty_server?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/ruby-qstat.rb', line 232

def empty_server?
  @players.empty?
end

#longest_playing_playerObject



226
227
228
229
230
# File 'lib/ruby-qstat.rb', line 226

def longest_playing_player
  @players.max_by{ |player|
    player.time_to_i
  }
end

#no_response?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/ruby-qstat.rb', line 204

def no_response?
  @header =~ /^(\d+(?:.\d+){3}:\d+)\s+no response$/
end

#number_of_active_playersObject



200
201
202
# File 'lib/ruby-qstat.rb', line 200

def number_of_active_players
  @players.size
end

#parse_qstat_query_player(data) ⇒ Object



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
189
190
191
192
193
194
195
196
197
198
# File 'lib/ruby-qstat.rb', line 145

def parse_qstat_query_player(data)
  lines = data.split(/\n/)
  @header = lines[0]
  @body = lines[1..-1]
  @players = []
  @rules = []

  if no_response? or down?
    return nil
  end

  if @header =~ /^(\d+(?:.\d+){3}:\d+)\s+(\d+\/\d+)\s+(\d+\/\d+)\s+(.*?)\s+([\d\s]+\/\s+\d+)\s+\S+\s+(.*)$/
    @addr = $1
    @num_players = $2
    @map = $4
    @response = $5
    @server_name = $6

    if @body
      if @body.first =~ /^\t\s*\d+\s*frags.*$/
        @body.each{ |player_info_line|
          @players << PlayerInfo.new(player_info_line)
        }
      else
        server_info = @body.join("\n")
        server_info.force_convert_to('UTF-8')
        server_info.sub!(/^\s+/, "")
        server_info.gsub!(/\u0001/, "")
        hash = {}
        ary = server_info.split(",").map{|e| e.split("=")}
        ary.each{ |e|
          hash[e.first] = e.last
        }
        rule = Rule.new
        rule.protocol = hash["protocol"]
        rule.gamedir = hash["gamedir"]
        rule.gamename = hash["gamename"]
        rule.bots = hash["bots"]
        rule.dedicated = hash["dedicated"]
        rule.sv_os = hash["sv_os"]
        rule.secure = hash["secure"]
        rule.version = hash["version"]
        rule.game_port = hash["game_port"]
        rule.game_tag = hash["game_tag"]
        p rule.game_tags
        @rules << rule
      end
    end
  end

  @ping = parse_map_info.first
  @number_of_max_players = parse_number_of_players.last
  @number_of_players = [number_of_active_players, @number_of_max_players].join(" / ")
end

#playing_timeObject



212
213
214
215
216
217
# File 'lib/ruby-qstat.rb', line 212

def playing_time
  if empty_server?
    return "00:00"
  end
  longest_playing_player.time_to_s
end

#playing_time_secondsObject



219
220
221
222
223
224
# File 'lib/ruby-qstat.rb', line 219

def playing_time_seconds
  if empty_server?
    return 0
  end
  longest_playing_player.time_to_i
end

#suggest_game_typeObject



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ruby-qstat.rb', line 236

def suggest_game_type
  unless @game_type.nil?
    return @game_type
  end

  if (not @rules.empty?) and (not @rules.first.game_tags.empty?)
    return @rules.first.game_tags.first
  end

  return 'unknown'
end