diff --git a/nullptr/analyzer.py b/nullptr/analyzer.py index 51aa258..ed173e1 100644 --- a/nullptr/analyzer.py +++ b/nullptr/analyzer.py @@ -194,14 +194,11 @@ class Analyzer: def find_deal(self, smkt, dmkt): best_margin = 0 best_resource = None - print(f'finding deal from {smkt} to {dmkt}') for r, sp in smkt.prices.items(): if not r in dmkt.prices: - print(r, 'not in dmkt') continue dp = dmkt.prices[r] margin = dp.sell - sp.buy - print(r, margin) if margin > best_margin: best_margin = margin best_resource = r diff --git a/nullptr/central_command.py b/nullptr/central_command.py index 78a4cde..7bf7dee 100644 --- a/nullptr/central_command.py +++ b/nullptr/central_command.py @@ -18,6 +18,7 @@ class CentralCommand: self.analyzer = Analyzer(store) self.api = api self.atlas_builder = AtlasBuilder(store, api) + self.update_missions() def get_ready_missions(self): result = [] @@ -25,6 +26,12 @@ class CentralCommand: if mission.is_ready(): result.append(ship) 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): self.update_missions() diff --git a/nullptr/commander.py b/nullptr/commander.py index dcdb093..1db43eb 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -54,6 +54,11 @@ class Commander(CommandLine): def do_auto(self): self.centcom.run_interactive() + + def do_log(self, level): + if not self.has_ship(): return + self.ship._log_level = int(level) + ######## Resolvers ######### def ask_obj(self, typ, prompt): obj = None @@ -108,7 +113,10 @@ class Commander(CommandLine): if not self.has_ship(): return s = self.ship.location.system 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 ######### @@ -223,11 +231,7 @@ class Commander(CommandLine): ######## Specials ######### def do_market(self, arg=''): - if arg == '': - if not self.has_ship(): return - waypoint = self.ship.location - else: - waypoint = self.resolve('Waypoint', arg) + waypoint = self.resolve_waypoint(arg) r = self.api.marketplace(waypoint) pprint(r, 3) @@ -349,6 +353,11 @@ class Commander(CommandLine): if not self.has_ship(): return self.centcom.restart_mission(self.ship, status) 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): if not self.has_ship(): return diff --git a/nullptr/missions/base.py b/nullptr/missions/base.py index c988bd9..60a714c 100644 --- a/nullptr/missions/base.py +++ b/nullptr/missions/base.py @@ -96,7 +96,7 @@ class Mission: } def step_done(self): - logging.info(f'mission finished for {self.ship}') + self.ship.log(f'mission finished', 3) def is_waiting(self): return self.next_step > time() or self.ship.cooldown > time() or self.ship.arrival > time() @@ -113,7 +113,7 @@ class Mission: self.init_state() status = self.status() if not status in steps: - logging.warning(f"Invalid mission status {status}") + self.ship.log(f"Invalid mission status {status}", 1) self.status('error') return handler, next_step = steps[status] @@ -127,12 +127,13 @@ class Mission: self.status(next_step) elif type(next_step) == dict: 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') return else: + if result is None: 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): def step_go_dest(self): @@ -168,22 +169,19 @@ class BaseMission(Mission): return 'more' def step_sell(self, except_resource=True): - target = self.st('resource') market = self.store.get('Marketplace', self.ship.location.symbol) sellables = market.sellable_items(self.ship.cargo.keys()) - if target in sellables and except_resource: - sellables.remove(target) if len(sellables) == 0: return 'done' resource = sellables[0] volume = market.volume(resource) - amount = self.ship.get_cargo(resource) - while amount > 0: - amt = min(amount, volume) - self.api.sell(self.ship, resource, amt) - amount -= amt - if len(sellables) == 1: + amt_cargo = self.ship.get_cargo(resource) + + amount = min(amt_cargo, volume) + self.api.sell(self.ship, resource, amount) + + if len(sellables) == 1 and amt_cargo == amount: return 'done' else: return 'more' diff --git a/nullptr/missions/trade.py b/nullptr/missions/trade.py index 4fcbc9d..a0ba551 100644 --- a/nullptr/missions/trade.py +++ b/nullptr/missions/trade.py @@ -17,7 +17,6 @@ class TradeMission(BaseMission): price = smkt.buy_price(resource) volume = smkt.volume(resource) affordable = credits // price - print(cargo_space, affordable, volume) amount = min(cargo_space, affordable, volume) if amount == 0: return 'done' @@ -43,6 +42,9 @@ class TradeMission(BaseMission): 'market-post': (self.step_market, 'travel-back'), **self.travel_steps('back', 'dest', 'dock-dest'), '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'), } diff --git a/nullptr/models/ship.py b/nullptr/models/ship.py index 67bdd70..c2c604b 100644 --- a/nullptr/models/ship.py +++ b/nullptr/models/ship.py @@ -1,5 +1,5 @@ from .base import Base -from time import time +from time import time, strftime from nullptr.util import * from nullptr.models import Waypoint import os @@ -22,15 +22,18 @@ class Ship(Base): self.frame = '' self.speed = "CRUISE" self._log_file = None + self._log_level = 5 - def log(self, m): + def log(self, m, l=3): 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()) + ts = strftime('%Y%m%d %H%M%S') m = m.strip() self._log_file.write(f'{ts} {m}\n') self._log_file.flush() + if l <= self._log_level: + print(f'{self} {ts} {m}') @classmethod def ext(self):