class UrbanTerror

Constants

GAME_MODES
GEAR_TYPES
MAX_GEAR

Public Class Methods

gear_calc(gear_array) click to toggle source
# File lib/urbanterror.rb, line 63
def self.gear_calc(gear_array)
  gear_array.each{ |w| raise "No such gear type '#{w}'" unless GEAR_TYPES.has_key?(w) }
  MAX_GEAR - gear_array.map{|w| GEAR_TYPES[w] }.reduce(:+)
end
match_type(number, abbreviate=false) click to toggle source
# File lib/urbanterror.rb, line 84
def self.match_type(number, abbreviate=false)
  raise "#{number} is not a valid gametype." unless GAME_MODES.has_key? number
  GAME_MODES[number][abbreviate ? 1 : 0]
end
new(server, port=nil, rcon=nil) click to toggle source
# File lib/urbanterror.rb, line 7
def initialize(server, port=nil, rcon=nil)
  @server = server
  @port = port || 27960
  @rcon = rcon || ''
  @socket = UDPSocket.open
end
reverse_gear_calc(number) click to toggle source
# File lib/urbanterror.rb, line 68
def self.reverse_gear_calc(number)
  raise "#{number} is outside of the range 0 to 63." unless (0..63).include?(number)
  GEAR_TYPES.select{|weapon, gear_val| number & gear_val == 0 }.map(&:first)
end

Public Instance Methods

get(command) click to toggle source
# File lib/urbanterror.rb, line 20
def get(command)
  send_command("get#{command}")
end
players() click to toggle source

players() returns a list of hashes. Each hash contains name, score, ping.

# File lib/urbanterror.rb, line 39
def players
  results = get_parts("status", 2..-1)
  results.map do |player|
    player = player.split(" ", 3)
    {
      :name => player[2][1..-2],
      :ping => player[1].to_i,
      :score => player[0].to_i
    }
  end
end
rcon(command) click to toggle source
# File lib/urbanterror.rb, line 24
def rcon(command)
  send_command("rcon #{@rcon} #{command}")
end
send_command(command) click to toggle source
# File lib/urbanterror.rb, line 14
def send_command(command)
  magic = "\377\377\377\377"
  @socket.send("#{magic}#{command}\n", 0, @server, @port)
  @socket.recv(2048)
end
settings() click to toggle source

settings() returns a hash of settings => values. We /were/ going to accept an optional setting arg, but it would be doing the same thing and just selecting one from the Hash, so why not just let the user do server.settings or whatever.

# File lib/urbanterror.rb, line 32
def settings
  result = get_parts("status", 1).split("\\").reject(&:empty?)
  Hash[*result]
end

Private Instance Methods

get_parts(command, i) click to toggle source
# File lib/urbanterror.rb, line 90
def get_parts(command, i)
  get(command).split("\n")[i]
end