Update analyzer.py, base.py and five other files

This commit is contained in:
Richard Bronkhorst 2023-06-13 22:29:04 +02:00
parent 263fc5d29e
commit f0c7568d38
7 changed files with 48 additions and 19 deletions

View File

@ -12,20 +12,30 @@ class Analyzer:
if resource in resources:
yield m
def get_jumpgate(self, system):
gates = self.store.all_members(system, Jumpgate)
return next(gates, None)
def find_path(self, orig, to):
if orig == to: return []
jg = self.store.get(Jumpgate, orig.symbol)
if jg is None: return None
jg = self.get_jumpgate(orig)
if jg is None:
print(f'{orig} no jg')
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
if hop is None:
print(f'{hop_str} hopnon')
continue
dist = hop.distance(to)
if dist < best_distance:
best_distance = dist
best_hop = hop
if best_hop is None: return None
if best_hop is None:
print('nobest')
return None
tail = self.find_path(best_hop, to)
if tail is None: return None
return [best_hop] + tail

View File

@ -38,7 +38,7 @@ class Base:
def dict(self):
r = {}
for k,v in self.__dict__.items():
if k == 'store':
if k in ['store']:
continue
r[k] = deepcopy(v)
return r

View File

@ -1,7 +1,7 @@
from .base import Base
from .system_member import SystemMember
from typing import List
class Jumpgate(Base):
class Jumpgate(SystemMember):
range: int
faction: str
systems:List[str] = []

View File

@ -1,8 +1,8 @@
from .base import Base
from .system_member import SystemMember
from typing import List
class Marketplace(Base):
class Marketplace(SystemMember):
imports:List[str] = []
exports:List[str] = []
exchange:List[str] = []

View File

@ -0,0 +1,10 @@
from .base import Base
class SystemMember(Base):
@classmethod
def ext(cls):
return 'obj'
def system(self):
p = self.symbol.split('-')
return f'{p[0]}-{p[1]}'

View File

@ -1,8 +1,8 @@
from .base import Base
from .system_member import SystemMember
from nullptr.util import *
from typing import List
class Waypoint(Base):
class Waypoint(SystemMember):
x:int = 0
y:int = 0
type:str = 'unknown'
@ -23,7 +23,3 @@ class Waypoint(Base):
def path(self):
sector, system, _ = self.symbol.split('-')
return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}'
def system(self):
p = self.symbol.split('-')
return f'{p[0]}-{p[1]}'

View File

@ -4,6 +4,7 @@ from nullptr.models.sector import Sector
from nullptr.models.system import System
from nullptr.models.agent import Agent
from nullptr.models.marketplace import Marketplace
from nullptr.models.system_member import SystemMember
from nullptr.models.jumpgate import Jumpgate
from os.path import isfile, dirname, isdir
import os
@ -17,10 +18,11 @@ class Store:
self.init_models()
self.data_dir = data_dir
self.data = {m: {} for m in self.models}
self.system_members = {}
self.dirty_objects = set()
def init_models(self):
self.models = Base.__subclasses__()
self.models = all_subclasses(Base)
self.extensions = {c.ext(): c for c in self.models}
def dirty(self, obj):
@ -41,10 +43,9 @@ class Store:
data = json.load(f)
typ = self.extensions[ext]
obj = typ(symbol, self)
obj = self.create(typ, symbol)
data['store'] = self
obj.__dict__ = data
self.data[typ][obj.symbol] = obj
return obj
def load(self):
@ -68,6 +69,11 @@ class Store:
def create(self, typ, symbol):
obj = typ(symbol, self)
self.data[typ][symbol] = obj
if issubclass(typ, SystemMember):
system_str = obj.system()
if system_str not in self.system_members:
self.system_members[system_str] = set()
self.system_members[system_str].add(obj)
return obj
def get(self, typ, symbol, create=False):
@ -92,6 +98,13 @@ class Store:
for m in self.data[typ].values():
yield m
def all_members(self, system, typ=None):
if type(system) == System:
system = system.symbol
for m in self.system_members[system]:
if typ is None or type(m) == typ:
yield m
def flush(self):
it = 0
start_time = time()