Class: Vrowser

Inherits:
Object
  • Object
show all
Includes:
VrowserModel
Defined in:
lib/vrowser.rb,
lib/plugins/l4d2.rb

Defined Under Namespace

Classes: Daemon, HTTPDaemon

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from VrowserModel

connect, define_models

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Vrowser

instance methods

Yields:

  • (_self)

Yield Parameters:

  • _self (Vrowser)

    the object that the method was called on



287
288
289
290
291
292
293
294
# File 'lib/vrowser.rb', line 287

def initialize(options={})
  @master_server = options[:master_server] or raise ArgumentError("master_server")
  @gametype = options[:gametype] or raise ArgumentError("gametype")
  @gamename = options[:gamename] or raise ArgumentError("gamename")
  @protocol = options[:protocol] or raise ArgumentError("protocol")
  @maxping  = options[:maxping] ||= 130
  yield(self) if block_given?
end

Class Method Details

.active_serversObject



201
202
203
# File 'lib/vrowser.rb', line 201

def self.active_servers
  Servers.filter(:status => 'UP')
end

.before_update(server_info) ⇒ Object



165
166
167
# File 'lib/vrowser.rb', line 165

def self.before_update(server_info)
  server_info
end

.debug_listObject



231
232
233
234
235
# File 'lib/vrowser.rb', line 231

def self.debug_list
  Servers.all.each{ |server|
    puts "#{server.name}, #{server.host}"
  }
end

.fetch_serverlist(host, gametype, gamename, maxping) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/vrowser.rb', line 217

def self.fetch_serverlist(host, gametype, gamename, maxping)
  proc{
    @@logger.info "try to fetch server list"
    return QStat.query_serverlist(host, gametype, gamename, maxping)
  }.retry(:accept_exception => StandardError, :logger => @@logger)
rescue => ex
  @@logger.error "#{ex.inspect}"
  return []
end

.load_all_pluginsObject



251
252
253
# File 'lib/vrowser.rb', line 251

def self.load_all_plugins
  self.load_plugins(self.plugin_dir)
end

.load_config(config) ⇒ Object

Raises:

  • (ArgumentError)


266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/vrowser.rb', line 266

def self.load_config(config)
  raise ArgumentError.new("config['qstat']") unless config["qstat"]
  raise ArgumentError.new("config['database']") unless config["database"]
  config['plugins'] ||= []

  VrowserModel.connect(config["database"])

  config['plugins'].each{ |plugin_symbol|
    Vrowser.load_plugin plugin_symbol
  }

  Vrowser.new(config['qstat'].symbolize_keys)
end

.load_file(path) {|instance| ... } ⇒ Object

Yields:

  • (instance)


280
281
282
283
284
# File 'lib/vrowser.rb', line 280

def self.load_file(path)
  instance = self.load_config YAML.load_file(path)
  yield(instance) if block_given?
  instance
end

.load_plugin(plugin_name) ⇒ Object



262
263
264
# File 'lib/vrowser.rb', line 262

def self.load_plugin(plugin_name)
  load self.plugin_path(plugin_name.to_s)
end

.load_plugins(dir) ⇒ Object



255
256
257
258
259
260
# File 'lib/vrowser.rb', line 255

def self.load_plugins(dir)
  Dir.entries(dir).each{ |entry|
    next if entry == "." or entry == ".."
    load File.join(dir, entry)
  }
end

.logger=(logger) ⇒ Object



56
57
58
# File 'lib/vrowser.rb', line 56

def self.logger=(logger)
  @@logger = logger
end

.plugin_dirObject



243
244
245
# File 'lib/vrowser.rb', line 243

def self.plugin_dir
  File.expand_path(File.join(File.dirname(__FILE__), "./plugins"))
end

.plugin_path(plugin_name) ⇒ Object



247
248
249
# File 'lib/vrowser.rb', line 247

def self.plugin_path(plugin_name)
  File.expand_path(File.join(self.plugin_dir, plugin_name + ".rb"))
end

.qstat_path=(path) ⇒ Object



60
61
62
# File 'lib/vrowser.rb', line 60

def self.qstat_path=(path)
  QStat.qstat_path = path
