Class: CallOfDuty::Cod4Server
- Inherits:
-
Object
- Object
- CallOfDuty::Cod4Server
- Defined in:
- lib/call_of_duty/cod4_server.rb
Overview
Thanks Tom Russell www.trdev.co.uk/posts/hello-world/
Constant Summary collapse
- QUERY_TIMEOUT =
maximum query of 5 seconds
5
- QUERY_STRING =
"\xFF\xFF\xFF\xFFgetstatus\x00"
Instance Attribute Summary collapse
-
#currplayers ⇒ Object
Returns the value of attribute currplayers.
-
#ip_address ⇒ Object
Returns the value of attribute ip_address.
-
#mapname ⇒ Object
Returns the value of attribute mapname.
-
#maxplayers ⇒ Object
Returns the value of attribute maxplayers.
-
#players ⇒ Object
Returns the value of attribute players.
-
#port ⇒ Object
Returns the value of attribute port.
-
#response ⇒ Object
Returns the value of attribute response.
-
#servername ⇒ Object
Returns the value of attribute servername.
Instance Method Summary collapse
-
#initialize(ip_address, port = "28960") ⇒ Cod4Server
constructor
A new instance of Cod4Server.
- #query_server ⇒ Object
Constructor Details
#initialize(ip_address, port = "28960") ⇒ Cod4Server
Returns a new instance of Cod4Server.
16 17 18 19 |
# File 'lib/call_of_duty/cod4_server.rb', line 16 def initialize(ip_address, port = "28960") @ip_address = ip_address @port = port end |
Instance Attribute Details
#currplayers ⇒ Object
Returns the value of attribute currplayers.
14 15 16 |
# File 'lib/call_of_duty/cod4_server.rb', line 14 def currplayers @currplayers end |
#ip_address ⇒ Object
Returns the value of attribute ip_address.
12 13 14 |
# File 'lib/call_of_duty/cod4_server.rb', line 12 def ip_address @ip_address end |
#mapname ⇒ Object
Returns the value of attribute mapname.
13 14 15 |
# File 'lib/call_of_duty/cod4_server.rb', line 13 def mapname @mapname end |
#maxplayers ⇒ Object
Returns the value of attribute maxplayers.
14 15 16 |
# File 'lib/call_of_duty/cod4_server.rb', line 14 def maxplayers @maxplayers end |
#players ⇒ Object
Returns the value of attribute players.
13 14 15 |
# File 'lib/call_of_duty/cod4_server.rb', line 13 def players @players end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/call_of_duty/cod4_server.rb', line 12 def port @port end |
#response ⇒ Object
Returns the value of attribute response.
13 14 15 |
# File 'lib/call_of_duty/cod4_server.rb', line 13 def response @response end |
#servername ⇒ Object
Returns the value of attribute servername.
13 14 15 |
# File 'lib/call_of_duty/cod4_server.rb', line 13 def servername @servername end |
Instance Method Details
#query_server ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/call_of_duty/cod4_server.rb', line 21 def query_server begin socket = UDPSocket.open socket.send(QUERY_STRING, 0, @ip_address, @port) response = if IO.select([socket], nil, nil, QUERY_TIMEOUT) socket.recvfrom(65536) #wait 5 seconds to recieve 65536 bytes of data end if response data = response.first.split("\\") players_arr = data.last.split("\n") players_arr.shift @maxplayers = data[data.index("sv_maxclients")+1] @currplayers = players_arr.size @players = [] players_arr.sort! do |a,b| t1 = a.split(" ") t2 = b.split(" ") t2[0].to_i <=> t1[0].to_i end players_arr.each do |player| p = player.split(" ") score = p[0] ping = p[1] p.delete_at(0) p.delete_at(0) p = p.join(" ") @players.push({:score => score, :ping => ping, :name => p.gsub("\"","")}) end @mapname = data[data.index("mapname")+1] @servername = data[data.index("sv_hostname")+1] @response = response end rescue IOError, SystemCallError #TODO: something smart ensure socket.close if socket end response ? true : false end |