Update analyzer.py and commander.py
This commit is contained in:
parent
38a2ee7870
commit
27bd054e8b
@ -30,30 +30,28 @@ class Analyzer:
|
|||||||
|
|
||||||
def find_markets(self, resource, sellbuy):
|
def find_markets(self, resource, sellbuy):
|
||||||
for m in self.store.all(Marketplace):
|
for m in self.store.all(Marketplace):
|
||||||
if sellbuy == 'sell':
|
if 'sell' in sellbuy and resource in m.imports:
|
||||||
resources = m.imports
|
yield ('sell', m)
|
||||||
elif sellbuy == 'buy':
|
|
||||||
resources = m.exports
|
elif 'buy' in sellbuy and resource in m.exports:
|
||||||
elif sellbuy == 'exchange':
|
yield ('buy', m)
|
||||||
resources = m.exchange
|
|
||||||
if resource in resources:
|
elif 'exchange' in sellbuy and resource in m.exchange:
|
||||||
yield m
|
yield ('exchange', m)
|
||||||
|
|
||||||
def find_closest_market(self, resource, sellbuy, location):
|
def find_closest_markets(self, resource, sellbuy, location):
|
||||||
if type(location) == str:
|
if type(location) == str:
|
||||||
location = self.store.get(Waypoint, location)
|
location = self.store.get(Waypoint, location)
|
||||||
market = None
|
|
||||||
distance = None
|
|
||||||
mkts = self.find_markets(resource, sellbuy)
|
mkts = self.find_markets(resource, sellbuy)
|
||||||
for m in mkts:
|
candidates = []
|
||||||
|
for typ, m in mkts:
|
||||||
system = self.store.get(System, m.system())
|
system = self.store.get(System, m.system())
|
||||||
origin = self.store.get(System, location.system())
|
origin = self.store.get(System, location.system())
|
||||||
p = self.find_path(origin, system)
|
p = self.find_path(origin, system)
|
||||||
if p is None: continue
|
if p is None: continue
|
||||||
if distance is None or len(p) < distance:
|
candidates.append((typ, m, len(p)))
|
||||||
market = m
|
result = sorted(candidates, key=lambda m: m[2])
|
||||||
distance = len(p)
|
return result[:10]
|
||||||
return market
|
|
||||||
|
|
||||||
|
|
||||||
def get_jumpgate(self, system):
|
def get_jumpgate(self, system):
|
||||||
|
@ -218,15 +218,12 @@ class Commander(CommandLine):
|
|||||||
pprint(r)
|
pprint(r)
|
||||||
|
|
||||||
def do_query(self):
|
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()
|
resource = input('what resource?').upper()
|
||||||
sellbuy = self.ask_multichoice(['sell','buy','exchange'], 'do you want to sell or buy?')
|
|
||||||
print('Found markets:')
|
print('Found markets:')
|
||||||
for m in self.analyzer.find_markets(resource, sellbuy):
|
for typ, m, plen in self.analyzer.find_closest_markets(resource, 'buy,exchange',location):
|
||||||
system = self.store.get(System, m.system())
|
print(m, typ[0], f'{plen-1} hops')
|
||||||
p = self.analyzer.find_path(location, system)
|
|
||||||
if p is None: continue
|
|
||||||
print(m, f'{len(p)-1} hops')
|
|
||||||
|
|
||||||
def do_path(self):
|
def do_path(self):
|
||||||
orig = self.ask_obj(System, 'from: ')
|
orig = self.ask_obj(System, 'from: ')
|
||||||
|
Loading…
Reference in New Issue
Block a user