Update analyzer.py, commander.py and six other files
This commit is contained in:
parent
269b5cf537
commit
97296e1859
@ -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)))
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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' ]
|
||||
|
@ -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()
|
||||
|
@ -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')
|
||||
|
@ -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
|
||||
@ -22,16 +22,14 @@ class Ship(Base):
|
||||
def ext(self):
|
||||
return 'shp'
|
||||
|
||||
def location(self):
|
||||
return self.store.get('Waypoint', self.location_str)
|
||||
|
||||
def path(self):
|
||||
agent = self.symbol.split('-')[0]
|
||||
return f'{agent}/{self.symbol}.{self.ext()}'
|
||||
|
||||
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():
|
||||
|
@ -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('<QQ', d)
|
||||
o.size = d & 0x7fffffffffffffff
|
||||
o.in_use = d & 0x8000000000000000 != 0
|
||||
@ -97,7 +99,10 @@ class Store:
|
||||
self.fil.seek(0)
|
||||
offset = 0
|
||||
while (hdr := ChunkHeader.parse(self.fil)):
|
||||
if not hdr.in_use: continue
|
||||
print(hdr)
|
||||
if not hdr.in_use:
|
||||
self.fil.seek(hdr.size, 1)
|
||||
continue
|
||||
data = self.fil.read(hdr.used)
|
||||
self.load_object(data, offset)
|
||||
self.fil.seek(hdr.size - hdr.used, 1)
|
||||
@ -118,6 +123,7 @@ class Store:
|
||||
h = ChunkHeader()
|
||||
h.size = sz
|
||||
h.used = used
|
||||
h.offset = self.fil.tell()
|
||||
h.write(self.fil)
|
||||
return offset, h
|
||||
|
||||
@ -125,7 +131,7 @@ class Store:
|
||||
data = self.dump_object(obj)
|
||||
osize = len(data)
|
||||
# is there an existing chunk for this obj?
|
||||
if obj.file_offset > 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:
|
||||
|
Loading…
Reference in New Issue
Block a user