Skip to content

Instantly share code, notes, and snippets.

@kreeger
Created February 28, 2015 18:02
Show Gist options
  • Save kreeger/a7b70c12f64be35f4715 to your computer and use it in GitHub Desktop.
Save kreeger/a7b70c12f64be35f4715 to your computer and use it in GitHub Desktop.
A wrapper script for tradedangerous.
#!/usr/bin/env ruby
# A wrapper around Trade Dangerous that persists settings in a local JSON file.
require 'json'
class Trader
attr_reader :data
def initialize(data:)
@data = data
fail "Script does not exist at #{location}." unless File.exists?(location)
end
def calculate!
arguments = keys_and_args.each_with_object([]) do |(key, arg), array|
value = data[key.to_s]
next if value.nil?
value = value.map { |v| %Q("#{v}") }.join(',') if key == :avoid
array << "--#{arg} #{value}"
end
puts "Calculating trade run...\n\n"
value = execute('run -vvv', arguments.join(' '))
end
def update!
execute 'import', '--plug=maddavo'
end
private
def execute(command, arguments)
system("python #{location} #{command} #{arguments}")
end
def keys_and_args
{
from: 'from',
credits: 'credits',
insurance: 'insurance',
capacity: 'capacity',
empty_ly: 'empty-ly',
ly_per: 'ly-per',
ls_max: 'ls-max',
avoid: 'avoid',
hops: 'hops',
jumps: 'jumps',
start_jumps: 'start-jumps'
}
end
def location
File.expand_path(File.join('~', 'Code', 'bitbucket', 'kfsone',
'tradedangerous', 'trade.py'))
end
end
class Persister
attr_reader :data
def initialize
@data = File.exists?(location) ? JSON.parse(IO.read(location)) : {}
end
def for_key(key)
data[key.to_s]
end
def set_value(value, for_key:)
return if value.nil? || value == ""
if for_key == :avoid && value.is_a?(String)
value = value.split(",").map(&:strip)
end
data[for_key.to_s] = value
end
def clear_value_for_key(key)
data[key.to_s] = nil
end
def persist!
FileUtils.mkdir_p(File.dirname(location))
File.open(location, 'w') { |f| f.write(JSON.pretty_generate(data)) }
end
private
def location
File.expand_path(File.join('~', '.tradedangerous', 'trade-settings.json'))
end
end
class Asker
attr_reader :settings
def initialize(settings: Persister.new)
@settings = settings
end
def ask_and_persist!
ask!
settings.persist!
end
def ask!
questions.each do |key, question|
to_ask = question
to_ask += " (Previous: #{settings.for_key(key)})" if settings.for_key(key)
puts to_ask
value = gets.chomp
settings.set_value(value, for_key: key)
end
end
private
def questions
{
from: 'Where are you now?',
credits: 'How much money do you have?',
insurance: 'Insurance amount?',
capacity: 'Cargo capacity?',
empty_ly: 'Max distance per jump?',
ly_per: 'Max distance per jump, laden?',
ls_max: 'Max distance per station from star?',
avoid: 'Anything to avoid?',
hops: 'How many hops?',
jumps: 'How many jumps per hop?',
start_jumps: 'How many jumps out to start?'
}
end
end
is_quick = ARGV[0] && ARGV[0] == "quick"
settings = Persister.new
if !is_quick
asker = Asker.new(settings: settings)
asker.ask_and_persist!
end
trader = Trader.new(data: settings.data)
trader.calculate!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment