Update analyzer.py and commander.py

This commit is contained in:
Richard Bronkhorst 2023-06-21 09:32:31 +02:00
parent b7d3347fac
commit b19e3ed2b2
2 changed files with 21 additions and 6 deletions

View File

@ -1,6 +1,7 @@
from nullptr.models.marketplace import Marketplace
from nullptr.models.jumpgate import Jumpgate
from nullptr.models.system import System
from nullptr.models.waypoint import Waypoint
from dataclasses import dataclass
@dataclass
@ -29,18 +30,25 @@ class Analyzer:
def find_markets(self, resource, sellbuy):
for m in self.store.all(Marketplace):
resources = m.imports if sellbuy == 'sell' else m.exports
if sellbuy == 'sell':
resources = m.imports
elif sellbuy == 'buy':
resources = m.exports
elif sellbuy == 'exchange':
resources = m.exchange
if resource in resources:
yield m
def find_closest_market(self, resource, sellbuy, location):
if type(location) == str:
location = self.store.get(System, location)
location = self.store.get(Waypoint, location)
market = None
distance = None
for m in self.find_markets(resource, sellbuy):
mkts = self.find_markets(resource, sellbuy)
for m in mkts:
system = self.store.get(System, m.system())
p = self.find_path(location, system)
origin = self.store.get(System, location.system())
p = self.find_path(origin, system)
if p is None: continue
if distance is None or len(p) < distance:
market = m

View File

@ -137,9 +137,14 @@ class Commander(CommandLine):
resource = delivery['trade_symbol']
destination = delivery['destination']
m = self.analyzer.find_closest_market(resource, 'buy', destination)
if m is None:
m = self.analyzer.find_closest_market(resource, 'exchange', destination)
if m is None:
print('no market found')
return
site = self.store.get(Waypoint, m.symbol)
self.set_mission('mine')
self.centcom.set_mission_param(self.ship, 'site', site)
self.centcom.set_mission_param(self.ship, 'site', site.symbol)
self.centcom.set_mission_param(self.ship, 'resource', resource)
self.centcom.set_mission_param(self.ship, 'destination', destination)
self.centcom.set_mission_param(self.ship, 'contract', contract.symbol)
@ -333,7 +338,9 @@ class Commander(CommandLine):
def do_shipyard(self):
if not self.has_ship(): return
location = self.ship.location()
pprint(self.api.shipyard(location))
data = self.api.shipyard(location)
for s in must_get(data, 'ships'):
print(s['type'], s['purchasePrice'])
def do_jump(self, system_str):
if not self.has_ship(): return