trading debug and ship logs

This commit is contained in:
Richard 2024-01-16 19:13:10 +01:00
parent 560ac056ff
commit 592c628a46
6 changed files with 43 additions and 27 deletions

View File

@ -194,14 +194,11 @@ class Analyzer:
def find_deal(self, smkt, dmkt): def find_deal(self, smkt, dmkt):
best_margin = 0 best_margin = 0
best_resource = None best_resource = None
print(f'finding deal from {smkt} to {dmkt}')
for r, sp in smkt.prices.items(): for r, sp in smkt.prices.items():
if not r in dmkt.prices: if not r in dmkt.prices:
print(r, 'not in dmkt')
continue continue
dp = dmkt.prices[r] dp = dmkt.prices[r]
margin = dp.sell - sp.buy margin = dp.sell - sp.buy
print(r, margin)
if margin > best_margin: if margin > best_margin:
best_margin = margin best_margin = margin
best_resource = r best_resource = r

View File

@ -18,6 +18,7 @@ class CentralCommand:
self.analyzer = Analyzer(store) self.analyzer = Analyzer(store)
self.api = api self.api = api
self.atlas_builder = AtlasBuilder(store, api) self.atlas_builder = AtlasBuilder(store, api)
self.update_missions()
def get_ready_missions(self): def get_ready_missions(self):
result = [] result = []
@ -25,6 +26,12 @@ class CentralCommand:
if mission.is_ready(): if mission.is_ready():
result.append(ship) result.append(ship)
return result return result
def single_step(self, ship):
if ship not in self.missions:
print('ship has no mission')
mission = self.missions[ship]
mission.step()
def tick(self): def tick(self):
self.update_missions() self.update_missions()

View File

@ -54,6 +54,11 @@ class Commander(CommandLine):
def do_auto(self): def do_auto(self):
self.centcom.run_interactive() self.centcom.run_interactive()
def do_log(self, level):
if not self.has_ship(): return
self.ship._log_level = int(level)
######## Resolvers ######### ######## Resolvers #########
def ask_obj(self, typ, prompt): def ask_obj(self, typ, prompt):
obj = None obj = None
@ -108,7 +113,10 @@ class Commander(CommandLine):
if not self.has_ship(): return if not self.has_ship(): return
s = self.ship.location.system s = self.ship.location.system
w = f'{s}-{w}' w = f'{s}-{w}'
return self.store.get(Waypoint, w) r = self.store.get(Waypoint, w)
if r is None:
raise CommandError(f'{w} not found')
return r
######## First run ######### ######## First run #########
@ -223,11 +231,7 @@ class Commander(CommandLine):
######## Specials ######### ######## Specials #########
def do_market(self, arg=''): def do_market(self, arg=''):
if arg == '': waypoint = self.resolve_waypoint(arg)
if not self.has_ship(): return
waypoint = self.ship.location
else:
waypoint = self.resolve('Waypoint', arg)
r = self.api.marketplace(waypoint) r = self.api.marketplace(waypoint)
pprint(r, 3) pprint(r, 3)
@ -349,6 +353,11 @@ class Commander(CommandLine):
if not self.has_ship(): return if not self.has_ship(): return
self.centcom.restart_mission(self.ship, status) self.centcom.restart_mission(self.ship, status)
self.print_mission() self.print_mission()
def do_mstep(self):
if not self.has_ship(): return
self.centcom.single_step(self.ship)
self.print_mission()
def do_mreset(self): def do_mreset(self):
if not self.has_ship(): return if not self.has_ship(): return

View File

