implementing api changes since 5 months ago

This commit is contained in:
Richard 2023-12-28 19:49:00 +01:00
parent 1ba10260c0
commit a287897da9
9 changed files with 174 additions and 87 deletions

View File

@ -52,6 +52,7 @@ class Analyzer:
possibles = sorted(candidates, key=lambda m: m[2]) possibles = sorted(candidates, key=lambda m: m[2])
possibles = possibles[:10] possibles = possibles[:10]
results = [] results = []
print(len(possibles))
for typ,m,d in possibles: for typ,m,d in possibles:
system = m.waypoint.system system = m.waypoint.system
p = self.find_path(origin, system) p = self.find_path(origin, system)

View File

@ -61,6 +61,7 @@ class Api:
self.last_error = 0 self.last_error = 0
return result['data'] return result['data']
######## Account #########
def register(self, faction): def register(self, faction):
callsign = self.agent.symbol callsign = self.agent.symbol
data = { data = {
@ -77,6 +78,7 @@ class Api:
self.agent.update(data) self.agent.update(data)
return self.agent return self.agent
######## Atlas #########
def list_systems(self, page=1): def list_systems(self, page=1):
data = self.request('get', 'systems', params={'page': page}) data = self.request('get', 'systems', params={'page': page})
#pprint(self.last_meta) #pprint(self.last_meta)
@ -87,6 +89,9 @@ class Api:
def list_waypoints(self, system): def list_waypoints(self, system):
data = self.request('get', f'systems/{system}/waypoints/') data = self.request('get', f'systems/{system}/waypoints/')
tp = total_pages(self.last_meta)
for p in range(1, tp):
data += self.request('get', f'systems/{system}/waypoints/', params={'page': p})
# pprint(data) # pprint(data)
return self.store.update_list(Waypoint, data) return self.store.update_list(Waypoint, data)
@ -100,10 +105,32 @@ class Api:
symbol = str(waypoint) symbol = str(waypoint)
return self.store.update(Jumpgate, data, symbol) return self.store.update(Jumpgate, data, symbol)
def shipyard(self, wp):
return self.request('get', f'systems/{wp.system}/waypoints/{wp}/shipyard')
######## Fleet #########
def list_ships(self): def list_ships(self):
data = self.request('get', 'my/ships') data = self.request('get', 'my/ships')
return self.store.update_list(Ship, data) return self.store.update_list(Ship, data)
def refuel(self, ship, from_cargo=False):
fuel_need = ship.fuel_capacity - ship.fuel_current
fuel_avail = ship.get_cargo('FUEL') * 100
units = fuel_need
if from_cargo:
units = min(units, fuel_avail)
data = {'fromCargo': from_cargo, 'units': units }
data = self.request('post', f'my/ships/{ship}/refuel', data)
if from_cargo:
boxes = ceil(float(units) / 100)
ship.take_cargo('FUEL', boxes)
if 'fuel' in data:
ship.update(data)
if 'agent' in data:
self.agent.update(data['agent'])
return data
######## Contract #########
def list_contracts(self): def list_contracts(self):
data = self.request('get', 'my/contracts') data = self.request('get', 'my/contracts')
return self.store.update_list('Contract', data) return self.store.update_list('Contract', data)
@ -114,6 +141,14 @@ class Api:
contract = self.store.update('Contract', data['contract']) contract = self.store.update('Contract', data['contract'])
return contract return contract
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
def deliver(self, ship, typ, contract): def deliver(self, ship, typ, contract):
units = ship.get_cargo(typ) units = ship.get_cargo(typ)
if units == 0: if units == 0:
@ -139,6 +174,7 @@ class Api:
self.agent.update(data['agent']) self.agent.update(data['agent'])
return contract return contract
######## Nav #########
def navigate(self, ship, wp): def navigate(self, ship, wp):
data = {'waypointSymbol': str(wp)} data = {'waypointSymbol': str(wp)}
response = self.request('post', f'my/ships/{ship}/navigate', data) response = self.request('post', f'my/ships/{ship}/navigate', data)
@ -154,22 +190,51 @@ class Api:
ship.update(data) ship.update(data)
return data return data
def refuel(self, ship): def flight_mode(self, ship, mode):
data = self.request('post', f'my/ships/{ship}/refuel') data = {'flightMode': mode}
if 'fuel' in data: data = self.request('patch', f'my/ships/{ship}/nav', data)
ship.update(data)
if 'agent' in data:
self.agent.update(data['agent'])
return data return data
def accept_contract(self, contract): def jump(self, ship, waypoint):
data = self.request('post', f'my/contracts/{contract.symbol.lower()}/accept') if type(waypoint) == Waypoint:
if 'contract' in data: waypoint = waypoint.symbol
contract.update(data['contract']) data = {
if 'agent' in data: "waypointSymbol": waypoint
self.agent.update(data['agent']) }
return contract data = self.request('post', f'my/ships/{ship}/jump', data)
if 'nav' in data:
ship.update(data)
return ship
######## Extraction #########
def siphon(self, ship):
data = self.request('post', f'my/ships/{ship}/siphon')
ship.update(data)
return data['siphon']
def extract(self, ship, survey=None):
data = {}
url = f'my/ships/{ship}/extract'
if survey is not None:
data['survey'] = survey.api_dict()
url += '/survey'
try:
data = self.request('post', url, data=data)
except ApiError as e:
if e.code in [ 4221, 4224]:
survey.exhausted = True
else:
raise e
ship.update(data)
return data
def survey(self, ship):
data = self.request('post', f'my/ships/{ship}/survey')
ship.update(data)
result = self.store.update_list('Survey', mg(data, 'surveys'))
return result
######## Commerce #########
def sell(self, ship, typ): def sell(self, ship, typ):
units = ship.get_cargo(typ) units = ship.get_cargo(typ)
data = { data = {
@ -222,40 +287,3 @@ class Api:
if 'ship' in data: if 'ship' in data:
ship = self.store.update('Ship', data['ship']) ship = self.store.update('Ship', data['ship'])
return ship return ship
def jump(self, ship, system):
if type(system) == System:
system = system.symbol
data = {
"systemSymbol": system
}
data = self.request('post', f'my/ships/{ship}/jump', data)
if 'nav' in data:
ship.update(data)
return ship
def shipyard(self, wp):
return self.request('get', f'systems/{wp.system}/waypoints/{wp}/shipyard')
def extract(self, ship, survey=None):
data = {}
url = f'my/ships/{ship}/extract'
if survey is not None:
data['survey'] = survey.api_dict()
url += '/survey'
try:
data = self.request('post', url, data=data)
except ApiError as e:
if e.code in [ 4221, 4224]:
survey.exhausted = True
else:
raise e
ship.update(data)
return data
def survey(self, ship):
data = self.request('post', f'my/ships/{ship}/survey')
ship.update(data)
result = self.store.update_list('Survey', mg(data, 'surveys'))
return result

View File

@ -63,10 +63,10 @@ class AtlasBuilder:
if 'UNCHARTED' in w.traits: if 'UNCHARTED' in w.traits:
continue continue
if 'MARKETPLACE' in w.traits: if 'MARKETPLACE' in w.traits:
# print(f'marketplace at {w}') print(f'marketplace at {w}')
self.sched(self.api.marketplace, w) self.sched(self.api.marketplace, w)
if w.type == 'JUMP_GATE': if w.type == 'JUMP_GATE':
# print(f'jumpgate at {w}') print(f'jumpgate at {w}')
self.sched(self.api.jumps, w) self.sched(self.api.jumps, w)
if 'SHIPYARD' in w.traits: if 'SHIPYARD' in w.traits:
# todo # todo

View File

@ -68,8 +68,11 @@ class Commander(CommandLine):
self.agent = agent self.agent = agent
api = Api(self.store, agent) api = Api(self.store, agent)
self.api = api self.api = api
faction = input('faction: ') faction = input('faction or token: ')
api.register(faction.upper().strip()) if len(faction) > 50:
self.agent.token = faction
else:
api.register(faction.upper().strip())
print('=== agent:') print('=== agent:')
print(agent) print(agent)
print('=== ships') print('=== ships')
@ -226,15 +229,32 @@ class Commander(CommandLine):
r = self.store.all_members(system, 'Waypoint') r = self.store.all_members(system, 'Waypoint')
for w in r: for w in r:
traits = [] traits = []
if 'MARKETPLACE' in w.traits:
traits.append('MARKET')
if 'SHIPYARD' in w.traits:
traits.append('SHIPYARD')
if w.type == 'JUMP_GATE': if w.type == 'JUMP_GATE':
traits.append('JUMP') traits.append('JUMP')
if w.type == 'ASTEROID_FIELD': if 'SHIPYARD' in w.traits:
traits.append('ASTROIDS') traits.append('SHIPYARD')
print(w.symbol.split('-')[2], ', '.join(traits)) if 'MARKETPLACE' in w.traits:
traits.append('MARKET')
if w.type == 'ASTEROID':
if 'COMMON_METAL_DEPOSITS' in w.traits:
traits.append('METAL')
if 'PRECIOUS_METAL_DEPOSITS' in w.traits:
traits.append('GOLD')
if 'EXPLOSIVE_GASSES' in w.traits:
traits.append('GAS')
if 'MINERAL_DEPOSITS' in w.traits:
traits.append('MINS')
if 'STRIPPED' in w.traits:
traits.append('STRIPPED')
wname = w.symbol.split('-')[2]
traits = ', '.join(traits)
typ = w.type[0]
if typ not in ['F','J'] and len(traits) == 0:
continue
print(f'{wname:4} {typ} {traits}')
def do_wp(self, s=''): def do_wp(self, s=''):
self.do_waypoints(s) self.do_waypoints(s)
@ -243,6 +263,10 @@ class Commander(CommandLine):
waypoint = self.store.get(Waypoint, waypoint_str.upper()) waypoint = self.store.get(Waypoint, waypoint_str.upper())
r = self.api.marketplace(waypoint) r = self.api.marketplace(waypoint)
def do_atlas(self):
atlas = self.store.get(Atlas, 'ATLAS')
pprint(atlas, 5)
def do_jumps(self, waypoint_str=None): def do_jumps(self, waypoint_str=None):
if waypoint_str is None: if waypoint_str is None:
if not self.has_ship(): return if not self.has_ship(): return
@ -332,15 +356,32 @@ class Commander(CommandLine):
self.api.orbit(self.ship) self.api.orbit(self.ship)
pprint(self.ship) pprint(self.ship)
def do_siphon(self):
if not self.has_ship(): return
data = self.api.siphon(self.ship)
pprint(data)
def do_negotiate(self): def do_negotiate(self):
if not self.has_ship(): return if not self.has_ship(): return
r = self.api.negotiate(self.ship) r = self.api.negotiate(self.ship)
pprint(r) pprint(r)
def do_refuel(self): def do_token(self):
print(self.agent.token)
def do_speed(self, speed):
if not self.has_ship(): return if not self.has_ship(): return
r = self.api.refuel(self.ship) speed = speed.upper()
pprint(self.ship) speeds = ['DRIFT', 'STEALTH','CRUISE','BURN']
if speed not in speeds:
print('please choose from:', speeds)
self.api.flight_mode(self.ship, speed)
def do_refuel(self, source='market'):
if not self.has_ship(): return
from_cargo = source != 'market'
r = self.api.refuel(self.ship, from_cargo=from_cargo)
pprint(r)
def do_accept(self, c): def do_accept(self, c):
contract = self.resolve('Contract', c) contract = self.resolve('Contract', c)
@ -358,6 +399,7 @@ class Commander(CommandLine):
def do_cargo(self): def do_cargo(self):
if not self.has_ship(): return if not self.has_ship(): return
print(f'== Cargo {self.ship.cargo_units}/{self.ship.cargo_capacity} ==')
for c, units in self.ship.cargo.items(): for c, units in self.ship.cargo.items():
print(f'{units:4d} {c}') print(f'{units:4d} {c}')
@ -385,13 +427,11 @@ class Commander(CommandLine):
for s in must_get(data, 'ships'): for s in must_get(data, 'ships'):
print(s['type'], s['purchasePrice']) print(s['type'], s['purchasePrice'])
def do_jump(self, system_str): def do_jump(self, waypoint_str):
if not self.has_ship(): return if not self.has_ship(): return
if '-' not in system_str:
sector = self.ship.location.system.sector.symbol w = self.resolve('Waypoint', waypoint_str)
system_str = f'{sector}-{system_str}' self.api.jump(self.ship, w)
system = self.resolve('System', system_str)
self.api.jump(self.ship, system)
pprint(self.ship) pprint(self.ship)
def do_purchase(self, ship_type): def do_purchase(self, ship_type):

View File

@ -9,3 +9,8 @@ class Atlas(Base):
self.total_pages = 0 self.total_pages = 0
self.seen_pages = 0 self.seen_pages = 0
def f(self, detail=1):
r = super().f(detail)
if detail >2:
r += f' {self.seen_pages}/{self.total_pages}'
return r

View File

@ -57,15 +57,16 @@ class Base:
val = interp(val) val = interp(val)
setattr(self, attr, val) setattr(self, attr, val)
def setlst(self, attr, d, name, member, interp=None): def setlst(self, attr, d, name, member=None, interp=None):
val = sg(d, name) val = sg(d, name)
if val is not None: if val is not None:
lst = [] lst = []
for x in val: for x in val:
val = sg(x, member) if member is not None:
x = sg(x, member)
if interp is not None: if interp is not None:
val = interp(val) x = interp(x)
lst.append(val) lst.append(x)
setattr(self, attr, lst) setattr(self, attr, lst)
def __setattr__(self, name, value): def __setattr__(self, name, value):

View File

@ -1,19 +1,15 @@
from .base import Base from .base import Base
from .system import System from .waypoint import Waypoint
from dataclasses import field from dataclasses import field
class Jumpgate(Base): class Jumpgate(Base):
def define(self): def define(self):
self.range: int = 0 self.connections: list = []
self.faction: str = ''
self.systems: list = []
self.system = self.get_system() self.system = self.get_system()
def update(self, d): def update(self, d):
getter = self.store.getter(System, create=True) getter = self.store.getter(Waypoint, create=True)
self.setlst('systems', d, 'connectedSystems', 'symbol', interp=getter) self.setlst('connections', d, 'connections', interp=getter)
self.seta('faction', d, 'factionSymbol')
self.seta('range', d, 'jumpRange')
@classmethod @classmethod
def ext(self): def ext(self):
@ -23,5 +19,5 @@ class Jumpgate(Base):
r = self.symbol r = self.symbol
if detail > 1: if detail > 1:
r += '\n' r += '\n'
r += '\n'.join([s.symbol for s in self.systems]) r += '\n'.join([s.symbol for s in self.connections])
return r return r

View File

@ -55,12 +55,25 @@ class Ship(Base):
return 0 return 0
return self.cargo[typ] return self.cargo[typ]
def take_cargo(self, typ, amt):
if typ not in self.cargo:
return
if self.cargo[typ] <= amt:
del self.cargo[typ]
else:
self.cargo[typ] -= amt
self.cargo_units = sum(self.cargo.values())
def load_cargo(self, cargo): def load_cargo(self, cargo):
result = {} result = {}
total = 0
for i in cargo: for i in cargo:
symbol = must_get(i, 'symbol') symbol = must_get(i, 'symbol')
units = must_get(i, 'units') units = must_get(i, 'units')
result[symbol] = units result[symbol] = units
total += units
self.cargo_units = total
self.cargo = result self.cargo = result
def deliverable_cargo(self, contract): def deliverable_cargo(self, contract):

View File

@ -10,14 +10,17 @@ class Waypoint(Base):
self.type:str = 'unknown' self.type:str = 'unknown'
self.traits:list = [] self.traits:list = []
self.faction:str = '' self.faction:str = ''
self.is_under_construction:bool = False
self.system = self.get_system() self.system = self.get_system()
self.uncharted = True self.uncharted = True
def update(self, d): def update(self, d):
self.seta('x', d) self.seta('x', d)
self.seta('y', d) self.seta('y', d)
self.seta('type', d) self.seta('type', d)
self.seta('faction', d, 'faction.symbol') self.seta('faction', d, 'faction.symbol')
self.seta('is_under_construction', d, 'isUnderConstruction')
self.setlst('traits', d, 'traits', 'symbol') self.setlst('traits', d, 'traits', 'symbol')
self.uncharted = 'UNCHARTED' in self.traits self.uncharted = 'UNCHARTED' in self.traits