Class: Queryserver

Inherits:
ActiveRecord::Base show all
Defined in:
lib/six-updater-web/app/models/queryserver.rb

Constant Summary collapse

FIELDS =
[:numplayers, :gamestate, :language, :ping, :difficulty, :gamever, :gametype, :gamemode, :mission, :mapname, :country,
:platform, :sv_battleye, :verifysignatures, :password, :dedicated, :players]
FIELDS2 =
[:mod, :signatures]
SYS_MODS =
:maxplayers, :equalModRequired, :numteams, :timelimit, :param1, :param2, :currentVersion, :requiredVersion, :gamename
["CA", "ca", "expansion"]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#associated_valid?, #no_errors_in_associated?, #save_associated, #save_associated!, #save_with_unsaved_flag, #unsaved=, #unsaved?

Class Method Details

.cleanObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/six-updater-web/app/models/queryserver.rb', line 140

def self.clean
  ar = []
  self.find(:all, :conditions => ["numplayers > 0 AND updated_at < ?", 20.minutes.ago]).each do |r|
    r.mission = ""
    r.players = []
    r.numplayers = 0
    r.save
    ar << r
  end
  ar
end

.import(e) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/six-updater-web/app/models/queryserver.rb', line 164

def self.import(e)
  return nil unless e[:gamedata]
  s = self.find_by_ip_and_port(e[:ip], e[:port])
  unless s
    s = self.new(:ip => e[:ip], :port => e[:port])
    #s.name = e[:gamedata]["hostname"].split(",").join(", ") if e[:gamedata]["hostname"]
    # s.save # only when using 'update later'
  end
  s.name = e[:gamedata]["hostname"].split(",").join(", ") if e[:gamedata]["hostname"]
  r = s.convert(e[:gamedata])
  s.attributes = r

  if s.mod
    # TODO: Can't use the unless condition this until we can measure if there are any new Mod records too
    s.mods = s.mods_fetch2 # unless s.mod == oldmod
  end
  return [s, r]
end

.pruneObject



152
153
154
155
156
157
158
# File 'lib/six-updater-web/app/models/queryserver.rb', line 152

def self.prune
  ar = []
  self.find(:all, :conditions => ["updated_at < ?", 7.days.ago]).each do |r|
    r.delete unless r.favorite # Don't prune favorites
  end
  ar
end

.purgeObject



160
161
162
# File 'lib/six-updater-web/app/models/queryserver.rb', line 160

def self.purge
  self.delete_all
end

Instance Method Details

#addressObject



15
16
17
# File 'lib/six-updater-web/app/models/queryserver.rb', line 15

def address
  [self.ip, self.port].join(":")
end

#convert(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/six-updater-web/app/models/queryserver.rb', line 78

def convert(data)
  r = Hash.new
  data.each_pair do |k, v|
    if !k.is_a? String
      logger.info "Key that is no string: #{k} #{v}!"
      #STDIN.gets
    else
      if k.empty?
        logger.info "Key that is empty: #{k} #{v}!"
        next
      end
      if FIELDS.include?(k.to_sym)
        r[k] = v
      end
      if FIELDS2.include?(k.to_sym)
        r[k] = v.split(";").reject{|e| e.empty?} 
      end
    end
  end
  r
end

#dynamic_serverObject



101
102
103
104
105
106
107
108
109
# File 'lib/six-updater-web/app/models/queryserver.rb', line 101

def dynamic_server
  s = Server.new
  s.name = self.name
  s.address = self.ip
  s.port = self.port
  s.password = self.saved_password
  s.mods = self.mods_fetch
  s
end

#exec2Object



115
116
117
# File 'lib/six-updater-web/app/models/queryserver.rb', line 115

def exec2
  ""
end

#gameObject



23
24
25
26
27
28
29
30
# File 'lib/six-updater-web/app/models/queryserver.rb', line 23

def game
  if self.gamever
    # TODO: Figure out how to determine Combined Ops or Standalone
    self.gamever[/^(1\.[0-9]*)/]
    return nil unless $1
    $1.to_f < 1.5 ? "A2" : "OA"
  end
end

#get_latestObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/six-updater-web/app/models/queryserver.rb', line 129

def get_latest
  # TODO: Also query with gslist, to get ping/country update?
  g = Six::Query::Gamespy.new(self.ip, self.port)
  begin
    g.sync
  rescue Six::Query::Base::TimeoutError
    logger.info "Timeout while accessing #{self.address}"
    nil
  end
end

#latestObject



119
120
121
122
123
124
125
126
127
# File 'lib/six-updater-web/app/models/queryserver.rb', line 119

def latest
  #unless self.updated_at < Time.now - 1.minutes
  n = get_latest
  if n
    self.attributes = self.convert(n)
    self.mods = self.mods_fetch2 if self.mod
  end
  n
end

#mods_fetchObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/six-updater-web/app/models/queryserver.rb', line 32

def mods_fetch
  if self.mod.size > 0
    self.mod.reject {|m| SYS_MODS.include? m }.map do |m|
      mods = Mod.find(:all, :conditions => "name LIKE '#{m}'") # needed for case insensitivity :S
      mod = nil
      mods.each do |mo|
        if mo.name.downcase == m.downcase
          mod = mo
          break
        else
          mod = nil
        end
      end
      unless mod
        # TODO: Filter for mods that are not installed, but actually requires an appsetting!
        mod = Mod.new
        mod.name = m
        mod.skip = true
      end
      mod
    end.reject {|m| m.nil? }
  else
    []
  end
end

#mods_fetch2Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/six-updater-web/app/models/queryserver.rb', line 58

def mods_fetch2
  if self.mod.size > 0
    self.mod.reject {|m| SYS_MODS.include? m }.map do |m|
      mods = Mod.find(:all, :conditions => "name LIKE '#{m}'") # needed for case insensitivity :S
      mod = nil
      mods.each do |mo|
        if mo.name.downcase == m.downcase
          mod = mo
          break
        else
          mod = nil
        end
      end
      mod
    end.reject {|m| m.nil? }
  else
    []
  end
end

#to_labelObject



19
20
21
# File 'lib/six-updater-web/app/models/queryserver.rb', line 19

def to_label
  "#{self.name} (#{self.game})"
end

#to_updater_ymlObject



111
112
113
# File 'lib/six-updater-web/app/models/queryserver.rb', line 111

def to_updater_yml
  self.dynamic_server.to_updater_yml
end