Created
March 27, 2017 00:19
-
-
Save reitermarkus/5ab617999b7deeed6e65028cee6047d1 to your computer and use it in GitHub Desktop.
Fetch CoD 4 server info and generate nice JSON output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Fetch CoD 4 server info and generate nice JSON output. | |
# | |
# Usage: | |
# `$0 0.0.0.0:00000` | |
require 'json' | |
require 'socket' | |
def get_server_info(host, port) | |
server_info, *players = UDPSocket.new.tap { | |
|s| s.send("\xFF\xFF\xFF\xFFgetstatus", 0, host, port) | |
}.recvfrom(1 << 16).first.chomp.gsub(/\^(\d)/, '').split("\n")[1..-1] | |
Hash[server_info.split("\\")[1..-1].each_slice(2).to_a].tap { |h| | |
h["players"] = players.map { |p| | |
/(?<score>\d+) (?<ping>\d+) \"(?<name>.*)\"/ =~ p | |
{"score" => score, "ping" => ping, "name" => name } | |
} | |
} | |
end | |
print JSON.pretty_generate(get_server_info(*ARGV.first.split(":"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment