Class: UrbanTerror

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanterror.rb

Constant Summary collapse

GEAR_TYPES =
{
  'knives'   => 0,
  'grenades' => 1,
  'snipers'  => 2,
  'spas'     => 4,
  'pistols'  => 8,
  'autos'    => 16,
  'negev'    => 32
}
MAX_GEAR =
63
GAME_MODES =
{
  0 => ['Free For All',      'FFA'],
  3 => ['Team Death Match',  'TDM'],
  4 => ['Team Survivor',     'TS'],
  5 => ['Follow the Leader', 'FTL'],
  6 => ['Capture and Hold',  'CAH'],
  7 => ['Capture the Flag',  'CTF'],
  8 => ['Bomb mode',         'BOMB']
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port = nil, rcon = nil) ⇒ UrbanTerror

Returns a new instance of UrbanTerror.



7
8
9
10
11
12
# File 'lib/urbanterror.rb', line 7

def initialize(server, port=nil, rcon=nil)
  @server = server
  @port = port.nil? ? 27960 : port
  @rcon = rcon.nil? ? '' : rcon
  @socket = UDPSocket.open
end

Class Method Details

.gearCalc(gearArray) ⇒ Object



67
68
69
70
# File 'lib/urbanterror.rb', line 67

def self.gearCalc(gearArray)
  gearArray.each{ |w| raise "No such gear type '#{w}'" unless GEAR_TYPES.has_key?(w) }
  MAX_GEAR - gearArray.map{|w| GEAR_TYPES[w] }.reduce(:+)
end

.matchType(number, abbreviate = false) ⇒ Object



87
88
89
90
# File 'lib/urbanterror.rb', line 87

def self.matchType(number, abbreviate=false)
  raise "#{number} is not a valid gametype." unless GAME_MODES.has_key? number
  GAME_MODES[number][abbreviate ? 1 : 0]
end

.reverseGearCalc(number) ⇒ Object



72
73
74
75
# File 'lib/urbanterror.rb', line 72

def self.reverseGearCalc(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

Instance Method Details

#get(command) ⇒ Object



20
21
22
# File 'lib/urbanterror.rb', line 20

def get(command)
  sendCommand("get#{command}")
end

#getparts(command, i) ⇒ Object



24
25
26
# File 'lib/urbanterror.rb', line 24

def getparts(command, i)
  get(command).split("\n")[i]
end

#playersObject

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/urbanterror.rb', line 43

def players
  results = getparts("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) ⇒ Object



28
29
30
# File 'lib/urbanterror.rb', line 28

def rcon(command)
  sendCommand("rcon #{@rcon} #{command}")
end

#sendCommand(command) ⇒ Object



14
15
16
17
18
# File 'lib/urbanterror.rb', line 14

def sendCommand(command)
  magic = "\377\377\377\377"
  @socket.send("#{magic}#{command}\n", 0, @server, @port)
  @socket.recv(2048)
end

#settingsObject

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.



36
37
38
39
# File 'lib/urbanterror.rb', line 36

def settings
  result = getparts("status", 1).split("\\").reject(&:empty?)
  Hash[*result]
end