From 263fc5d29eadcf94b2a9585103d37aff58c1d2d7 Mon Sep 17 00:00:00 2001 From: Richard Bronkhorst Date: Tue, 13 Jun 2023 16:33:22 +0200 Subject: [PATCH] Update analyzer.py, commander.py and one other file --- nullptr/analyzer.py | 20 ++++++++++++++++++++ nullptr/commander.py | 6 ++++++ nullptr/models/system.py | 5 ++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/nullptr/analyzer.py b/nullptr/analyzer.py index 3e951e8..8070f4a 100644 --- a/nullptr/analyzer.py +++ b/nullptr/analyzer.py @@ -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 diff --git a/nullptr/commander.py b/nullptr/commander.py index bbe74d5..de6bed2 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -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) diff --git a/nullptr/models/system.py b/nullptr/models/system.py index 98f6b55..0dab3d6 100644 --- a/nullptr/models/system.py +++ b/nullptr/models/system.py @@ -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)