1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-13 07:47:14 +03:00

Export ship loadout to Coriolis in Journal Loadout format

This commit is contained in:
Jonathan Harris 2018-04-16 01:21:55 +01:00
parent 6dac1e33eb
commit f203b8bc7e
2 changed files with 25 additions and 49 deletions

View File

@ -19,29 +19,12 @@ def plugin_start():
return 'Coriolis'
# Return a URL for the current ship
def shipyard_url(loadout, is_beta, data=None):
def shipyard_url(loadout, is_beta):
string = json.dumps(loadout, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8') # most compact representation
if not string:
return False
# Ignore supplied loadout (except for validation) until Coriolis updates to 3.0. Use cAPI instead.
if not data:
try:
data = companion.session.profile()
except Exception as e:
if __debug__: print_exc()
plug.show_error(str(e))
return
if not data.get('commander', {}).get('name'):
plug.show_error(_("Who are you?!")) # Shouldn't happen
elif (not data.get('lastSystem', {}).get('name') or
(data['commander'].get('docked') and not data.get('lastStarport', {}).get('name'))): # Only care if docked
plug.show_error(_("Where are you?!")) # Shouldn't happen
elif not data.get('ship', {}).get('name') or not data.get('ship', {}).get('modules'):
plug.show_error(_("What are you flying?!")) # Shouldn't happen
elif (loadout.get('ShipID') is not None and data['ship']['id'] != loadout['ShipID']) or (loadout.get('Ship') and data['ship']['name'].lower() != loadout['Ship']):
plug.show_error(_('Error: Frontier server is lagging')) # Raised when Companion API server is returning old data, e.g. when the servers are too busy
else:
string = json.dumps(companion.ship(data), ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8') # most compact representation
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode='w') as f:
f.write(string)
return (is_beta and 'https://beta.coriolis.edcd.io/import?data=' or 'https://coriolis.edcd.io/import?data=') + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D')
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode='w') as f:
f.write(string)
return (is_beta and 'https://beta.coriolis.edcd.io/import?data=' or 'https://coriolis.edcd.io/import?data=') + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D')

View File

@ -30,8 +30,8 @@ this = sys.modules[__name__] # For holding module globals
this.session = requests.Session()
this.queue = Queue() # Items to be sent to EDSM by worker thread
this.discardedEvents = [] # List discarded events from EDSM
this.lastship = None # Description of last ship that we sent to EDSM
this.lastloadout = None # Description of last ship that we sent to EDSM
this.last_edsy = None # URL of last ship that we sent to EDSM
this.last_coriolis = None # URL of last ship that we sent to EDSM
this.lastlookup = False # whether the last lookup succeeded
# Game state
@ -242,13 +242,21 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
this.queue.put((cmdr, entry))
if entry['event'] == 'Loadout' and 'EDShipyard' not in this.discardedEvents:
url = plug.invoke('EDSY', None, 'shipyard_url', entry, is_beta)
if this.lastloadout != url:
this.lastloadout = url
this.queue.put((cmdr, {
'event': 'EDShipyard', 'timestamp': entry['timestamp'], '_shipId': state['ShipID'], 'url': this.lastloadout
}))
if entry['event'] == 'Loadout':
if 'EDShipyard' not in this.discardedEvents:
url = plug.invoke('EDSY', None, 'shipyard_url', entry, is_beta)
if this.last_edsy != url:
this.last_edsy = url
this.queue.put((cmdr, {
'event': 'EDShipyard', 'timestamp': entry['timestamp'], '_shipId': state['ShipID'], 'url': url
}))
if 'Coriolis' not in this.discardedEvents:
url = plug.invoke('Coriolis', None, 'shipyard_url', entry, is_beta)
if this.last_coriolis != url:
this.last_coriolis = url
this.queue.put((cmdr, {
'event': 'Coriolis', 'timestamp': entry['timestamp'], '_shipId': state['ShipID'], 'url': url
}))
# Update system data
@ -261,21 +269,6 @@ def cmdr_data(data, is_beta):
this.system['image'] = ''
this.system.update_idletasks()
# Send ship info to EDSM
if config.getint('edsm_out') and not is_beta and not this.multicrew and credentials(data['commander']['name']):
ship = companion.ship(data)
if ship != this.lastship:
cmdr = data['commander']['name']
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
if 'Coriolis' not in this.discardedEvents:
this.queue.put((cmdr, {
'event': 'Coriolis',
'timestamp': timestamp,
'_shipId': data['ship']['id'],
'url': plug.invoke('Coriolis', None, 'shipyard_url', {}, is_beta, data)
}))
this.lastship = ship
# Worker thread
def worker():