From b47fa44cb0282c020b085613c102c105f388c621 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 2 Jan 2024 06:35:26 +0100 Subject: [PATCH] mission and cli improvements --- nullptr/api.py | 6 +-- nullptr/atlas_builder.py | 6 ++- nullptr/commander.py | 74 +++++++++++++++++++++++++++++++---- nullptr/missions/base.py | 3 +- nullptr/models/atlas.py | 3 ++ nullptr/models/jumpgate.py | 4 +- nullptr/models/marketplace.py | 4 +- 7 files changed, 83 insertions(+), 17 deletions(-) diff --git a/nullptr/api.py b/nullptr/api.py index 6aeb63b..a1209d1 100644 --- a/nullptr/api.py +++ b/nullptr/api.py @@ -32,7 +32,7 @@ class Api: try: return self.request_once(method, path, data, need_token, params) except (ApiLimitError, requests.exceptions.Timeout): - print('oops, hit the limit. take a break') + # print('oops, hit the limit. take a break') sleep(10) return self.request_once(method, path, data, need_token, params) @@ -90,8 +90,8 @@ class Api: def list_waypoints(self, system): data = self.request('get', f'systems/{system}/waypoints/') tp = total_pages(self.last_meta) - for p in range(1, tp): - data += self.request('get', f'systems/{system}/waypoints/', params={'page': p}) + for p in range(tp): + data += self.request('get', f'systems/{system}/waypoints/', params={'page': p+1}) # pprint(data) return self.store.update_list(Waypoint, data) diff --git a/nullptr/atlas_builder.py b/nullptr/atlas_builder.py index f5f70bf..0dc7ad5 100644 --- a/nullptr/atlas_builder.py +++ b/nullptr/atlas_builder.py @@ -15,6 +15,8 @@ class AtlasBuilder: self.atlas = self.store.get(Atlas, 'ATLAS', create=True) def find_work(self): + if not self.atlas.enabled: + return first_page = self.atlas.total_pages == 0 pages_left = self.atlas.total_pages > self.atlas.seen_pages @@ -63,10 +65,10 @@ class AtlasBuilder: if 'UNCHARTED' in w.traits: continue if 'MARKETPLACE' in w.traits: - print(f'marketplace at {w}') + #print(f'marketplace at {w}') self.sched(self.api.marketplace, w) if w.type == 'JUMP_GATE': - print(f'jumpgate at {w}') + #print(f'jumpgate at {w}') self.sched(self.api.jumps, w) if 'SHIPYARD' in w.traits: # todo diff --git a/nullptr/commander.py b/nullptr/commander.py index 34bb71f..e622f3e 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -10,6 +10,8 @@ from threading import Thread from nullptr.central_command import CentralCommand import readline import os +from copy import copy + class CommandError(Exception): pass @@ -80,7 +82,9 @@ class Commander(CommandLine): print('=== contracts') self.do_contracts('r') ship = self.store.get(Ship, symbol.upper() + '-2') - api.list_waypoints(ship.location.system) + print("=== catalog initial system") + self.do_catalog(ship.location.system) + self.do_stats() self.store.flush() return agent @@ -93,7 +97,17 @@ class Commander(CommandLine): raise CommandError('multiple matches') else: raise CommandError('not found') - + + def resolve_system(self, system_str): + if type(system_str) == System: + return system_str + if system_str == '': + if not self.has_ship(): return + system = self.ship.location.system + else: + system = self.store.get(System, system_str) + return system + def after_cmd(self): self.store.flush() @@ -179,7 +193,7 @@ class Commander(CommandLine): resource = delivery['trade_symbol'] destination = delivery['destination'] m = self.analyzer.find_closest_markets(resource, 'buy,exchange', destination) - if len(m) is None: + if len(m) == 0: print('no market found') return markets = [ mkt[1] for mkt in m] @@ -188,6 +202,16 @@ class Commander(CommandLine): self.centcom.set_mission_param(self.ship, 'hops', markets) self.print_mission() + def do_sprobe(self): + if not self.has_ship(): return + system = self.ship.location.system + m = self.store.all_members(system, 'Marketplace') + m = self.analyzer.solve_tsp(m) + hops = [w.symbol for w in m] + self.centcom.init_mission(self.ship, 'probe') + self.centcom.set_mission_param(self.ship, 'hops', hops) + self.print_mission() + def do_travel(self, dest): dest = self.resolve('Waypoint', dest) self.centcom.init_mission(self.ship, 'travel') @@ -213,7 +237,17 @@ class Commander(CommandLine): def do_defrag(self): self.store.defrag() - + + def do_catalog(self, system_str=''): + system = self.resolve_system(system_str) + r = self.api.list_waypoints(system) + for w in r: + if 'MARKETPLACE' in w.traits: + self.api.marketplace(w) + if w.type == 'JUMP_GATE': + self.api.jumps(w) + + def do_system(self, system_str): system = self.store.get(System, system_str) r = self.api.list_waypoints(system) @@ -256,6 +290,11 @@ class Commander(CommandLine): continue print(f'{wname:4} {typ} {traits}') + def do_members(self): + if not self.has_ship(): return + system = self.ship.location.system + pprint(list(self.store.all_members(system))) + def do_wp(self, s=''): self.do_waypoints(s) @@ -263,8 +302,10 @@ class Commander(CommandLine): waypoint = self.store.get(Waypoint, waypoint_str.upper()) r = self.api.marketplace(waypoint) - def do_atlas(self): + def do_atlas(self, state=None): atlas = self.store.get(Atlas, 'ATLAS') + if state is not None: + atlas.enabled = True if state == 'on' else 'off' pprint(atlas, 5) def do_jumps(self, waypoint_str=None): @@ -395,8 +436,27 @@ class Commander(CommandLine): else: waypoint = self.resolve('Waypoint', arg) r = self.api.marketplace(waypoint) - pprint(r) - + pprint(r, 3) + + def do_prices(self, resource=None): + if not self.has_ship(): return + system = self.ship.location.system + prices = {} + for m in self.store.all_members(system, Marketplace): + for p in m.prices.values(): + r = p['symbol'] + if not r in prices: + prices[r] = [] + prices[r].append({ + 'wp': m.symbol, + 'buy': p['buy'], + 'sell': p['sell'] + }) + if resource is not None: + pprint(prices[resource.upper()]) + else: + pprint(prices) + def do_cargo(self): if not self.has_ship(): return print(f'== Cargo {self.ship.cargo_units}/{self.ship.cargo_capacity} ==') diff --git a/nullptr/missions/base.py b/nullptr/missions/base.py index 42bfc43..51478f6 100644 --- a/nullptr/missions/base.py +++ b/nullptr/missions/base.py @@ -148,10 +148,10 @@ class BaseMission(Mission): self.next_step = self.ship.arrival def step_unload(self): - contract = self.rst(Contract, 'contract') delivery = self.st('delivery') if delivery == 'sell': return self.step_sell(False) + contract = self.rst(Contract, 'contract') typs = self.ship.deliverable_cargo(contract) if len(typs) == 0: return 'done' @@ -206,6 +206,7 @@ class BaseMission(Mission): dest = self.store.get(Waypoint, dest) loc = self.ship.location loc_sys = loc.system + loc_jg = self.analyzer.get_jumpgate(loc_sys) loc_jg_wp = self.store.get(Waypoint, loc_jg.symbol) dest_sys = dest.system diff --git a/nullptr/models/atlas.py b/nullptr/models/atlas.py index dd11a99..18c3994 100644 --- a/nullptr/models/atlas.py +++ b/nullptr/models/atlas.py @@ -8,9 +8,12 @@ class Atlas(Base): def define(self): self.total_pages = 0 self.seen_pages = 0 + self.enabled = False def f(self, detail=1): r = super().f(detail) if detail >2: + if not self.enabled: + r += ' OFF' r += f' {self.seen_pages}/{self.total_pages}' return r diff --git a/nullptr/models/jumpgate.py b/nullptr/models/jumpgate.py index a7092d3..e525029 100644 --- a/nullptr/models/jumpgate.py +++ b/nullptr/models/jumpgate.py @@ -16,8 +16,8 @@ class Jumpgate(Base): return 'jmp' def f(self, detail=1): - r = self.symbol - if detail > 1: + r = super().f(detail) + if detail > 2: r += '\n' r += '\n'.join([s.symbol for s in self.connections]) return r diff --git a/nullptr/models/marketplace.py b/nullptr/models/marketplace.py index a020f42..3b3281a 100644 --- a/nullptr/models/marketplace.py +++ b/nullptr/models/marketplace.py @@ -53,8 +53,8 @@ class Marketplace(Base): return '?' def f(self, detail=1): - r = self.symbol - if detail > 1: + r = super().f(detail) + if detail > 2: r += '\n' if len(self.imports) > 0: r += 'I: ' + ', '.join(self.imports) + '\n'