Update api.py, commander.py and six other files

This commit is contained in:
Richard Bronkhorst 2023-06-17 14:59:52 +02:00
parent bb93950fa3
commit b5850bcb5f
8 changed files with 92 additions and 13 deletions

View File

@ -95,7 +95,7 @@ class Api:
def jumps(self, waypoint): def jumps(self, waypoint):
data = self.request('get', f'systems/{waypoint.system()}/waypoints/{waypoint}/jump-gate') data = self.request('get', f'systems/{waypoint.system()}/waypoints/{waypoint}/jump-gate')
symbol = str(waypoint) symbol = str(waypoint)
return self.store.update(Jumpgate, data) return self.store.update(Jumpgate, data, symbol)
def list_ships(self): def list_ships(self):
data = self.request('get', 'my/ships') data = self.request('get', 'my/ships')
@ -111,6 +111,31 @@ class Api:
contract = self.store.update('Contract', data['contract']) contract = self.store.update('Contract', data['contract'])
return contract return contract
def deliver(self, ship, typ, contract):
units = ship.get_cargo(typ)
if units == 0:
print("Resource not in cargo")
return {}
data = {
'shipSymbol': str(ship),
'tradeSymbol': typ.upper(),
'units': units
}
data = self.request('post', f'my/contracts/{contract}/deliver', data)
if 'cargo' in data:
ship.update(data)
if 'contract' in data:
contract.update(data['contract'])
return contract
def fulfill(self, contract):
data = self.request('post', f'my/contracts/{contract}/fulfill')
if 'contract' in data:
contract.update(data['contract'])
if 'agent' in data:
self.agent.update(data['agent'])
return contract
def navigate(self, ship, wp): def navigate(self, ship, wp):
data = {'waypointSymbol': str(wp)} data = {'waypointSymbol': str(wp)}
response = self.request('post', f'my/ships/{ship}/navigate', data) response = self.request('post', f'my/ships/{ship}/navigate', data)
@ -182,3 +207,27 @@ class Api:
if 'agent' in data: if 'agent' in data:
self.agent.update(data['agent']) self.agent.update(data['agent'])
return data return data
def purchase(self, typ, wp):
data = {
'shipType': typ,
'waypointSymbol': str(wp)
}
data = self.request('post', 'my/ships', data)
if 'agent' in data:
self.agent.update(data['agent'])
if 'ship' in data:
ship = self.store.update('Ship', data['ship'])
return ship
def jump(self, ship, system):
data = {
"systemSymbol": system.symbol
}
data = self.request('post', f'my/ships/{ship}/jump', data)
if 'nav' in data:
ship.update(data)
return ship
def shipyard(self, wp):
return self.request('get', f'systems/{wp.system()}/waypoints/{wp}/shipyard')

View File

@ -110,7 +110,11 @@ class Commander(CommandLine):
waypoint = self.store.get(Waypoint, waypoint_str.upper()) waypoint = self.store.get(Waypoint, waypoint_str.upper())
r = self.api.marketplace(waypoint) r = self.api.marketplace(waypoint)
def do_jumps(self, waypoint_str): def do_jumps(self, waypoint_str=None):
if waypoint_str is None:
if not self.has_ship(): return
waypoint = self.ship.location()
else:
waypoint = self.store.get(Waypoint, waypoint_str.upper()) waypoint = self.store.get(Waypoint, waypoint_str.upper())
r = self.api.jumps(waypoint) r = self.api.jumps(waypoint)
pprint(r) pprint(r)
@ -226,3 +230,22 @@ class Commander(CommandLine):
self.api.jettison(self.ship, resource.upper()) self.api.jettison(self.ship, resource.upper())
self.do_cargo() self.do_cargo()
def do_shipyard(self):
if not self.has_ship(): return
location = self.ship.location()
pprint(self.api.shipyard(location))
def do_jump(self, system_str):
if not self.has_ship(): return
system = self.resolve('System', system_str)
self.api.jump(self.ship, system)
pprint(self.ship)
def do_purchase(self, ship_type):
if not self.has_ship(): return
location = self.ship.location()
ship_type = ship_type.upper()
if not ship_type.startswith('SHIP'):
ship_type = 'SHIP_' + ship_type
s = self.api.purchase(ship_type, location)
pprint(s)

View File

@ -25,7 +25,7 @@ class Contract(Base):
def api_dict(self): def api_dict(self):
return { return {
'id': self.symbol, 'id': self.symbol.lower(),
'expiration': self.expires_str, 'expiration': self.expires_str,
} }

View File

@ -4,7 +4,7 @@ from dataclasses import field
class Jumpgate(SystemMember): class Jumpgate(SystemMember):
range: int range: int
faction: str faction: str
systems: list = field(default_factory=list) systems: list = []
def update(self, d): def update(self, d):
self.setlst('systems', d, 'connectedSystems', 'symbol') self.setlst('systems', d, 'connectedSystems', 'symbol')
@ -18,3 +18,9 @@ class Jumpgate(SystemMember):
def path(self): def path(self):
sector, system, _ = self.symbol.split('-') sector, system, _ = self.symbol.split('-')
return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}' return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}'
def f(self, detail=1):
r = self.symbol
if detail > 1:
r += '\n'.join(self.systems)
return r

View File

@ -5,10 +5,10 @@ from nullptr.util import *
from dataclasses import field from dataclasses import field
class Marketplace(SystemMember): class Marketplace(SystemMember):
imports:list = field(default_factory=list) imports:list = []
exports:list = field(default_factory=list) exports:list = []
exchange:list = field(default_factory=list) exchange:list = []
prices:dict = field(default_factory=dict) prices:dict = {}
last_prices:int = 0 last_prices:int = 0
def update(self, d): def update(self, d):

View File

@ -5,7 +5,7 @@ from dataclasses import dataclass, field
class Ship(Base): class Ship(Base):
cargo:dict = {} cargo:dict = {}
mission_state:dict = field(default_factory=dict) mission_state:dict = {}
status:str = '' status:str = ''
cargo_capacity:int = 0 cargo_capacity:int = 0
cargo_units:int = 0 cargo_units:int = 0

View File

@ -6,7 +6,7 @@ class Waypoint(SystemMember):
x:int = 0 x:int = 0
y:int = 0 y:int = 0
type:str = 'unknown' type:str = 'unknown'
traits:list = field(default_factory=list) traits:list = []
faction:str = '' faction:str = ''
def update(self, d): def update(self, d):

View File

@ -93,9 +93,10 @@ class Store:
return None return None
return self.data[typ][symbol] return self.data[typ][symbol]
def update(self, typ, data): def update(self, typ, data, symbol=None):
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]
if symbol is None:
symbol = mg(data, typ.identifier) symbol = mg(data, typ.identifier)
obj = self.get(typ, symbol, True) obj = self.get(typ, symbol, True)
obj.update(data) obj.update(data)