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

This commit is contained in:
Richard Bronkhorst 2023-06-16 14:41:11 +02:00
parent 2c96cbb533
commit 3f93d863a0
8 changed files with 65 additions and 17 deletions

View File

@ -128,5 +128,16 @@ class Api:
def refuel(self, ship):
data = self.request('post', f'my/ships/{ship}/refuel')
ship.update(data)
if 'fuel' in data:
ship.update(data)
if 'agent' in data:
self.agent.update(data['agent'])
return data
def accept_contract(self, contract):
data = self.request('post', f'my/contracts/{contract.symbol.lower()}/accept')
if 'contract' in data:
contract.update(data['contract'])
if 'agent' in data:
self.agent.update(data['agent'])
return contract

View File

@ -41,7 +41,7 @@ class CommandLine:
print(f'command not found; {c}')
def handle_error(self, cmd, args, e):
logging.error(e, exc_info=type(e).__name__ !='ApiError')
logging.error(e, exc_info=type(e).__name__ not in ['ApiError','CommandError'])
def handle_empty(self):
pass

View File

@ -13,6 +13,9 @@ from time import sleep, time
from threading import Thread
from nullptr.atlas_builder import AtlasBuilder
class CommandError(Exception):
pass
class Commander(CommandLine):
def __init__(self, store_dir='data'):
self.store_dir = store_dir
@ -53,9 +56,19 @@ class Commander(CommandLine):
agent = next(agents, None)
if agent is None:
symbol = input('agent name: ')
agent = self.store.get(Agent, symbol)
agent = self.store.get(Agent, symbol, create=True)
return agent
def resolve(self, typ, arg):
arg = arg.upper()
matches = [c for c in self.store.all(typ) if c.symbol.startswith(arg)]
if len(matches) == 1:
return matches[0]
elif len(matches) > 1:
raise CommandError('multiple matches')
else:
raise CommandError('not found')
def after_cmd(self):
self.store.flush()
@ -150,7 +163,7 @@ class Commander(CommandLine):
if not self.has_ship(): return
system = self.ship.location().system()
symbol = f'{system}-{arg}'
dest = self.store.get('Waypoint', symbol)
dest = self.resolve('Waypoint', symbol)
self.api.navigate(self.ship, dest)
pprint(self.ship)
@ -169,3 +182,21 @@ class Commander(CommandLine):
r = self.api.negotiate(self.ship)
pprint(r)
def do_refuel(self):
if not self.has_ship(): return
r = self.api.refuel(self.ship)
pprint(self.ship)
def do_accept(self, c):
contract = self.resolve('Contract', c)
r = self.api.accept_contract(contract)
pprint(r)
def do_market(self, arg=''):
if arg == '':
if not self.has_ship(): return
waypoint = self.ship.location()
else:
waypoint = self.resolve('Waypoint', arg)
r = self.api.marketplace(waypoint)
pprint(r)

View File

@ -2,12 +2,11 @@
from time import time
from nullptr.util import *
from .base import Base
from typing import List
class Contract(Base):
identifier = 'id'
type: str
deliveries: List
deliveries: list
accepted: bool
fulfilled: bool
expires: int

View File

@ -1,10 +1,10 @@
from .system_member import SystemMember
from typing import List
from dataclasses import field
class Jumpgate(SystemMember):
range: int
faction: str
systems:List[str] = []
systems: list = field(default_factory=list)
def update(self, d):
self.setlst('systems', d, 'connectedSystems', 'symbol')

View File

@ -1,17 +1,24 @@
from .system_member import SystemMember
from typing import List
from time import time
from nullptr.util import *
from dataclasses import field
class Marketplace(SystemMember):
imports:List[str] = []
exports:List[str] = []
exchange:List[str] = []
imports:list = field(default_factory=list)
exports:list = field(default_factory=list)
exchange:list = field(default_factory=list)
prices:dict = field(default_factory=dict)
last_prices:int = 0
def update(self, d):
self.setlst('imports', d, 'imports', 'symbol')
self.setlst('exports', d, 'exports', 'symbol')
self.setlst('exchange', d, 'exchange', 'symbol')
if 'tradeGoods' in d:
self.last_prices = time()
for g in mg(d, 'tradeGoods'):
pass
@classmethod
def ext(self):
return 'mkt'

View File

@ -1,11 +1,11 @@
from .base import Base
from time import time
from nullptr.util import *
from dataclasses import dataclass
from dataclasses import dataclass, field
class Ship(Base):
cargo:dict = {}
mission_state:dict = {}
mission_state:dict = field(default_factory=dict)
status:str = ''
cargo_capacity:int = 0
cargo_units:int = 0

View File

@ -1,12 +1,12 @@
from .system_member import SystemMember
from nullptr.util import *
from typing import List
from dataclasses import field
class Waypoint(SystemMember):
x:int = 0
y:int = 0
type:str = 'unknown'
traits:List[str]=[]
traits:list = field(default_factory=list)
faction:str = ''
def update(self, d):