Class: Query
- Inherits:
-
Object
- Object
- Query
- Defined in:
- lib/query/query.rb
Class Method Summary collapse
- .fullQuery(addr = 'localhost', port = 25565) ⇒ Object
- .init ⇒ Object
- .key ⇒ Object
- .simpleQuery(addr = 'localhost', port = 25565) ⇒ Object
Class Method Details
.fullQuery(addr = 'localhost', port = 25565) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/query/query.rb', line 47 def self.fullQuery(addr = 'localhost', port = 25565) @addr = addr @port = port init begin timeout(1) do query = @sock.send("\xFE\xFD\x00\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT) + @key.to_s + "\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT), 0) data = @sock.recvfrom(1460)[0] buffer = data[11...-1] items, players = buffer.split("\x00\x00\x01player_\x00\x00".force_encoding(Encoding::ASCII_8BIT)) if items[0...8] == 'hostname' items = 'motd' + items[8...-1] end vals = {} items = items.split("\x00") items.each_with_index do |key, idx| next unless idx % 2 == 0 vals[key] = items[idx + 1] end vals["motd"] = vals["hostname"] vals.delete("hostname") vals.delete("um") if vals["um"] players = players[0..-2] if players if players vals[:players] = players.split("\x00") end puts vals vals["raw_plugins"] = vals["plugins"] parts = vals["raw_plugins"].split(":") server = parts[0].strip() if parts[0] plugins = [] if parts.size == 2 plugins = parts[1].split(";").map {|value| value.strip() } end vals["plugins"] = plugins vals["server"] = server vals["timestamp"] = Time.now return vals.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} end rescue StandardError => e return "An other error occured. Check your minecraft config and tell the tech monkey this:" raise e end end |
.init ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/query/query.rb', line 2 def self.init @sock = UDPSocket.new @sock.connect(@addr,@port) @val = {} @buff = nil key end |
.key ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/query/query.rb', line 10 def self.key begin timeout(1) do start = @sock.send("\xFE\xFD\x09\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT), 0) t = @sock.recvfrom(1460)[0] key = t[5...-1].to_i @key = Array(key).pack('N') end rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH return "Host unreachable, check your configuration and try again" return "An Exception occured, please check last message" rescue StandardError => e return "An other error occured. Check your ruby installation and tell the tech monkey this:" return e end end |
.simpleQuery(addr = 'localhost', port = 25565) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/query/query.rb', line 27 def self.simpleQuery(addr = 'localhost', port = 25565) @addr = addr @port = port init begin timeout(1) do query = @sock.send("\xFE\xFD\x00\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT) + @key.to_s, 0) data = @sock.recvfrom(1460)[0] buffer = data[5...-1] @val[:motd], @val[:gametype], @val[:map], @val[:numplayers], @val[:maxplayers], @buf = buffer.split("\x00", 6) if @sock != nil @sock.close end end return @val rescue StandardError => e return e end end |