From b5850bcb5f825b1f3809064cd59f80bf47472371 Mon Sep 17 00:00:00 2001 From: Richard Bronkhorst Date: Sat, 17 Jun 2023 14:59:52 +0200 Subject: [PATCH] Update api.py, commander.py and six other files --- nullptr/api.py | 51 ++++++++++++++++++++++++++++++++++- nullptr/commander.py | 27 +++++++++++++++++-- nullptr/models/contract.py | 2 +- nullptr/models/jumpgate.py | 8 +++++- nullptr/models/marketplace.py | 8 +++--- nullptr/models/ship.py | 2 +- nullptr/models/waypoint.py | 2 +- nullptr/store.py | 5 ++-- 8 files changed, 92 insertions(+), 13 deletions(-) diff --git a/nullptr/api.py b/nullptr/api.py index 266fdb9..16fa3fa 100644 --- a/nullptr/api.py +++ b/nullptr/api.py @@ -95,7 +95,7 @@ class Api: def jumps(self, waypoint): data = self.request('get', f'systems/{waypoint.system()}/waypoints/{waypoint}/jump-gate') symbol = str(waypoint) - return self.store.update(Jumpgate, data) + return self.store.update(Jumpgate, data, symbol) def list_ships(self): data = self.request('get', 'my/ships') @@ -110,6 +110,31 @@ class Api: if data is not None and 'contract' in data: contract = self.store.update('Contract', data['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): data = {'waypointSymbol': str(wp)} @@ -182,3 +207,27 @@ class Api: if 'agent' in data: self.agent.update(data['agent']) 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') diff --git a/nullptr/commander.py b/nullptr/commander.py index e463a64..f5898bc 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -110,8 +110,12 @@ class Commander(CommandLine): waypoint = self.store.get(Waypoint, waypoint_str.upper()) r = self.api.marketplace(waypoint) - def do_jumps(self, waypoint_str): - waypoint = self.store.get(Waypoint, waypoint_str.upper()) + 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()) r = self.api.jumps(waypoint) pprint(r) @@ -225,4 +229,23 @@ class Commander(CommandLine): if not self.has_ship(): return self.api.jettison(self.ship, resource.upper()) 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) diff --git a/nullptr/models/contract.py b/nullptr/models/contract.py index 419648c..1713340 100644 --- a/nullptr/models/contract.py +++ b/nullptr/models/contract.py @@ -25,7 +25,7 @@ class Contract(Base): def api_dict(self): return { - 'id': self.symbol, + 'id': self.symbol.lower(), 'expiration': self.expires_str, } diff --git a/nullptr/models/jumpgate.py b/nullptr/models/jumpgate.py index d2ee8f9..29a8070 100644 --- a/nullptr/models/jumpgate.py +++ b/nullptr/models/jumpgate.py @@ -4,7 +4,7 @@ from dataclasses import field class Jumpgate(SystemMember): range: int faction: str - systems: list = field(default_factory=list) + systems: list = [] def update(self, d): self.setlst('systems', d, 'connectedSystems', 'symbol') @@ -18,3 +18,9 @@ class Jumpgate(SystemMember): def path(self): sector, system, _ = self.symbol.split('-') 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 diff --git a/nullptr/models/marketplace.py b/nullptr/models/marketplace.py index 9229c3f..e8cbfe6 100644 --- a/nullptr/models/marketplace.py +++ b/nullptr/models/marketplace.py @@ -5,10 +5,10 @@ from nullptr.util import * from dataclasses import field class Marketplace(SystemMember): - imports:list = field(default_factory=list) - exports:list = field(default_factory=list) - exchange:list = field(default_factory=list) - prices:dict = field(default_factory=dict) + imports:list = [] + exports:list = [] + exchange:list = [] + prices:dict = {} last_prices:int = 0 def update(self, d): diff --git a/nullptr/models/ship.py b/nullptr/models/ship.py index 3cfcb0c..70d648c 100644 --- a/nullptr/models/ship.py +++ b/nullptr/models/ship.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field class Ship(Base): cargo:dict = {} - mission_state:dict = field(default_factory=dict) + mission_state:dict = {} status:str = '' cargo_capacity:int = 0 cargo_units:int = 0 diff --git a/nullptr/models/waypoint.py b/nullptr/models/waypoint.py index c867fad..68b85f4 100644 --- a/nullptr/models/waypoint.py +++ b/nullptr/models/waypoint.py @@ -6,7 +6,7 @@ class Waypoint(SystemMember): x:int = 0 y:int = 0 type:str = 'unknown' - traits:list = field(default_factory=list) + traits:list = [] faction:str = '' def update(self, d): diff --git a/nullptr/store.py b/nullptr/store.py index 2a2f0e2..818cc26 100644 --- a/nullptr/store.py +++ b/nullptr/store.py @@ -93,10 +93,11 @@ class Store: return None 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: typ = self.model_names[typ] - symbol = mg(data, typ.identifier) + if symbol is None: + symbol = mg(data, typ.identifier) obj = self.get(typ, symbol, True) obj.update(data) return obj