Class: KAG::Server::Instance

Inherits:
SymbolTable
  • Object
show all
Defined in:
lib/kag/server/instance.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch_allObject



11
12
13
14
15
16
17
18
# File 'lib/kag/server/instance.rb', line 11

def self.fetch_all
  servers = {}
  KAG::Config.instance[:servers].each do |k,s|
    s[:key] = k
    servers[k] = KAG::Server::Instance.new(s)
  end
  servers
end

Instance Method Details

#_cycleObject



105
106
107
108
109
110
111
# File 'lib/kag/server/instance.rb', line 105

def _cycle
  while (line = self.socket.gets)
    line = _parse_line(line)
    puts "[RCON] "+line
    break if (_is_newline?(line) or line.empty?)
  end
end

#_is_newline?(line) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/kag/server/instance.rb', line 86

def _is_newline?(line)
  line.empty?
end

#_parse_line(line) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kag/server/instance.rb', line 90

def _parse_line(line)
  line.gsub!(/\r/,"")
  line.gsub!(/\n/,"")
  line = line.strip
  ep = line.index(']')
  if ep
    line = line[(ep+1)..line.length].strip
  else
    line = line[10..line.length].strip
  end
  line.gsub!(/\r/,"")
  line.gsub!(/\n/,"")
  line
end

#connectObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kag/server/instance.rb', line 49

def connect
  return true if self.connected?
  puts "[RCON] Attempting to get socket"
  unless self.socket
    puts "[RCON] Could not establish TCP socket to connect"
    return false
  end
  success = false
  begin
    self.socket.puts self[:rcon_password]
    z = self.socket.gets
    puts "[RCON] "+z.to_s
    z.include?("now authenticated")
    self[:_connected] = true
    success = true
  rescue Exception => e
    puts e.message
    puts e.backtrace.join("\n")
  end
  success
end

#connected?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/kag/server/instance.rb', line 82

def connected?
  self[:_connected]
end

#disconnectObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/kag/server/instance.rb', line 71

def disconnect
  if self[:_socket]
    puts "[RCON] Closing socket..."
    #self.socket.puts "/quit"
    self[:_socket].close
    self[:_connected] = false
    self.delete(:_socket)
  end
  true
end

#has_rcon?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/kag/server/instance.rb', line 28

def has_rcon?
  self[:rcon_password] and !self[:rcon_password].empty?
end

#in_use?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/kag/server/instance.rb', line 24

def in_use?
  self.key?(:match)
end

#infoObject



20
21
22
# File 'lib/kag/server/instance.rb', line 20

def info
  Kagerator.server(self[:ip],self[:port])
end

#kick(nick) ⇒ Object



158
159
160
161
162
# File 'lib/kag/server/instance.rb', line 158

def kick(nick)
  return false unless self.connect
  self.socket.puts "/kick #{nick}"
  _cycle
end

#kick_allObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kag/server/instance.rb', line 164

def kick_all
  return false unless self.connect
  ps = self.players
  if ps
    ps.each do |player|
      _command "/kick #{player[:nick]}"
    end
  else
    puts "No Players found on Server #{self[:ip]}!"
  end
  _cycle
end

#next_mapObject



183
184
185
186
187
# File 'lib/kag/server/instance.rb', line 183

def next_map
  return false unless self.connect
  _command "/nextmap"
  _cycle
end

#playersObject

broken



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kag/server/instance.rb', line 116

def players
  return false unless self.connect
  _command "/rcon /players"

  players = []
  while (line = self.socket.gets)
    puts "[RCONPRIOR] '"+line+"'"
    line = _parse_line(line)
    puts "[RCON] '"+line+"'"
    break if (line.empty? or line == '' or line == "\n")

    player = SymbolTable.new

    # get nick
    sp = line.index("[")
    next if sp == nil
    ep = line.index("]",sp)
    player[:nick] = line[(sp+1)..(ep-1)]
    line = line[ep..line.length].strip

    # get id
    sp = line.index("(")
    ep = line.index(")",sp)
    player[:id] = line[(sp+4)..(ep-1)]
    line = line[ep..line.length]

    # get ip
    sp = line.index("(")
    ep = line.index(")",sp)
    player[:ip] = line[(sp+4)..(ep-1)]
    line = line[ep..line.length]

    # get hwid
    sp = line.index("(")
    ep = line.index(")",sp)
    player[:hwid] = line[(sp+6)..(ep-1)]

    players << player
  end
  players
end

#restart_mapObject



177
178
179
180
181
# File 'lib/kag/server/instance.rb', line 177

def restart_map
  return false unless self.connect
  _command "/restartmap"
  _cycle
end

#socketObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kag/server/instance.rb', line 32

def socket
  unless self[:_socket]
    begin
      self[:_socket] = TCPSocket.new(self[:ip],self[:port])
    rescue Exception => e
      puts e.message
      puts e.backtrace.join("\n")
    end
  end
  puts self[:_]
  self[:_socket]
end

#text_joinObject



45
46
47
# File 'lib/kag/server/instance.rb', line 45

def text_join
  "Join \x0305#{self[:key]} - #{self[:ip]}:#{self[:port]} \x0306password #{self[:password]}\x0301 | Visit kag://#{self[:ip]}/#{self[:password]}"
end