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

This commit is contained in:
Richard Bronkhorst 2023-06-16 23:05:47 +02:00
parent 3f93d863a0
commit bb93950fa3
3 changed files with 86 additions and 3 deletions

View File

@ -141,3 +141,44 @@ class Api:
if 'agent' in data: if 'agent' in data:
self.agent.update(data['agent']) self.agent.update(data['agent'])
return contract 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

View File

@ -72,8 +72,11 @@ class Commander(CommandLine):
def after_cmd(self): def after_cmd(self):
self.store.flush() self.store.flush()
def do_info(self): def do_info(self, arg=''):
pprint(self.api.info(), 100) if arg.startswith('r'):
self.api.info()
pprint(self.agent, 100)
def do_register(self, faction): def do_register(self, faction):
self.api.register(faction.upper()) self.api.register(faction.upper())
@ -200,3 +203,26 @@ class Commander(CommandLine):
waypoint = self.resolve('Waypoint', arg) waypoint = self.resolve('Waypoint', arg)
r = self.api.marketplace(waypoint) r = self.api.marketplace(waypoint)
pprint(r) 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()

View File

@ -17,8 +17,16 @@ class Marketplace(SystemMember):
self.setlst('exchange', d, 'exchange', 'symbol') self.setlst('exchange', d, 'exchange', 'symbol')
if 'tradeGoods' in d: if 'tradeGoods' in d:
self.last_prices = time() self.last_prices = time()
prices = {}
for g in mg(d, 'tradeGoods'): 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 @classmethod
def ext(self): def ext(self):
return 'mkt' return 'mkt'
@ -26,3 +34,11 @@ class Marketplace(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'
for p in self.prices.values():
r += f'{p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n'
return r