end

.read_serverlist_from_xml(path) ⇒ Object



227
228
229
# File 'lib/vrowser.rb', line 227

def self.read_serverlist_from_xml(path)
  QStat.read_from_xml(path)
end

.recommend_game_type(info) ⇒ Object



2
3
4
5
6
7
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
# File 'lib/plugins/l4d2.rb', line 2

def self.recommend_game_type(info)
  server_name = info.server_name
  game_type = info.suggest_game_type
  if game_type != 'unknown'
    return game_type
  end

  if server_name =~ /co\-?op/i
    return 'coop'
  elsif server_name =~ /scavenge/i
    return 'scavenge'
  elsif server_name =~ /survival/i
    return 'survival'
  elsif server_name =~ /realism/i
    return 'realism'
  elsif server_name =~ /versus/i
    return 'versus'
  elsif server_name =~ /hard(eight|six|twelve)/i
    return 'coop'
  elsif server_name =~ /(exp(ert)?|advance|easy|normal)/i
    return 'coop'
  elsif server_name =~ /RPG/
    return 'coop'
  elsif server_name =~ /4vs4/i
    return 'versus'
  elsif server_name =~ /vs/i
    return 'versus'
  elsif server_name =~ /team/i
    return 'versus'
  elsif server_name =~ /confogl/i
    return 'versus'
  elsif server_name =~ /fresh(\s*config)?/i
    return 'versus'
  elsif server_name =~ /skullsaba/i
    return 'versus'
  elsif server_name =~ /S A M U R A i/i
    return 'versus'
  elsif info.number_of_max_players == '4'
    return 'coop'
  elsif info.number_of_max_players == '8'
    return 'versus'
  else
    return 'unknown'
  end
end

.remove_allObject



205
206
207
208
# File 'lib/vrowser.rb', line 205

def self.remove_all
  o = Servers.delete
  @@logger.info "remove all records: #{o}"
end

.remove_debrisObject



210
211
212
213
214
215
# File 'lib/vrowser.rb', line 210

def self.remove_debris
  records = Servers.filter(:status => 'DOWN')
  @@logger.info "remove debris: count #{records.count}"
  records.delete
  @@logger.info "removed"
end

.serversObject



197
198
199
# File 'lib/vrowser.rb', line 197

def self.servers
  Servers.all
end

.update(host, protocol) ⇒ Object



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
# File 'lib/vrowser.rb', line 134

def self.update(host, protocol)
  new_info = QStat.query(host, protocol)
  if new_info.nil? or new_info.no_response? or new_info.down?
    @@logger.info "server is downing"
    Servers.find(:host => host).update(:status => 'DOWN')
    return
  end

  new_info = self.before_update(new_info)
  @@logger.info "game_type is #{new_info.game_type}"

  record = Servers.find(:host => host)
  if record
    record.update(:name => new_info.server_name,
                  :status => 'UP', :ping => new_info.ping,
                  :num_players => new_info.number_of_players,
                  :type => new_info.game_type,
                  :map => new_info.map,
                  :players => new_info.players.map(&:name).join(','))
    @@logger.info "updated: #{new_info.addr}, #{new_info.server_name}"
  else
    @@logger.info "not found record: #{record.inspect.players}"
    @@logger.info "#{new_info.server_name}"
    Servers.insert(:host => new_info.addr, :name => new_info.server_name,
                   :status => 'UP', :ping => new_info.ping, :num_players => new_info.number_of_players,
                   :type => new_info.game_type, :map => new_info.map,
                   :players => new_info.players.map(&:name).join(','))
                   @@logger.info "inserted: #{new_info.addr}, #{new_info.server_name}"
  end
end

.update_info(host, protocol) ⇒ Object



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
# File 'lib/vrowser.rb', line 169

