diff --git a/nullptr/analyzer.py b/nullptr/analyzer.py index 53ad12c..03f8074 100644 --- a/nullptr/analyzer.py +++ b/nullptr/analyzer.py @@ -44,16 +44,16 @@ class Analyzer: location = self.store.get(Waypoint, location) mkts = self.find_markets(resource, sellbuy) candidates = [] - origin = self.store.get(System, location.system()) + origin = location.system for typ, m in mkts: - system = self.store.get(System, m.system()) + system = m.waypoint.system d = origin.distance(system) candidates.append((typ, m, d)) possibles = sorted(candidates, key=lambda m: m[2]) possibles = possibles[:10] results = [] for typ,m,d in possibles: - system = self.store.get(System, m.system()) + system = m.waypoint.system p = self.find_path(origin, system) if p is None: continue results.append((typ,m,d,len(p))) diff --git a/nullptr/commander.py b/nullptr/commander.py index 7b12842..d4c509c 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -121,7 +121,7 @@ class Commander(CommandLine): def do_chaul(self): if not self.has_ship(): return - if len(ship.cargo) > 0: + if len(self.ship.cargo) > 0: raise CommandError('please dump cargo first') contract = self.active_contract() delivery = contract.unfinished_delivery() diff --git a/nullptr/missions/base.py b/nullptr/missions/base.py index e509dfd..702b2b6 100644 --- a/nullptr/missions/base.py +++ b/nullptr/missions/base.py @@ -28,7 +28,10 @@ class MissionParam: elif self.cls == list: return [i.strip() for i in val.split(',')] elif issubclass(self.cls, Base): - data = store.get(self.cls, val) + if type(val) == str: + data = store.get(self.cls, val) + else: + data = val if data is None: raise ValueError('object not found') return data.symbol @@ -179,9 +182,8 @@ class BaseMission(Mission): traject = self.st('traject') if traject is None or traject == []: return 'done' - dest = self.store.get(Waypoint, traject[-1]) - loc = self.ship.location() - print(dest, loc) + dest = traject[-1] + loc = self.ship.location if dest == loc: self.sts('traject', None) return 'done' @@ -200,10 +202,10 @@ class BaseMission(Mission): def step_calculate_traject(self, dest): if type(dest) == str: dest = self.store.get(Waypoint, dest) - loc = self.ship.location() - loc_sys = self.store.get(System, loc.system()) + loc = self.ship.location + loc_sys = loc.system loc_jg = self.analyzer.get_jumpgate(loc_sys) - dest_sys = self.store.get(System, dest.system()) + dest_sys = dest.system dest_jg = self.analyzer.get_jumpgate(dest_sys) if dest_sys == loc_sys: result = [dest.symbol] @@ -212,10 +214,10 @@ class BaseMission(Mission): path = self.analyzer.find_path(loc_sys, dest_sys) result = [] if loc.symbol != loc_jg.symbol: - result.append(loc_jg.symbol) + result.append(loc_jg) result += [s.symbol for s in path[1:]] if dest_jg.symbol != dest.symbol: - result.append(dest.symbol) + result.append(dest) self.sts('traject', result) print(result) return result diff --git a/nullptr/models/__init__.py b/nullptr/models/__init__.py index f919fce..c9063dc 100644 --- a/nullptr/models/__init__.py +++ b/nullptr/models/__init__.py @@ -9,4 +9,4 @@ from nullptr.models.ship import Ship from nullptr.models.contract import Contract from nullptr.models.survey import Survey -__all__ = [ 'Waypoint', 'Sector', 'Ship', 'Survey', 'Agent', 'Marketplace', 'Jumpgate', 'Contract', 'Base' ] +__all__ = [ 'Waypoint', 'Sector', 'Ship', 'Survey', 'System', 'Agent', 'Marketplace', 'Jumpgate', 'Contract', 'Base' ] diff --git a/nullptr/models/base.py b/nullptr/models/base.py index 4efd2e5..883f690 100644 --- a/nullptr/models/base.py +++ b/nullptr/models/base.py @@ -23,7 +23,7 @@ class Base: def __init__(self, symbol, store): self.disable_dirty = True - self.file_offset = 0 + self.file_offset = None self.store = store self.symbol = symbol self.define() diff --git a/nullptr/models/marketplace.py b/nullptr/models/marketplace.py index 5187cdf..b71c9c9 100644 --- a/nullptr/models/marketplace.py +++ b/nullptr/models/marketplace.py @@ -3,6 +3,7 @@ from .base import Base from time import time from nullptr.util import * from dataclasses import field +from nullptr.models import Waypoint class Marketplace(Base): def define(self): @@ -11,6 +12,11 @@ class Marketplace(Base): self.exchange:list = [] self.prices:dict = {} self.last_prices:int = 0 + self.set_waypoint() + + def set_waypoint(self): + waypoint = self.store.get(Waypoint, self.symbol, create=True) + self.waypoint = waypoint def update(self, d): self.setlst('imports', d, 'imports', 'symbol') diff --git a/nullptr/models/ship.py b/nullptr/models/ship.py index 5172f04..2350d1b 100644 --- a/nullptr/models/ship.py +++ b/nullptr/models/ship.py @@ -1,7 +1,7 @@ from .base import Base from time import time from nullptr.util import * -from dataclasses import dataclass, field +from nullptr.models import Waypoint class Ship(Base): def define(self): @@ -10,7 +10,7 @@ class Ship(Base): self.status:str = '' self.cargo_capacity:int = 0 self.cargo_units:int = 0 - self.location_str = '' + self.location = None self.cooldown:int = 0 self.arrival:int = 0 self.fuel_current:int = 0 @@ -21,9 +21,6 @@ class Ship(Base): @classmethod def ext(self): return 'shp' - - def location(self): - return self.store.get('Waypoint', self.location_str) def path(self): agent = self.symbol.split('-')[0] @@ -31,7 +28,8 @@ class Ship(Base): def update(self, d): self.seta('status', d, 'nav.status') - self.seta('location_str', d, 'nav.waypointSymbol') + getter = self.store.getter(Waypoint, create=True) + self.seta('location', d, 'nav.waypointSymbol', interp=getter) self.seta('cargo_capacity', d, 'cargo.capacity') self.seta('cargo_units', d, 'cargo.units') self.seta('fuel_capacity', d, 'fuel.capacity') @@ -95,7 +93,7 @@ class Ship(Base): if detail > 1: r += ' ' + self.status r += f' [{self.fuel_current}/{self.fuel_capacity}]' - r += ' ' + str(self.location()) + r += ' ' + str(self.location) if self.is_travelling(): r += f' [A: {arrival}]' if self.is_cooldown(): diff --git a/nullptr/store.py b/nullptr/store.py index e581c47..2328fb9 100644 --- a/nullptr/store.py +++ b/nullptr/store.py @@ -27,17 +27,19 @@ class StoreUnpickler(pickle.Unpickler): class ChunkHeader: def __init__(self): + self.offset = 0 self.in_use = True self.size = 0 self.used = 0 @classmethod def parse(cls, fil): + offset = fil.tell() d = fil.read(16) if len(d) < 16: return None - # print(d) o = cls() + o.offset = offset d, o.used = unpack(' 0: + if obj.file_offset is not None: # read chunk hdr self.fil.seek(obj.file_offset) hdr = ChunkHeader.parse(self.fil) @@ -135,15 +141,16 @@ class Store: # free the chunk hdr.in_use = False # force a new chunk - obj.file_offset = 0 + obj.file_offset = None else: # if it is big enough, update the used field hdr.used = osize - self.fil.seek(obj.file_offset) + self.fil.seek(hdr.offset) hdr.write(self.fil) - if obj.file_offset == 0: + if obj.file_offset is None: obj.file_offset, hdr = self.allocate_chunk(osize) + print(type(obj).__name__, hdr) self.fil.write(data) slack = b'\x00' * (hdr.size - hdr.used) self.fil.write(slack) @@ -181,7 +188,7 @@ class Store: def getter(self, typ, create=False): if type(typ) == str and typ in self.model_names: typ = self.model_names[typ] - return partial(self.get, typ=typ, create=create) + return partial(self.get, typ, create=create) def update(self, typ, data, symbol=None): if type(typ) == str and typ in self.model_names: