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): def refuel(self, ship):
data = self.request('post', f'my/ships/{ship}/refuel') data = self.request('post', f'my/ships/{ship}/refuel')
if 'fuel' in data:
ship.update(data) ship.update(data)
if 'agent' in data:
self.agent.update(data['agent'])
return data 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}') print(f'command not found; {c}')
def handle_error(self, cmd, args, e): 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): def handle_empty(self):
pass pass

View File

@ -13,6 +13,9 @@ from time import sleep, time
from threading import Thread from threading import Thread
from nullptr.atlas_builder import AtlasBuilder from nullptr.atlas_builder import AtlasBuilder
class CommandError(Exception):
pass
class Commander(CommandLine): class Commander(CommandLine):
def __init__(self, store_dir='data'): def __init__(self, store_dir='data'):
self.store_dir = store_dir self.store_dir = store_dir
@ -53,9 +56,19 @@ class Commander(CommandLine):
agent = next(agents, None) agent = next(agents, None)
if agent is None: if agent is None:
symbol = input('agent name: ') symbol = input('agent name: ')
agent = self.store.get(Agent, symbol) agent = self.store.get(Agent, symbol, create=True)
return agent 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): def after_cmd(self):
self.store.flush() self.store.flush()
@ -150,7 +163,7 @@ class Commander(CommandLine):
if not self.has_ship(): return if not self.has_ship(): return
system = self.ship.location().system() system = self.ship.location().system()
symbol = f'{system}-{arg}' symbol = f'{system}-{arg}'
dest = self.store.get('Waypoint', symbol) dest = self.resolve('Waypoint', symbol)
self.api.navigate(self.ship, dest) self.api.navigate(self.ship, dest)
pprint(self.ship) pprint(self.ship)
@ -169,3 +182,21 @@ class Commander(CommandLine):
r = self.api.negotiate(self.ship) r = self.api.negotiate(self.ship)
pprint(r) 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 time import time
from nullptr.util import * from nullptr.util import *
from .base import Base from .base import Base
from typing import List
class Contract(Base): class Contract(Base):
identifier = 'id' identifier = 'id'
type: str type: str
deliveries: List deliveries: list
accepted: bool accepted: bool
fulfilled: bool fulfilled: bool
expires: int expires: int

View File

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

View File

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

View File

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

View File

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