1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-08-25 18:33:44 +03:00

Notify plugins of Inara ship, system and station IDs

Fixes #425
This commit is contained in:
Jonathan Harris 2019-07-06 15:28:48 +01:00
parent cd73383986
commit d8b5884b05
2 changed files with 82 additions and 11 deletions

View File

@ -146,14 +146,61 @@ This gets called when EDMC has just fetched fresh Cmdr and station data from Fro
```python
def cmdr_data(data, is_beta):
"""
We have new data on our commander
"""
sys.stderr.write(data.get('commander') and data.get('commander').get('name') or '')
"""
We have new data on our commander
"""
sys.stderr.write(data.get('commander') and data.get('commander').get('name') or '')
```
The data is a dictionary and full of lots of wonderful stuff!
### Plugin-specific events
If the player has chosen to "Send flight log and Cmdr status to EDSM" this gets called when the player starts the game or enters a new system. It is called some time after the corresponding `journal_entry()` event.
```python
def edsm_notify_system(reply):
"""
`reply` holds the response from a call to https://www.edsm.net/en/api-journal-v1
"""
if not reply:
sys.stderr.write("Error: Can't connect to EDSM\n")
elif reply['msgnum'] // 100 not in (1,4):
sys.stderr.write('Error: EDSM {MSG}\n').format(MSG=reply['msg'])
elif reply.get('systemCreated'):
sys.stderr.write('New EDSM system!\n')
else:
sys.stderr.write('Known EDSM system\n')
```
If the player has chosen to "Send flight log and Cmdr status to Inara" this gets called when the player starts the game, enters a new system, docks or undocks. It is called some time after the corresponding `journal_entry()` event.
```python
def inara_notify_location(eventData):
"""
`eventData` holds the response to one of the "Commander's Flight Log" events https://inara.cz/inara-api-docs/#event-29
"""
if eventData.get('starsystemInaraID'):
sys.stderr.write('Now in Inara system {ID} at {URL}\n'.format(ID=eventData['starsystemInaraID'], URL=eventData['starsystemInaraURL']))
else:
sys.stderr.write('System not known to Inara\n')
if eventData.get('stationInaraID'):
sys.stderr.write('Docked at Inara station {ID} at {URL}\n'.format(ID=eventData['stationInaraID'], URL=eventData['stationInaraURL']))
else:
sys.stderr.write('Undocked or station unknown to Inara\n')
```
If the player has chosen to "Send flight log and Cmdr status to Inara" this gets called when the player starts the game or switches ship. It is called some time after the corresponding `journal_entry()` event.
```python
def inara_notify_ship(eventData):
"""
`eventData` holds the response to an addCommanderShip or setCommanderShip event https://inara.cz/inara-api-docs/#event-11
"""
if eventData.get('shipInaraID'):
sys.stderr.write('Now in Inara ship {ID} at {URL}\n'.format(ID=eventData['shipInaraID'], URL=eventData['shipInaraURL']))
```
## Error messages
You can display an error in EDMC's status area by returning a string from your `journal_entry()`, `dashboard_entry()` or `cmdr_data()` function, or asynchronously (e.g. from a "worker" thread that is performing a long running operation) by calling `plug.show_error()`. Either method will cause the "bad" sound to be played (unless the user has muted sound).

View File

@ -30,6 +30,8 @@ CREDIT_RATIO = 1.05 # Update credits if they change by 5% over the course of a
this = sys.modules[__name__] # For holding module globals
this.session = requests.Session()
this.queue = Queue() # Items to be sent to Inara by worker thread
this.lastlocation = None # eventData from the last Commander's Flight Log event
this.lastship = None # eventData from the last addCommanderShip or setCommanderShip event
# Cached Cmdr state
this.events = [] # Unsent events
@ -71,6 +73,8 @@ def plugin_start():
def plugin_app(parent):
this.system_link = parent.children['system'] # system label in main window
this.station_link = parent.children['station'] # station label in main window
this.system_link.bind_all('<<InaraLocation>>', update_location)
this.system_link.bind_all('<<InaraShip>>', update_ship)
def plugin_stop():
# Send any unsent events
@ -818,13 +822,12 @@ def worker():
if reply_event['eventStatus'] // 100 != 2:
plug.show_error(_('Error: Inara {MSG}').format(MSG = '%s, %s' % (data_event['eventName'], reply_event.get('eventStatusText', reply_event['eventStatus']))))
if data_event['eventName'] in ['addCommanderTravelDock', 'addCommanderTravelFSDJump', 'setCommanderTravelLocation']:
eventData = reply_event.get('eventData', {})
this.system = eventData.get('starsystemInaraURL')
if config.get('system_provider') == 'Inara':
this.system_link['url'] = this.system # Override standard URL function
this.station = eventData.get('stationInaraURL')
if config.get('station_provider') == 'Inara':
this.station_link['url'] = this.station or this.system # Override standard URL function
this.lastlocation = reply_event.get('eventData', {})
this.system_link.event_generate('<<InaraLocation>>', when="tail") # calls update_location in main thread
elif data_event['eventName'] in ['addCommanderShip', 'setCommanderShip']:
this.lastship = reply_event.get('eventData', {})
this.system_link.event_generate('<<InaraShip>>', when="tail") # calls update_ship in main thread
break
except:
if __debug__: print_exc()
@ -834,3 +837,24 @@ def worker():
callback(None)
else:
plug.show_error(_("Error: Can't connect to Inara"))
# Call inara_notify_location() in this and other interested plugins with Inara's response when changing system or station
def update_location(event=None):
if this.lastlocation:
for plugin in plug.provides('inara_notify_location'):
plug.invoke(plugin, None, 'inara_notify_location', this.lastlocation)
def inara_notify_location(eventData):
this.system = eventData.get('starsystemInaraURL')
if config.get('system_provider') == 'Inara':
this.system_link['url'] = this.system # Override standard URL function
this.station = eventData.get('stationInaraURL')
if config.get('station_provider') == 'Inara':
this.station_link['url'] = this.station or this.system # Override standard URL function
# Call inara_notify_ship() in interested plugins with Inara's response when changing ship
def update_ship(event=None):
if this.lastship:
for plugin in plug.provides('inara_notify_ship'):
plug.invoke(plugin, None, 'inara_notify_ship', this.lastship)