Class: KAG::Server::Instance
- Inherits:
-
SymbolTable
- Object
- SymbolTable
- KAG::Server::Instance
- Defined in:
- lib/kag/server/instance.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_cycle ⇒ Object
- #_is_newline?(line) ⇒ Boolean
- #_parse_line(line) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #has_rcon? ⇒ Boolean
- #in_use? ⇒ Boolean
- #info ⇒ Object
- #kick(nick) ⇒ Object
- #kick_all ⇒ Object
- #next_map ⇒ Object
-
#players ⇒ Object
broken.
- #restart_map ⇒ Object
- #socket ⇒ Object
- #text_join ⇒ Object
Class Method Details
Instance Method Details
#_cycle ⇒ Object
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
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 |
#connect ⇒ Object
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. puts e.backtrace.join("\n") end success end |
#connected? ⇒ Boolean
82 83 84 |
# File 'lib/kag/server/instance.rb', line 82 def connected? self[:_connected] end |
#disconnect ⇒ Object
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
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
24 25 26 |
# File 'lib/kag/server/instance.rb', line 24 def in_use? self.key?(:match) end |
#info ⇒ Object
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_all ⇒ Object
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_map ⇒ Object
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 |
#players ⇒ Object
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_map ⇒ Object
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 |
#socket ⇒ Object
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. puts e.backtrace.join("\n") end end puts self[:_] self[:_socket] end |
#text_join ⇒ Object
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 |