From bb93950fa3f941bc65f8a83d3f40a260b2e9949a Mon Sep 17 00:00:00 2001 From: Richard Bronkhorst Date: Fri, 16 Jun 2023 23:05:47 +0200 Subject: [PATCH] Update api.py, commander.py and one other file --- nullptr/api.py | 41 +++++++++++++++++++++++++++++++++++ nullptr/commander.py | 30 +++++++++++++++++++++++-- nullptr/models/marketplace.py | 18 ++++++++++++++- 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/nullptr/api.py b/nullptr/api.py index f96c012..266fdb9 100644 --- a/nullptr/api.py +++ b/nullptr/api.py @@ -141,3 +141,44 @@ class Api: if 'agent' in data: self.agent.update(data['agent']) return contract + + def sell(self, ship, typ): + units = ship.get_cargo(typ) + data = { + 'symbol': typ, + 'units': units + } + data = self.request('post', f'my/ships/{ship}/sell', data) + if 'cargo' in data: + ship.update(data) + if 'agent' in data: + self.agent.update(data['agent']) + return data + + def buy(self, ship, typ, amt): + data = { + 'symbol': typ, + 'units': amt + } + data = self.request('post', f'my/ships/{ship}/purchase', data) + if 'cargo' in data: + ship.update(data) + if 'agent' in data: + self.agent.update(data['agent']) + return data + + def jettison(self, ship, typ): + units = ship.get_cargo(typ) + if units == 0: + print('cargo not found') + return + data = { + 'symbol': typ, + 'units': units + } + data = self.request('post', f'my/ships/{ship.symbol}/jettison', data) + if 'cargo' in data: + ship.update(data) + if 'agent' in data: + self.agent.update(data['agent']) + return data diff --git a/nullptr/commander.py b/nullptr/commander.py index 648ade8..e463a64 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -72,8 +72,11 @@ class Commander(CommandLine): def after_cmd(self): self.store.flush() - def do_info(self): - pprint(self.api.info(), 100) + def do_info(self, arg=''): + if arg.startswith('r'): + self.api.info() + + pprint(self.agent, 100) def do_register(self, faction): self.api.register(faction.upper()) @@ -200,3 +203,26 @@ class Commander(CommandLine): waypoint = self.resolve('Waypoint', arg) r = self.api.marketplace(waypoint) pprint(r) + + def do_cargo(self): + if not self.has_ship(): return + for c, units in self.ship.cargo.items(): + print(f'{units:4d} {c}') + + def do_buy(self, resource, amt=None): + if not self.has_ship(): return + if amt is None: + amt = self.ship.cargo_capacity - self.ship.cargo_units + self.api.buy(self.ship, resource.upper(), amt) + self.do_cargo() + + def do_sell(self, resource): + if not self.has_ship(): return + self.api.sell(self.ship, resource.upper()) + self.do_cargo() + + def do_dump(self, resource): + if not self.has_ship(): return + self.api.jettison(self.ship, resource.upper()) + self.do_cargo() + diff --git a/nullptr/models/marketplace.py b/nullptr/models/marketplace.py index 79eee6d..9229c3f 100644 --- a/nullptr/models/marketplace.py +++ b/nullptr/models/marketplace.py @@ -17,8 +17,16 @@ class Marketplace(SystemMember): self.setlst('exchange', d, 'exchange', 'symbol') if 'tradeGoods' in d: self.last_prices = time() + prices = {} for g in mg(d, 'tradeGoods'): - pass + price = {} + symbol= mg(g, 'symbol') + price['symbol'] = symbol + price['buy'] = mg(g, 'purchasePrice') + price['sell'] = mg(g, 'sellPrice') + prices[symbol] = price + self.prices = prices + @classmethod def ext(self): return 'mkt' @@ -26,3 +34,11 @@ class Marketplace(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' + for p in self.prices.values(): + r += f'{p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n' + return r