Class: Six::Query::Gamespy

Inherits:
Base show all
Defined in:
lib/six-updater-web/vendor/plugins/six-query/lib/six/query/gamespy.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#ic

Instance Method Summary collapse

Methods inherited from Base

#clean, #logger

Constructor Details

#initialize(host, port, number = 1) ⇒ Gamespy

Returns a new instance of Gamespy.



22
23
24
# File 'lib/six-updater-web/vendor/plugins/six-query/lib/six/query/gamespy.rb', line 22

def initialize(host, port, number = 1)
  @host, @port = host, port
end

Instance Attribute Details

#gamedataObject (readonly)

Returns the value of attribute gamedata.



19
20
21
# File 'lib/six-updater-web/vendor/plugins/six-query/lib/six/query/gamespy.rb', line 19

def gamedata
  @gamedata
end

Instance Method Details

#fetchObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/six-updater-web/vendor/plugins/six-query/lib/six/query/gamespy.rb', line 75

def fetch
  s = UDPSocket.new
  s.connect(@host, @port)
  s.puts(PACKET)

  status, reply = nil, nil
  begin
    status = Timeout::timeout(4) do
      reply = s.recvfrom(4096)[0]
    end
  rescue Timeout::Error
    raise TimeoutError
  ensure
    s.close
  end

  return nil unless reply

  reply.gsub!("\x00\x04\x05\x06\a", '')
  reply.split(SPLIT * 3)
end

#syncObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/six-updater-web/vendor/plugins/six-query/lib/six/query/gamespy.rb', line 26

def sync
  @gamedata, key = Hash.new, nil
  reply = self.fetch
  return @gamedata unless reply

  gamedata = reply[0]
  gamedata.split(SPLIT).each_with_index do |c, i|
    if ((i + 1) % 2) > 0
      key = c
    else
      c = clean(c)
      c = c.to_s if key == "hostname" # FIXME: Bah!
      @gamedata[key] = c
    end
  end

  @gamedata["players"] = []
  if @gamedata["numplayers"].to_i > 0
    if reply.size > 1
      players = reply[1]
      colums = "player_\x00team_\x00score_\x00deaths_\x00\x00"
      players.gsub!(colums, '')
      players.gsub!(/\A./, '')

      player, nplayers = nil, []
      players.split(SPLIT).each_with_index do |c, i|
        r = ((i + 1) % 4)
        case r
          when 1
            player = Hash.new
            nplayers << player
            player["name"] = clean(c).to_s
          when 2
            player["team"] = clean(c).to_s
          when 3
            c += player["score"] if player["score"]
            player["score"] = clean(c)
          when 0
            c += player["deaths"] if player["deaths"]
            player["deaths"] = clean(c)
        end
      end
      @gamedata["players"] = nplayers
      @gamedata["players"].sort! {|a, b| a["name"].downcase <=> b["name"].downcase }
    end
  end
  @gamedata
end