From 3f93d863a0a6f383127dd5394245965a244c07a8 Mon Sep 17 00:00:00 2001 From: Richard Bronkhorst Date: Fri, 16 Jun 2023 14:41:11 +0200 Subject: [PATCH] Update api.py, command_line.py and six other files --- nullptr/api.py | 13 ++++++++++++- nullptr/command_line.py | 2 +- nullptr/commander.py | 35 +++++++++++++++++++++++++++++++++-- nullptr/models/contract.py | 3 +-- nullptr/models/jumpgate.py | 4 ++-- nullptr/models/marketplace.py | 17 ++++++++++++----- nullptr/models/ship.py | 4 ++-- nullptr/models/waypoint.py | 4 ++-- 8 files changed, 65 insertions(+), 17 deletions(-) diff --git a/nullptr/api.py b/nullptr/api.py index a2de79f..f96c012 100644 --- a/nullptr/api.py +++ b/nullptr/api.py @@ -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 diff --git a/nullptr/command_line.py b/nullptr/command_line.py index 389e86d..cc85563 100644 --- a/nullptr/command_line.py +++ b/nullptr/command_line.py @@ -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 diff --git a/nullptr/commander.py b/nullptr/commander.py index c618ee3..648ade8 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -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,8 +56,18 @@ 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) diff --git a/nullptr/models/contract.py b/nullptr/models/contract.py index 2e29d44..419648c 100644 --- a/nullptr/models/contract.py +++ b/nullptr/models/contract.py @@ -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 diff --git a/nullptr/models/jumpgate.py b/nullptr/models/jumpgate.py index 760136a..d2ee8f9 100644 --- a/nullptr/models/jumpgate.py +++ b/nullptr/models/jumpgate.py @@ -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') diff --git a/nullptr/models/marketplace.py b/nullptr/models/marketplace.py index dd5dbef..79eee6d 100644 --- a/nullptr/models/marketplace.py +++ b/nullptr/models/marketplace.py @@ -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' diff --git a/nullptr/models/ship.py b/nullptr/models/ship.py index 8eeba11..3cfcb0c 100644 --- a/nullptr/models/ship.py +++ b/nullptr/models/ship.py @@ -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 diff --git a/nullptr/models/waypoint.py b/nullptr/models/waypoint.py index fcf6e17..c867fad 100644 --- a/nullptr/models/waypoint.py +++ b/nullptr/models/waypoint.py @@ -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):