0ptr/nullptr/analyzer.py
2023-06-13 16:33:22 +02:00

32 lines
979 B
Python

from nullptr.models.marketplace import Marketplace
from nullptr.models.jumpgate import Jumpgate
from nullptr.models.system import System
class Analyzer:
def __init__(self, store):
self.store = store
def find_markets(self, resource, sellbuy, location):
for m in self.store.all(Marketplace):
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