Compare commits
2 Commits
81dce33c45
...
522ec53d88
Author | SHA1 | Date |
---|---|---|
Arthur POULET | 522ec53d88 | |
Arthur POULET | 23367f8f68 |
36
bin/console
36
bin/console
|
@ -7,17 +7,41 @@ Dotenv.load!
|
|||
$LOAD_PATH << File.join(Dir.pwd, 'lib')
|
||||
|
||||
require 'pry'
|
||||
require 'space_traders'
|
||||
include SpaceTraders
|
||||
|
||||
def restart!
|
||||
load 'lib/space_traders.rb'
|
||||
include SpaceTraders
|
||||
end
|
||||
restart!
|
||||
|
||||
c = Client.load_from_env
|
||||
# c = Client.register name: "Sceptique#{(rand * 10000).round}"
|
||||
# File.open(".env", "a") { |f| f.write("\nSPACETRADERS_TOKEN=#{c.token}\n") }
|
||||
Log.debug "client loaded"
|
||||
|
||||
contracts = Contract.my_contracts(c)
|
||||
contracts.first.accept!
|
||||
contracts.first.reload!
|
||||
ship = Ship.my_ships(c).first.reload!
|
||||
contract = Contract.my_contracts(c).first
|
||||
contract.accept!
|
||||
contract.reload!
|
||||
ship = Ship.my_ships(c).first
|
||||
system = System.systems(c).first
|
||||
waypoint = system.waypoints.first
|
||||
|
||||
# copper ore
|
||||
target_delivery = contract.terms["deliver"][0]["tradeSymbol"]
|
||||
destination1 = ship.helper.waypoints.find { |wp| wp.traits.find { |trait| trait["symbol"] == "COMMON_METAL_DEPOSITS" } }.symbol
|
||||
destination2 = contract.terms["deliver"].first["destinationSymbol"]
|
||||
|
||||
ship.navigate! waypoint: destination1
|
||||
def wait_until(t)
|
||||
s = t.to_i - Time.now.to_i + 1.second
|
||||
Log.debug "sleeping until #{t} (#{s} seconds)"
|
||||
sleep(s) if s > 0
|
||||
end
|
||||
wait_until Time.parse(ship.nav.dig("route", "arrival"))
|
||||
|
||||
ship.survey!
|
||||
survey = c.data["surveys"].find { |s| s["deposits"].find { |d| d["symbol"] == target_delivery } }
|
||||
|
||||
ship.extract! **survey.to_h{|k,v| [k.to_sym, v]}
|
||||
|
||||
binding.pry
|
||||
|
|
|
@ -53,7 +53,7 @@ module SpaceTraders
|
|||
end
|
||||
|
||||
def post(endpoint:, body:)
|
||||
Log.debug("post #{endpoint}")
|
||||
Log.debug("post #{endpoint} with body #{body}")
|
||||
res = Net::HTTP.post(
|
||||
URI("#{base_url}/#{endpoint}"),
|
||||
body ? body.to_json : nil,
|
||||
|
@ -93,7 +93,8 @@ module SpaceTraders
|
|||
Log.debug("add_endpoint #{name}")
|
||||
define_method(name.to_s.downcase) do
|
||||
variable_name = "@memoize_#{name}"
|
||||
variable = instance_variable_get(variable_name)
|
||||
variable = nil
|
||||
# variable = instance_variable_get(variable_name)
|
||||
if variable.nil?
|
||||
endpoint = SpaceTraders::Endpoint::const_get(name.to_s.camelcase).new(client: self)
|
||||
instance_variable_set(variable_name, endpoint)
|
||||
|
@ -108,6 +109,6 @@ module SpaceTraders
|
|||
names.each { add_endpoint(_1) }
|
||||
end
|
||||
|
||||
add_endpoints :agents, :contracts, :ships
|
||||
add_endpoints :agents, :contracts, :ships, :systems
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,15 +12,17 @@ class SpaceTraders::Endpoint
|
|||
delegate :get, to: :client
|
||||
delegate :post, to: :client
|
||||
|
||||
def self.fills_endpoint_placeholders(raw_endpoint, params)
|
||||
def self.fills_endpoint_placeholders(raw_endpoint, params, query: false)
|
||||
Log.debug("raw_endpoint #{raw_endpoint}")
|
||||
mutated = params.reduce(raw_endpoint) do |base, kv|
|
||||
Log.debug("endpoint mutate #{base}")
|
||||
if base.match?(/:#{kv[0]}\b/)
|
||||
base.gsub(/:#{kv[0]}\b/, kv[1])
|
||||
else
|
||||
elsif query
|
||||
join = base.include?("?") ? "&" : "?"
|
||||
"#{base}#{join}#{kv[0]}=#{kv[1]}"
|
||||
else
|
||||
base
|
||||
end
|
||||
end
|
||||
Log.debug("endpoint mutate to #{mutated}")
|
||||
|
@ -31,7 +33,7 @@ class SpaceTraders::Endpoint
|
|||
SpaceTraders::Log.debug("define endpoint #{method_name} to #{get || post}")
|
||||
if get
|
||||
define_method(method_name) do |params={}, &block|
|
||||
endpoint = SpaceTraders::Endpoint.fills_endpoint_placeholders(get || post, params)
|
||||
endpoint = SpaceTraders::Endpoint.fills_endpoint_placeholders(get || post, params, query: true)
|
||||
SpaceTraders::Log.debug("filled_endpoint of #{method_name} to #{endpoint} with #{get || post} and #{params}")
|
||||
if block
|
||||
get(endpoint: endpoint, &block)
|
||||
|
@ -42,7 +44,7 @@ class SpaceTraders::Endpoint
|
|||
end
|
||||
elsif post
|
||||
define_method(method_name) do |params={}, body=nil, &block|
|
||||
endpoint = SpaceTraders::Endpoint.fills_endpoint_placeholders(get || post, params)
|
||||
endpoint = SpaceTraders::Endpoint.fills_endpoint_placeholders(get || post, params, query: false)
|
||||
SpaceTraders::Log.debug("filled_endpoint of #{method_name} to #{endpoint} with #{get || post} and #{params}")
|
||||
if block
|
||||
post(endpoint: endpoint, body: body, &block)
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SpaceTraders::Endpoint::Systems < SpaceTraders::Endpoint
|
||||
define_endpoint :systems, get: 'systems'
|
||||
define_endpoint :system, get: 'systems/:id'
|
||||
define_endpoint :waypoints, get: 'systems/:id/waypoints'
|
||||
define_endpoint :waypoint, get: 'systems/:system/waypoints/:waypoint'
|
||||
# define_endpoint :market, get: 'systems/:system/waypoints/:waypoint/market'
|
||||
# define_endpoint :shipyard, get: 'systems/:system/waypoints/:waypoint/shipyard'
|
||||
# define_endpoint :jump_gate, get: 'systems/:system/waypoints/:waypoint/jump-gate'
|
||||
# define_endpoint :construction, get: 'systems/:system/waypoints/:waypoint/construction'
|
||||
# define_endpoint :construction_supply, get: 'systems/:system/waypoints/:waypoint/construction_supply'
|
||||
end
|
23
lib/model.rb
23
lib/model.rb
|
@ -19,6 +19,19 @@ class SpaceTraders::Model
|
|||
end
|
||||
end
|
||||
|
||||
# it's recursive
|
||||
def to_h
|
||||
self.attributes.to_h do |k|
|
||||
v = self[k]
|
||||
v = v.to_h if v.is_a?(SpaceTraders::Model)
|
||||
v = v.map { _1.to_h } if v.is_a?(Array) && v.first.is_a?(SpaceTraders::Model)
|
||||
[
|
||||
k,
|
||||
v,
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(client, **attrs)
|
||||
raise "Cannot use model with client parse disabled" if !client.parse?
|
||||
|
||||
|
@ -45,6 +58,16 @@ class SpaceTraders::Model
|
|||
def client
|
||||
@_client
|
||||
end
|
||||
|
||||
def update(**params)
|
||||
params = client.data if params.empty?
|
||||
|
||||
attributes.each do
|
||||
if params[_1]
|
||||
self[_1] = _2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Dir['lib/models/*.rb'].each do
|
||||
|
|
|
@ -14,19 +14,26 @@ class SpaceTraders::Ship < SpaceTraders::Model
|
|||
:cargo,
|
||||
:fuel
|
||||
|
||||
def self.my_ships(client)
|
||||
list = client.ships.my_ships
|
||||
list.map { new(client, **_1) }
|
||||
class Helper
|
||||
def initialize(ship)
|
||||
@ship = ship
|
||||
end
|
||||
|
||||
def waypoints
|
||||
Waypoint.waypoints(@ship.client, system: @ship.nav["systemSymbol"])
|
||||
end
|
||||
end
|
||||
|
||||
def update(**params)
|
||||
params = client.data if params.empty?
|
||||
attr_reader :helper
|
||||
|
||||
attributes.each do
|
||||
if params[_1]
|
||||
self[_1] = _2
|
||||
end
|
||||
end
|
||||
def initialize(client, **params)
|
||||
super
|
||||
@helper = Helper.new(self)
|
||||
end
|
||||
|
||||
def self.my_ships(client, page: 1)
|
||||
list = client.ships.my_ships(page: 1)
|
||||
list.map { new(client, **_1) }
|
||||
end
|
||||
|
||||
def reload!
|
||||
|
@ -50,7 +57,7 @@ class SpaceTraders::Ship < SpaceTraders::Model
|
|||
end
|
||||
|
||||
def extract!(signature:, symbol:, deposits:, expiration:, size:)
|
||||
client.ships.my_ship_extract(id: symbol, signature:, symbol:, deposits:, expiration:, size:)
|
||||
client.ships.my_ship_extract({ id: symbol }, { signature:, symbol:, deposits:, expiration:, size: })
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -60,17 +67,17 @@ class SpaceTraders::Ship < SpaceTraders::Model
|
|||
end
|
||||
|
||||
def jump!(waypoint:)
|
||||
client.ships.my_ship_jump(id: symbol, waypointSymbol: waypoint)
|
||||
client.ships.my_ship_jump({ id: symbol }, { waypointSymbol: waypoint })
|
||||
self
|
||||
end
|
||||
|
||||
def navigate!(waypoint:)
|
||||
client.ships.my_ship_navigate(id: symbol, waypointSymbol: waypoint)
|
||||
client.ships.my_ship_navigate({ id: symbol }, { waypointSymbol: waypoint })
|
||||
self
|
||||
end
|
||||
|
||||
def warp!(waypoint:)
|
||||
client.ships.my_ship_warp(id: symbol, waypointSymbol: waypoint)
|
||||
client.ships.my_ship_warp({ id: symbol }, { waypointSymbol: waypoint })
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -80,7 +87,7 @@ class SpaceTraders::Ship < SpaceTraders::Model
|
|||
end
|
||||
|
||||
def sell!(cargo:, unit:)
|
||||
client.ships.my_ship_warp(id: symbol, cargo:, unit:)
|
||||
client.ships.my_ship_warp({ id: symbol }, { cargo:, unit: })
|
||||
self
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SpaceTraders::System < SpaceTraders::Model
|
||||
attributes :symbol,
|
||||
:sectorSymbol,
|
||||
:type,
|
||||
:x,
|
||||
:y,
|
||||
:waypoints,
|
||||
:factions
|
||||
|
||||
def self.systems(client, page: 1)
|
||||
list = client.systems.systems(page:)
|
||||
list.map { new(client, **_1) }
|
||||
end
|
||||
|
||||
def self.load(client, id:)
|
||||
new(client, **client.systems.system(id:))
|
||||
end
|
||||
|
||||
def initialize(client, **params)
|
||||
super
|
||||
self.waypoints = self.waypoints.map { SpaceTraders::Waypoint.new(client, **_1) }
|
||||
end
|
||||
|
||||
def reload!
|
||||
update(**client.systems.system(id: symbol))
|
||||
self
|
||||
end
|
||||
end
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SpaceTraders::Waypoint < SpaceTraders::Model
|
||||
attributes :symbol,
|
||||
:type,
|
||||
:systemSymbol,
|
||||
:x,
|
||||
:y,
|
||||
:orbitals,
|
||||
:orbits,
|
||||
:faction,
|
||||
:traits,
|
||||
:modifiers,
|
||||
:chart,
|
||||
:isUnderConstruction
|
||||
|
||||
def self.waypoints(client, system:, page: 1)
|
||||
list = client.systems.waypoints(id: system, page:)
|
||||
list.map { new(client, **_1) }
|
||||
end
|
||||
|
||||
def self.load(client, id:, system:)
|
||||
new(client, _system: system, **client.systems.waypoints(system:, waypoint: id))
|
||||
end
|
||||
|
||||
def reload!
|
||||
update(**client.systems.waypoint(system: self.systemSymbol, waypoint: symbol))
|
||||
self
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue