Class: UrbanTerror

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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/urbanterror.rb', line 55

def self.gearCalc(gearArray)
  initial = 63
  selected_i = 0
  selected = []
  gearMaster = {
    'grenades' => 1,
    'snipers'  => 2,
    'spas'     => 4,
    'pistols'  => 8,
    'autos'    => 16,
    'negev'    => 32
  }

  gearArray.each do |w|
    if gearMaster.has_key? w
      selected << gearMaster[w]
    end
  end

  selected_i = selected.inject(:+)
  return initial - selected_i
end

.matchType(number, abbreviate = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/urbanterror.rb', line 97

def self.matchType(number, abbreviate=false)
  # 0=FreeForAll, 3=TeamDeathMatch, 4=Team Survivor, 5=Follow the Leader, 6=Capture and Hold, 7=Capture The Flag, 8=Bombmode
  match = {
    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']
  }

  throw "#{number} is not a valid gametype." if not match.has_key? number
  if abbreviate
    match[number][1]
  else
    match[number][0]
  end
end

.reverseGearCalc(number) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/urbanterror.rb', line 78

def self.reverseGearCalc(number)
  weapons = []
  gearMaster = {
    1 => 'grenades',
    2 => 'snipers',
    4 => 'spas',
    8 => 'pistols',
    16 => 'autos',
    32 => 'negev'
  }
  
  gearMaster.each do |num, weapon|
    if number & num == 0
      weapons << weapon
    end
  end
  return weapons
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