Update analyzer.py and commander.py

This commit is contained in:
Richard Bronkhorst 2023-06-23 13:32:08 +02:00
parent 38a2ee7870
commit 27bd054e8b
2 changed files with 19 additions and 24 deletions

View File

@ -30,30 +30,28 @@ class Analyzer:
def find_markets(self, resource, sellbuy):
for m in self.store.all(Marketplace):
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 'sell' in sellbuy and resource in m.imports:
yield ('sell', m)
elif 'buy' in sellbuy and resource in m.exports:
yield ('buy', m)
elif 'exchange' in sellbuy and resource in m.exchange:
yield ('exchange', m)
def find_closest_markets(self, resource, sellbuy, location):
if type(location) == str:
location = self.store.get(Waypoint, location)
market = None
distance = None
mkts = self.find_markets(resource, sellbuy)
for m in mkts:
candidates = []
for typ, m in mkts:
system = self.store.get(System, m.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
distance = len(p)
return market
candidates.append((typ, m, len(p)))
result = sorted(candidates, key=lambda m: m[2])
return result[:10]
def get_jumpgate(self, system):

View File

@ -218,15 +218,12 @@ class Commander(CommandLine):
pprint(r)
def do_query(self):
location = self.ask_obj(System, 'Where are you? ')
if not self.has_ship(): return
location = self.ship.location()
resource = input('what resource?').upper()
sellbuy = self.ask_multichoice(['sell','buy','exchange'], 'do you want to sell or buy?')
print('Found markets:')
for m in self.analyzer.find_markets(resource, sellbuy):
system = self.store.get(System, m.system())
p = self.analyzer.find_path(location, system)
if p is None: continue
print(m, f'{len(p)-1} hops')
for typ, m, plen in self.analyzer.find_closest_markets(resource, 'buy,exchange',location):
print(m, typ[0], f'{plen-1} hops')
def do_path(self):
orig = self.ask_obj(System, 'from: ')