@ -96,7 +96,7 @@ class Mission:
} }
def step_done(self): def step_done(self):
logging.info(f'mission finished for {self.ship}') self.ship.log(f'mission finished', 3)
def is_waiting(self): def is_waiting(self):
return self.next_step > time() or self.ship.cooldown > time() or self.ship.arrival > time() return self.next_step > time() or self.ship.cooldown > time() or self.ship.arrival > time()
@ -113,7 +113,7 @@ class Mission:
self.init_state() self.init_state()
status = self.status() status = self.status()
if not status in steps: if not status in steps:
logging.warning(f"Invalid mission status {status}") self.ship.log(f"Invalid mission status {status}", 1)
self.status('error') self.status('error')
return return
handler, next_step = steps[status] handler, next_step = steps[status]
@ -127,12 +127,13 @@ class Mission:
self.status(next_step) self.status(next_step)
elif type(next_step) == dict: elif type(next_step) == dict:
if result not in next_step: if result not in next_step:
logging.warning(f'Invalid step result {result}') self.ship.log(f'Invalid step result {result}', 1)
self.status('error') self.status('error')
return return
else: else:
if result is None: result=''
self.status(next_step[result]) self.status(next_step[result])
print(f'{self.ship} {status} -> {self.status()}') self.ship.log(f'{status} {result} -> {self.status()}', 8)
class BaseMission(Mission): class BaseMission(Mission):
def step_go_dest(self): def step_go_dest(self):
@ -168,22 +169,19 @@ class BaseMission(Mission):
return 'more' return 'more'
def step_sell(self, except_resource=True): def step_sell(self, except_resource=True):
target = self.st('resource')
market = self.store.get('Marketplace', self.ship.location.symbol) market = self.store.get('Marketplace', self.ship.location.symbol)
sellables = market.sellable_items(self.ship.cargo.keys()) sellables = market.sellable_items(self.ship.cargo.keys())
if target in sellables and except_resource:
sellables.remove(target)
if len(sellables) == 0: if len(sellables) == 0:
return 'done' return 'done'
resource = sellables[0] resource = sellables[0]
volume = market.volume(resource) volume = market.volume(resource)
amount = self.ship.get_cargo(resource) amt_cargo = self.ship.get_cargo(resource)
while amount > 0:
amt = min(amount, volume) amount = min(amt_cargo, volume)
self.api.sell(self.ship, resource, amt) self.api.sell(self.ship, resource, amount)
amount -= amt
if len(sellables) == 1: if len(sellables) == 1 and amt_cargo == amount:
return 'done' return 'done'
else: else:
return 'more' return 'more'

View File

@ -17,7 +17,6 @@ class TradeMission(BaseMission):
price = smkt.buy_price(resource) price = smkt.buy_price(resource)
volume = smkt.volume(resource) volume = smkt.volume(resource)
affordable = credits // price affordable = credits // price
print(cargo_space, affordable, volume)
amount = min(cargo_space, affordable, volume) amount = min(cargo_space, affordable, volume)
if amount == 0: if amount == 0:
return 'done' return 'done'
@ -43,6 +42,9 @@ class TradeMission(BaseMission):
'market-post': (self.step_market, 'travel-back'), 'market-post': (self.step_market, 'travel-back'),
**self.travel_steps('back', 'dest', 'dock-dest'), **self.travel_steps('back', 'dest', 'dock-dest'),
'dock-dest': (self.step_dock, 'unload'), 'dock-dest': (self.step_dock, 'unload'),
'unload': (self.step_unload, 'market-dest'), 'unload': (self.step_sell, {
'more': 'unload',
'done': 'market-dest'
}),
'market-dest': (self.step_market, 'done'), 'market-dest': (self.step_market, 'done'),
} }

View File

@ -1,5 +1,5 @@
from .base import Base from .base import Base
from time import time from time import time, strftime
from nullptr.util import * from nullptr.util import *
from nullptr.models import Waypoint from nullptr.models import Waypoint
import os import os
@ -22,15 +22,18 @@ class Ship(Base):
self.frame = '' self.frame = ''
self.speed = "CRUISE" self.speed = "CRUISE"
self._log_file = None self._log_file = None
self._log_level = 5
def log(self, m): def log(self, m, l=3):
if self._log_file is None: if self._log_file is None:
fn = os.path.join(self.store.data_dir, f'{self.symbol}.{self.ext()}.log') fn = os.path.join(self.store.data_dir, f'{self.symbol}.{self.ext()}.log')
self._log_file = open(fn, 'a') self._log_file = open(fn, 'a')
ts = int(time()) ts = strftime('%Y%m%d %H%M%S')
m = m.strip() m = m.strip()
self._log_file.write(f'{ts} {m}\n') self._log_file.write(f'{ts} {m}\n')
self._log_file.flush() self._log_file.flush()
if l <= self._log_level:
print(f'{self} {ts} {m}')
@classmethod @classmethod
def ext(self): def ext(self):