mission and cli improvements

This commit is contained in:
Richard 2024-01-02 06:35:26 +01:00
parent 6118772a63
commit b47fa44cb0
7 changed files with 83 additions and 17 deletions

View File

@ -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)

View File

@ -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

View File

@ -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} ==')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'