09-02-2009, 11:31 PM
I want to thank "Toast" from this originating thread => Query server with php
I took their code and ported it over to Ruby for anyone interested in using it with RoR.
I tried to grab all information possible to parse out the data being returned into a Struct object..
Querying a Server with Ruby
To get the information, just pull the attributes from the struct.
e.g. Here is a sample of how to get player count
My programming sucks, but feel free to take and improve
I took their code and ported it over to Ruby for anyone interested in using it with RoR.
I tried to grab all information possible to parse out the data being returned into a Struct object..
Querying a Server with Ruby
Code:
require "socket"
require "struct"
UDP_RECV_TIMEOUT = 3
Settings = Struct.new(:server_name, :map, :game_directory,
:game_description, :appID, :number_players,
:maximum_players, :number_bots, :dedicated,
:os, :password, :secure, :game_version)
def query(host, port = 27015)
host = host.to_s
sock = UDPSocket.open
sock.send("\377\377\377\377TSource Engine Query\0", 0,host,port)
resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)
sock.recvfrom(65536)
end
data = resp[0].unpack('@6Z*Z*Z*Z*vcccaaccZ*')
server_info = Settings.new(data[0],data[1],data[2],data[3],data[4],data[5],
data[6],data[7],data[8],data[9],data[10],data[11],
data[12])
return server_info
end
To get the information, just pull the attributes from the struct.
e.g. Here is a sample of how to get player count
Code:
server = query("someserver.com")
puts "#{server.server_name} has #{server.number_players} / #{server.maximum_players} players"
My programming sucks, but feel free to take and improve