def self.update_info(host, protocol)
  new_info = QStat.query_serverinfo(host, protocol)
  if new_info.nil? or new_info.no_response? or new_info.down?
    @@logger.info "server is downing"
    Servers.find(:host => host).update(:status => 'DOWN')
    return
  end

  new_info = self.before_update(new_info)
  @@logger.info "game_type is #{new_info.game_type}"

  record = Servers.find(:host => host)
  if record
    record.update(:name => new_info.server_name,
                  :status => 'UP', :ping => new_info.ping,
                  :type => new_info.game_type,
                  :map => new_info.map)
    @@logger.info "updated: #{new_info.addr}, #{new_info.server_name}"
  else
    @@logger.info "not found record: #{record.inspect.players}"
    @@logger.info "#{new_info.server_name}"
    Servers.insert(:host => new_info.addr, :name => new_info.server_name,
                   :status => 'UP', :ping => new_info.ping, 
                   :type => new_info.game_type, :map => new_info.map)
                   @@logger.info "inserted: #{new_info.addr}, #{new_info.server_name}"
  end
end

.update_info_registered_all(protocol) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vrowser.rb', line 119

def self.update_info_registered_all(protocol)
  updated = 0
  begin
    Servers.all.each{ |server|
      @@logger.info "trying to update_info: #{server.host}, #{server.name}"
      self.update_info(server.host, protocol)
      updated += 1
    }
  rescue => ex
    @@logger.error "#{ex.inspect}"
  ensure
    @@logger.info "updated #{updated} servers"
  end
end

.update_registered_all(protocol) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vrowser.rb', line 104

def self.update_registered_all(protocol)
  updated = 0
  begin
    Servers.all.each{ |server|
      @@logger.info "trying to update: #{server.host}, #{server.name}"
      self.update(server.host, protocol)
      updated += 1
    }
  #rescue => ex
  #  @@logger.error "#{ex.inspect}"
  ensure
    @@logger.info "updated #{updated} servers"
  end
end

.update_server_typesObject



237
238
239
240
241
# File 'lib/vrowser.rb', line 237

def self.update_server_types
  active_servers.each{ |sv|
    update(sv.host)
  }
end

.update_serverlist(host, gametype, gamename, maxping) ⇒ Object



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
# File 'lib/vrowser.rb', line 64

def self.update_serverlist(host, gametype, gamename, maxping)
  inserted = updated = 0

  servers = self.fetch_serverlist(host, gametype, gamename, maxping)
  servers.each{ |sv|
    if sv.rules.empty?
      game_type = "unknown"
    else
      game_type = sv.rules.first.game_tags.first
    end
    @@logger.info "game_type is #{game_type}"

    @@logger.info "finding hostname: #{sv.addr}"
    record = Servers.find(:host => sv.addr)
    @@logger.info "record result: #{record.inspect}"

    if record.nil?
      @@logger.info "new record for #{sv.addr}"
      Servers.insert(:name => sv.server_name, :host => sv.addr,
                     :status => sv.status, :ping => sv.ping, :num_players => sv.number_of_players,
                     :type => game_type, :map => sv.map, :players => sv.players.map(&:name).join(',')
                    )
                    inserted += 1
    else
      @@logger.info "already exist hostname, record update: #{sv.addr}"
      record.update(
        :name => sv.server_name, :status => sv.status, 
        :ping => sv.ping,
        #:num_players => sv.number_of_players,
        #:type => game_type,
        #:map => sv.map,
        #:players => sv.players.map(&:name).join(',')
      )
      updated += 1
    end
  }
  @@logger.info "updated exit: inserted:#{inserted}, updated:#{updated}"
  servers
end

Instance Method Details

#active_serversObject



316
317
318
# File 'lib/vrowser.rb', line 316

def active_servers
  self.class.active_servers
end

#clearObject



308
309
310
# File 'lib/vrowser.rb', line 308

def clear
  self.class.remove_debris
end

#fetchObject



296
297
298
# File 'lib/vrowser.rb', line 296

def fetch
  self.class.update_serverlist(@master_server, @gametype, @gamename, @maxping)
end

#serversObject



312
313
314
# File 'lib/vrowser.rb', line 312

def servers
  self.class.servers
end

#updateObject



300
301
302
# File 'lib/vrowser.rb', line 300

def update
  self.class.update_registered_all(@protocol)
end

#update_only_infoObject



304
305
306
# File 'lib/vrowser.rb', line 304

def update_only_info
  self.class.update_info_registered_all(@protocol)
end