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



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

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/six-updater-web/app/models/queryserver.rb', line 174

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



162
163
164
165
166
167
168
# File 'lib/six-updater-web/app/models/queryserver.rb', line 162

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



170
171
172
# File 'lib/six-updater-web/app/models/queryserver.rb', line 170

def self.purge
  self.delete_all
end

Instance Method Details

#addressObject



25
26
27
# File 'lib/six-updater-web/app/models/queryserver.rb', line 25

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

#convert(data) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/six-updater-web/app/models/queryserver.rb', line 88

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



111
112
113
114
115
116
117
118
119
# File 'lib/six-updater-web/app/models/queryserver.rb', line 111

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



125
126
127
# File 'lib/six-updater-web/app/models/queryserver.rb', line 125

def exec2
  ""
end

#exportObject



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

def export

end

#export_to_presetObject



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

def export_to_preset
  cfg = Sixconfig.new(:name => "CUST - #{self.name} clone", :mods => self.mods.reject {|e| e.new_record? })
  cfg.save
  cfg
end

#gameObject



33
34
35
36
37
38
39
40
# File 'lib/six-updater-web/app/models/queryserver.rb', line 33

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



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

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



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

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/six-updater-web/app/models/queryserver.rb', line 42

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/six-updater-web/app/models/queryserver.rb', line 68

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



29
30
31
# File 'lib/six-updater-web/app/models/queryserver.rb', line 29

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

#to_updater_ymlObject



121
122
123
# File 'lib/six-updater-web/app/models/queryserver.rb', line 121

def to_updater_yml
  self.dynamic_server.to_updater_yml
end