Update api.py, commander.py and one other file

This commit is contained in:
Richard Bronkhorst 2023-06-15 22:37:33 +02:00
parent ffd094df87
commit 92a5a02180
3 changed files with 78 additions and 6 deletions

View File

@ -101,4 +101,22 @@ class Api:
data = self.request('get', 'my/ships') data = self.request('get', 'my/ships')
return self.store.update_list(Ship, data) return self.store.update_list(Ship, data)
def navigate(self, ship, wp):
data = {'waypointSymbol': str(wp)}
response = self.request('post', f'my/ships/{ship}/navigate', data)
ship.update(response)
def dock(self, ship):
data = self.request('post', f'my/ships/{ship}/dock')
ship.update(data)
return data
def orbit(self, ship):
data = self.request('post', f'my/ships/{ship}/orbit')
ship.update(data)
return data
def refuel(self, ship):
data = self.request('post', f'my/ships/{ship}/refuel')
ship.update(data)
return data

View File

@ -22,10 +22,23 @@ class Commander(CommandLine):
self.api = Api(self.store, self.agent) self.api = Api(self.store, self.agent)
self.atlas_builder = AtlasBuilder(self.store, self.api) self.atlas_builder = AtlasBuilder(self.store, self.api)
self.analyzer = Analyzer(self.store) self.analyzer = Analyzer(self.store)
self.ship = None
self.stop_auto= False self.stop_auto= False
super().__init__() super().__init__()
def prompt(self):
if self.ship:
return f'{self.ship.symbol}> '
else:
return '> '
def has_ship(self):
if self.ship is not None:
return True
else:
print('set a ship')
def ask_obj(self, typ, prompt): def ask_obj(self, typ, prompt):
obj = None obj = None
while obj is None: while obj is None:
@ -59,10 +72,17 @@ class Commander(CommandLine):
r = self.api.list_systems(int(page)) r = self.api.list_systems(int(page))
pprint(self.api.last_meta) pprint(self.api.last_meta)
def do_waypoints(self, system_str):
system = self.store.get(System, system_str.upper()) def do_waypoints(self, system_str=''):
r = self.api.list_waypoints(system) if system_str == '':
pprint(r) if not self.has_ship(): return
system = self.ship.location().system()
else:
system = self.store.get(System, system_str)
r = self.store.all_members(system, 'Waypoint')
for w in r:
traits = ','.join(w.traits)
print(w.symbol, traits)
def do_marketplace(self, waypoint_str): def do_marketplace(self, waypoint_str):
waypoint = self.store.get(Waypoint, waypoint_str.upper()) waypoint = self.store.get(Waypoint, waypoint_str.upper())
@ -99,5 +119,35 @@ class Commander(CommandLine):
r = list(self.store.all('Ship')) r = list(self.store.all('Ship'))
pprint(r) pprint(r)
def do_ship(self, arg=''):
if arg != '':
symbol = f'{self.agent.symbol}-{arg}'
ship = self.store.get('Ship', symbol)
if ship is None:
print('not found')
return
else:
self.ship = ship
pprint(ship)
def do_pp(self): def do_pp(self):
pprint(self.api.last_result) pprint(self.api.last_result)
def do_go(self, arg):
if not self.has_ship(): return
system = self.ship.location().system()
symbol = f'{system}-{arg}'
dest = self.store.get('Waypoint', symbol)
self.api.navigate(self.ship, dest)
pprint(self.ship)
def do_dock(self):
if not self.has_ship(): return
self.api.dock(self.ship)
pprint(self.ship)
def do_orbit(self):
if not self.has_ship(): return
self.api.orbit(self.ship)
pprint(self.ship)

View File

@ -82,7 +82,7 @@ class Store:
def get(self, typ, symbol, create=False): def get(self, typ, symbol, create=False):
if type(typ) == str and typ in self.model_names: if type(typ) == str and typ in self.model_names:
typ = self.model_names[typ] typ = self.model_names[typ]
symbol = symbol.upper()
if typ not in self.data: if typ not in self.data:
return None return None
if symbol not in self.data[typ]: if symbol not in self.data[typ]:
@ -108,8 +108,12 @@ class Store:
yield m yield m
def all_members(self, system, typ=None): def all_members(self, system, typ=None):
if type(typ) == str and typ in self.model_names:
typ = self.model_names[typ]
if type(system) == System: if type(system) == System:
system = system.symbol system = system.symbol
if system not in self.system_members: if system not in self.system_members:
return return
for m in self.system_members[system]: for m in self.system_members[system]: