Commander cleanup
First impl of ship logs
Ship display improved
Store debugged
This commit is contained in:
Richard
2024-01-09 20:07:27 +01:00
parent 2181583843
commit 237dcc8c14
11 changed files with 562 additions and 374 deletions

View File

@@ -22,13 +22,20 @@ class Base:
identifier = 'symbol'
def __init__(self, symbol, store):
self.disable_dirty = True
self.file_offset = None
self._disable_dirty = True
self._file_offset = None
self.store = store
self.symbol = symbol
self.define()
self.disable_dirty = False
self._disable_dirty = False
def __setstate__(self, d):
self.__init__(d['symbol'], d['store'])
self.__dict__.update(d)
def __getstate__(self):
return {k:v for k,v in self.__dict__.items() if not k.startswith('_')}
@classmethod
def ext(cls):
raise NotImplementedError('no ext')
@@ -73,7 +80,7 @@ class Base:
setattr(self, attr, lst)
def __setattr__(self, name, value):
if name not in ['symbol','store','disable_dirty', 'file_offset'] and not self.disable_dirty:
if not name.startswith('_') and not self._disable_dirty:
self.store.dirty(self)
if issubclass(type(value), Base):
value = Reference.create(value)
@@ -91,11 +98,6 @@ class Base:
def is_expired(self):
return False
def load(self, d):
self.disable_dirty = True
self.__dict__.update(d)
self.disable_dirty = False
def type(self):
return self.__class__.__name__

View File

@@ -2,6 +2,7 @@ from .base import Base
from time import time
from nullptr.util import *
from nullptr.models import Waypoint
import os
class Ship(Base):
def define(self):
@@ -18,7 +19,19 @@ class Ship(Base):
self.mission:str = None
self.mission_status:str = 'init'
self.role = None
self.frame = ''
self.speed = "CRUISE"
self._log_file = None
def log(self, m):
if self._log_file is None:
fn = os.path.join(self.store.data_dir, f'{self.symbol}.{self.ext()}.log')
self._log_file = open(fn, 'a')
ts = int(time())
m = m.strip()
self._log_file.write(f'{ts} {m}\n')
self._log_file.flush()
@classmethod
def ext(self):
return 'shp'
@@ -30,6 +43,8 @@ class Ship(Base):
def update(self, d):
self.seta('status', d, 'nav.status')
self.seta('speed', d, "nav.flightMode")
self.seta('frame', d, 'frame.name')
getter = self.store.getter(Waypoint, create=True)
self.seta('location', d, 'nav.waypointSymbol', interp=getter)
self.seta('cargo_capacity', d, 'cargo.capacity')
@@ -104,16 +119,45 @@ class Ship(Base):
self.update_timers()
arrival = int(self.arrival - time())
cooldown = int(self.cooldown - time())
r = self.symbol
if detail > 1:
if self.role is not None:
r += f' {self.role}'
r += ' ' + self.status
r += f' [{self.fuel_current}/{self.fuel_capacity}]'
r += ' ' + str(self.location)
role = self.role
if role is None:
role = 'none'
mstatus = self.mission_status
if mstatus == 'error':
mstatus = mstatus.upper()
status = self.status.lower()
if status.startswith('in_'):
status = status[3:]
if detail < 2:
r = self.symbol
elif detail == 2:
symbol = self.symbol.split('-')[1]
r = f'{symbol:<2} {role:7} {mstatus:8} {str(self.location):11}'
if self.is_travelling():
r += f' [A: {arrival}]'
if self.is_cooldown():
r += f' [C: {cooldown}]'
else:
r = f'== {self.symbol} {self.frame} ==\n'
r += f'Role: {role}\n'
r += f'Mission: {self.mission} ({mstatus})\n'
for k, v in self.mission_state.items():
r += f' {k}: {v}\n'
adj = 'to' if self.status == 'IN_TRANSIT' else 'at'
r += f'Status {self.status} {adj} {self.location}\n'
r += f'Fuel: {self.fuel_current}/{self.fuel_capacity}\n'
r += f'Speed: {self.speed}\n'
r += f'Cargo: {self.cargo_units}/{self.cargo_capacity}\n'
for res, u in self.cargo.items():
r += f' {res}: {u}\n'
if self.is_travelling():
r += f'Arrival: {arrival} seconds\n'
if self.is_cooldown():
r += f'Cooldown: {cooldown} seconds \n'
return r

View File

@@ -32,3 +32,26 @@ class Waypoint(Base):
def ext(self):
return 'way'
def traits(self):
traits = []
if self.type == 'JUMP_GATE':
traits.append('JUMP')
if self.type == 'GAS_GIANT':
traits.append('GAS')
if 'SHIPYARD' in self.traits:
traits.append('SHIPYARD')
if 'MARKETPLACE' in self.traits:
traits.append('MARKET')
if self.type == 'ASTEROID':
if 'COMMON_METAL_DEPOSITS' in self.traits:
traits.append('METAL')
if 'PRECIOUS_METAL_DEPOSITS' in self.traits:
traits.append('GOLD')
if 'MINERAL_DEPOSITS' in self.traits:
traits.append('MINS')
if 'STRIPPED' in self.traits:
traits.append('STRIPPED')
return traits