Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created February 24, 2015 04:47
Show Gist options
  • Save FilBot3/4e57fe175bed7b5aafdd to your computer and use it in GitHub Desktop.
Save FilBot3/4e57fe175bed7b5aafdd to your computer and use it in GitHub Desktop.
A work-in-progress MWO CW API script.
#
#
#
#
require 'net/https'
require 'uri'
require 'json'
require 'optparse'
opts = OptionParser.new
opts.on('-u', '--unit', String, "The unit to search for") do |opt|
$unit = opts
end
opts.on('-h', '--help', "Shows this screen") do
puts opts
exit 1
end
begin
opts.parse!(ARGV)
rescue OptionParser::ParseError => e
puts e
end
#raise OptionParser::MissingArgument, "Unit Name [-u]" if $unit.nil?
module Mechwarrior
class Api
def initialize
@PLANETIDS = Array(1..2240)
end # of initialize
def getJSON
puts "=> Downloading API"
url = 'https://static.mwomercs.com/data/cw/mapdata.json'
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
body = response.body
jsonOut = JSON.parse(body)
puts "Map Data generated at #{jsonOut['generated']}"
return jsonOut
end # of getJSON
def davion_report(mwo_api)
planetNumber = 0
puts ""
puts "Davion Planet Ownership Report"
puts ""
@PLANETIDS.each do |id|
if mwo_api["#{id}"]["owner"]["name"] == "Davion"
planetName = mwo_api["#{id}"]["name"]
houseName = mwo_api["#{id}"]["owner"]["name"]
invadeName = mwo_api["#{id}"]["invading"]["name"]
ownerName = mwo_api["#{id}"]["unit"]["name"]
puts "Planet #{planetName}, ID: #{id} is currnetly owned by #{ownerName} of the House #{houseName}"
puts "==> #{planetName} is being invaded by #{invadeName}" unless invadeName == "None"
planetNumber += 1
end # of Davion if
end # of PLANETIDS iteration
puts "Total Davion owned planets: #{planetNumber}"
end # of davion_report
def aw_report(mwo_api)
planetNumber = 0
puts ""
puts "Aces Wild Planetary Ownership Report"
puts ""
@PLANETIDS.each do |id|
if mwo_api["#{id}"]["unit"]["name"] == "AW"
planetName = mwo_api["#{id}"]["name"]
houseName = mwo_api["#{id}"]["owner"]["name"]
invadeName = mwo_api["#{id}"]["invading"]["name"]
ownerName = mwo_api["#{id}"]["unit"]["name"]
puts "#{planetName}: #{id} ---- Owner: #{ownerName}"
puts "Currently invaded by: #{invadeName}" unless invadeName == "None"
planetNumber += 1
end # of AW if
end # of PLANETIDS iteration
puts "Total Number of AW Owned planets: #{planetNumber}"
planetNumber = 0
@PLANETIDS.each do |id|
if mwo_api["#{id}"]["invading"]["name"] == "AW"
planetName = mwo_api["#{id}"]["name"]
houseName = mwo_api["#{id}"]["owner"]["name"]
invadeName = mwo_api["#{id}"]["invading"]["name"]
ownerName = mwo_api["#{id}"]["unit"]["name"]
puts "#{planetName}: #{id} ---- Owner: #{ownerName}"
puts "Currently invaded by: #{invadeName}" unless invadeName == "None"
planetNumber += 1
end # of AW if
end # of PLANETIDS iteration
puts "Total number of planets being invaded by AW: #{planetNumber}"
end # of aw_report
def main
mwo_api = getJSON
#self.davion_report(mwo_api)
self.aw_report(mwo_api)
end # of main
end # of Api class
end # of Mechwarrior module
apiRun = Mechwarrior::Api.new
apiRun.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment