Update analyzer.py, base.py and five other files
This commit is contained in:
parent
263fc5d29e
commit
f0c7568d38
@ -11,21 +11,31 @@ 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 get_jumpgate(self, system):
|
||||||
|
gates = self.store.all_members(system, Jumpgate)
|
||||||
|
return next(gates, None)
|
||||||
|
|
||||||
def find_path(self, orig, to):
|
def find_path(self, orig, to):
|
||||||
if orig == to: return []
|
if orig == to: return []
|
||||||
jg = self.store.get(Jumpgate, orig.symbol)
|
jg = self.get_jumpgate(orig)
|
||||||
if jg is None: return None
|
if jg is None:
|
||||||
|
print(f'{orig} no jg')
|
||||||
|
return None
|
||||||
best_distance = orig.distance(to)
|
best_distance = orig.distance(to)
|
||||||
best_hop = None
|
best_hop = None
|
||||||
for hop_str in jg.systems:
|
for hop_str in jg.systems:
|
||||||
hop = self.store.get(System, hop_str)
|
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)
|
dist = hop.distance(to)
|
||||||
if dist < best_distance:
|
if dist < best_distance:
|
||||||
best_distance = dist
|
best_distance = dist
|
||||||
best_hop = hop
|
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)
|
tail = self.find_path(best_hop, to)
|
||||||
if tail is None: return None
|
if tail is None: return None
|
||||||
return [best_hop] + tail
|
return [best_hop] + tail
|
||||||
|
@ -38,7 +38,7 @@ class Base:
|
|||||||
def dict(self):
|
def dict(self):
|
||||||
r = {}
|
r = {}
|
||||||
for k,v in self.__dict__.items():
|
for k,v in self.__dict__.items():
|
||||||
if k == 'store':
|
if k in ['store']:
|
||||||
continue
|
continue
|
||||||
r[k] = deepcopy(v)
|
r[k] = deepcopy(v)
|
||||||
return r
|
return r
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from .base import Base
|
from .system_member import SystemMember
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
class Jumpgate(Base):
|
class Jumpgate(SystemMember):
|
||||||
range: int
|
range: int
|
||||||
faction: str
|
faction: str
|
||||||
systems:List[str] = []
|
systems:List[str] = []
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
from .base import Base
|
from .system_member import SystemMember
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
class Marketplace(Base):
|
class Marketplace(SystemMember):
|
||||||
imports:List[str] = []
|
imports:List[str] = []
|
||||||
exports:List[str] = []
|
exports:List[str] = []
|
||||||
exchange:List[str] = []
|
exchange:List[str] = []
|
||||||
|
10
nullptr/models/system_member.py
Normal file
10
nullptr/models/system_member.py
Normal 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]}'
|
@ -1,8 +1,8 @@
|
|||||||
from .base import Base
|
from .system_member import SystemMember
|
||||||
from nullptr.util import *
|
from nullptr.util import *
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
class Waypoint(Base):
|
class Waypoint(SystemMember):
|
||||||
x:int = 0
|
x:int = 0
|
||||||
y:int = 0
|
y:int = 0
|
||||||
type:str = 'unknown'
|
type:str = 'unknown'
|
||||||
@ -23,7 +23,3 @@ class Waypoint(Base):
|
|||||||
def path(self):
|
def path(self):
|
||||||
sector, system, _ = self.symbol.split('-')
|
sector, system, _ = self.symbol.split('-')
|
||||||
return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}'
|
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]}'
|
|
||||||
|
@ -4,6 +4,7 @@ from nullptr.models.sector import Sector
|
|||||||
from nullptr.models.system import System
|
from nullptr.models.system import System
|
||||||
from nullptr.models.agent import Agent
|
from nullptr.models.agent import Agent
|
||||||
from nullptr.models.marketplace import Marketplace
|
from nullptr.models.marketplace import Marketplace
|
||||||
|
from nullptr.models.system_member import SystemMember
|
||||||
from nullptr.models.jumpgate import Jumpgate
|
from nullptr.models.jumpgate import Jumpgate
|
||||||
from os.path import isfile, dirname, isdir
|
from os.path import isfile, dirname, isdir
|
||||||
import os
|
import os
|
||||||
@ -17,10 +18,11 @@ class Store:
|
|||||||
self.init_models()
|
self.init_models()
|
||||||
self.data_dir = data_dir
|
self.data_dir = data_dir
|
||||||
self.data = {m: {} for m in self.models}
|
self.data = {m: {} for m in self.models}
|
||||||
|
self.system_members = {}
|
||||||
self.dirty_objects = set()
|
self.dirty_objects = set()
|
||||||
|
|
||||||
def init_models(self):
|
def init_models(self):
|
||||||
self.models = Base.__subclasses__()
|
self.models = all_subclasses(Base)
|
||||||
self.extensions = {c.ext(): c for c in self.models}
|
self.extensions = {c.ext(): c for c in self.models}
|
||||||
|
|
||||||
def dirty(self, obj):
|
def dirty(self, obj):
|
||||||
@ -41,10 +43,9 @@ class Store:
|
|||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
typ = self.extensions[ext]
|
typ = self.extensions[ext]
|
||||||
obj = typ(symbol, self)
|
obj = self.create(typ, symbol)
|
||||||
data['store'] = self
|
data['store'] = self
|
||||||
obj.__dict__ = data
|
obj.__dict__ = data
|
||||||
self.data[typ][obj.symbol] = obj
|
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
@ -68,6 +69,11 @@ class Store:
|
|||||||
def create(self, typ, symbol):
|
def create(self, typ, symbol):
|
||||||
obj = typ(symbol, self)
|
obj = typ(symbol, self)
|
||||||
self.data[typ][symbol] = obj
|
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
|
return obj
|
||||||
|
|
||||||
def get(self, typ, symbol, create=False):
|
def get(self, typ, symbol, create=False):
|
||||||
@ -92,6 +98,13 @@ class Store:
|
|||||||
for m in self.data[typ].values():
|
for m in self.data[typ].values():
|
||||||
yield m
|
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):
|
def flush(self):
|
||||||
it = 0
|
it = 0
|
||||||
start_time = time()
|
start_time = time()
|
||||||
|
Loading…
Reference in New Issue
Block a user