Update analyzer.py, commander.py and one other file
This commit is contained in:
parent
7385362d4f
commit
263fc5d29e
@ -1,4 +1,6 @@
|
||||
from nullptr.models.marketplace import Marketplace
|
||||
from nullptr.models.jumpgate import Jumpgate
|
||||
from nullptr.models.system import System
|
||||
|
||||
class Analyzer:
|
||||
def __init__(self, store):
|
||||
@ -9,3 +11,21 @@ class Analyzer:
|
||||
resources = m.imports if sellbuy == 'sell' else m.exports
|
||||
if resource in resources:
|
||||
yield m
|
||||
|
||||
def find_path(self, orig, to):
|
||||
if orig == to: return []
|
||||
jg = self.store.get(Jumpgate, orig.symbol)
|
||||
if jg is None: return None
|
||||
best_distance = orig.distance(to)
|
||||
best_hop = None
|
||||
for hop_str in jg.systems:
|
||||
hop = self.store.get(System, hop_str)
|
||||
if hop is None: continue
|
||||
dist = hop.distance(to)
|
||||
if dist < best_distance:
|
||||
best_distance = dist
|
||||
best_hop = hop
|
||||
if best_hop is None: return None
|
||||
tail = self.find_path(best_hop, to)
|
||||
if tail is None: return None
|
||||
return [best_hop] + tail
|
||||
|
@ -83,3 +83,9 @@ class Commander(CommandLine):
|
||||
print('Found markets:')
|
||||
for m in self.analyzer.find_markets(resource, sellbuy, location):
|
||||
print(m)
|
||||
|
||||
def do_path(self):
|
||||
orig = self.ask_obj(System, 'from: ')
|
||||
dest = self.ask_obj(System, 'to: ')
|
||||
path = self.analyzer.find_path(orig, dest)
|
||||
pprint(path)
|
||||
|
@ -1,6 +1,6 @@
|
||||
|
||||
from .base import Base
|
||||
|
||||
from math import sqrt
|
||||
|
||||
class System(Base):
|
||||
x:int = 0
|
||||
@ -19,3 +19,6 @@ class System(Base):
|
||||
def path(self):
|
||||
sector, symbol = self.symbol.split('-')
|
||||
return f'atlas/{sector}/{symbol[0:1]}/{self.symbol}.{self.ext()}'
|
||||
|
||||
def distance(self, other):
|
||||
return sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
|
||||
|
Loading…
Reference in New Issue
Block a user