
# eve.homeland
# 
# generates a svg printable map with current-ish kill counts (this hour only, ugh)
#
# $ bundle install
# $ ruby ./kills.rb
#
# todo: 
# - get access to the corp api and pull their kill list instead
# - get a big list of user api keys and query all kills for corp members
# - add a db of bad locations based on intel
#   - make map clickable (both: gates and systems) to communicate system safety
#   - track individual actors in the system by name so we can plot their velocity and
#     have other playes be ready to fuck them up
#     since we have relatively few access points to barl, we can probably
#     use this to blap any intruders by having scouts watch them on the way in
#     and a big fat gun 2 stations down the line.
# - cache api calls
#
require 'rubygems'
require 'faraday'
require 'faraday_middleware'
require 'erubis'

# load our special systems list. this should be in a sqlite db or something
# along with [x,y] coords on the svg page.
watched_systems = File.open('watched.txt').read.split("\n").map { |line| line.split(/\s/)[0] }

# load (truncated) master list of systems from the web somewhere and get IDs
systems = {}
lines =  File.open('systems.txt').read
lines.split("\n").each do |line|
	id, name = line.split(" ")
	if watched_systems.include?(name)
		systems[id] = name
	end
end

# setup api reader
xml = "https://api.eveonline.com"
conn = Faraday.new(url: xml) do |f|
	f.response :xml, :content_type => /\bxml/
	f.adapter Faraday.default_adapter
end

# read kill list into something we can use
# first convert our watched systems list and give it some defaults
kill_list = {}
systems.each {|id, name| kill_list[name] = {} }

kills = conn.get "/map/Kills.xml.aspx"
kills.body["eveapi"]["result"]["rowset"]["row"].each do |kill|
	# is this one of our systems? if not, ignore
	system_name = systems[kill['solarSystemID']]
	if system_name
		kill_list[system_name] = kill
	end
end

# debug
kill_list.each do |system_id, kl|
	puts "#{system_id} - #{kl.inspect}"
end

puts "----"

svg = Erubis::Eruby.load_file('BNI-map-v4.svg.erb').result({:kills => kill_list})
File.open('output.svg', 'w') { |f| f.write svg }

puts "Success."