Update analyzer.py, commander.py and one other file

This commit is contained in:
Richard Bronkhorst 2023-06-13 16:33:22 +02:00
parent 7385362d4f
commit 263fc5d29e
3 changed files with 30 additions and 1 deletions

View File

@ -1,4 +1,6 @@
from nullptr.models.marketplace import Marketplace from nullptr.models.marketplace import Marketplace
from nullptr.models.jumpgate import Jumpgate
from nullptr.models.system import System
class Analyzer: class Analyzer:
def __init__(self, store): def __init__(self, store):
@ -9,3 +11,21 @@ class Analyzer:
resources = m.imports if sellbuy == 'sell' else m.exports resources = m.imports if sellbuy == 'sell' else m.exports
if resource in resources: if resource in resources:
yield m 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

View File

@ -83,3 +83,9 @@ class Commander(CommandLine):
print('Found markets:') print('Found markets:')
for m in self.analyzer.find_markets(resource, sellbuy, location): for m in self.analyzer.find_markets(resource, sellbuy, location):
print(m) 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)

View File

@ -1,6 +1,6 @@
from .base import Base from .base import Base
from math import sqrt
class System(Base): class System(Base):
x:int = 0 x:int = 0
@ -19,3 +19,6 @@ class System(Base):
def path(self): def path(self):
sector, symbol = self.symbol.split('-') sector, symbol = self.symbol.split('-')
return f'atlas/{sector}/{symbol[0:1]}/{self.symbol}.{self.ext()}' 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)