Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Last active August 29, 2015 13:56
Show Gist options
  • Save punmechanic/8812610 to your computer and use it in GitHub Desktop.
Save punmechanic/8812610 to your computer and use it in GitHub Desktop.
require 'date'
require 'net/http'
# Get arguments from command line
if ARGV.count < 4
puts 'tribalwars world x y radius'
return
end
world = ARGV[0]
x = ARGV[1]
y = ARGV[2]
radius = ARGV[3]
files = {
:village => 'village.txt',
# Tribe file is literally the same as the player file
#:tribes => 'tribe.txt',
:players => 'player.txt'
}
puts "World #{world} (#{x}, #{y}) with radius of #{radius} tiles"
# Check if the folder for our world exists
unless File.exists? "world/#{world}" and File.directory? "world/#{world}"
# Make the directory
puts "Creating /world/#{world}"
Dir.mkdir "world/#{world}"
end
# Reads the request and returns in an inmemory format
def read_http_stream(result)
matches = /^(\d*),(\.*),(\d*),(\d*),(\d*),(\d*),(\d*)/.match result
puts matches
end
world_directory = Dir.open "world/#{world}"
database = nil
# Get the latest file in this directory
most_recent = Dir.glob("world/#{world}/*").max_by { |f| File.mtime(f) }
if most_recent == nil or (Time.now - most_recent) > 24
puts 'Last update was over 24 hours ago (or never) - downloading data for today'
# Get the request from the server
uri = "http://#{world}.tribalwars.net/map/"
files.each do |symbol, fname|
file_uri = URI "#{uri}#{fname}"
puts "Downloading #{file_uri}.."
result = Net::HTTP.get file_uri
# Now that we have the result, we want to
# parse it into memory, then dump it out in YAML format
database = read_http_stream result
destination_file_path = "#{Dir.pwd}/#{world_directory.path}/#{Time.now.strftime('%Y%m%d%H%M%S')}_#{fname}"
dest = File.new(destination_file_path, File::CREAT|File::WRONLY)
# Write to the file
dest.write database
dest.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment