1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-06 10:23:06 +03:00

Send unset events on exit or on switching Cmdr

This commit is contained in:
Jonathan Harris 2017-11-06 22:18:10 +00:00
parent 526c790d29
commit ad9885db98
2 changed files with 39 additions and 29 deletions

View File

@ -76,7 +76,7 @@ def plugin_app(parent):
this.system.bind_all('<<EDSMStatus>>', update_status) this.system.bind_all('<<EDSMStatus>>', update_status)
return (this.system_label, this.system) return (this.system_label, this.system)
def plugin_close(): def plugin_stop():
# Signal thread to close and wait for it # Signal thread to close and wait for it
this.queue.put(None) this.queue.put(None)
this.thread.join() this.thread.join()

View File

@ -34,8 +34,9 @@ this.queue = Queue() # Items to be sent to Inara by worker thread
# Cached Cmdr state # Cached Cmdr state
this.events = [] # Unsent events this.events = [] # Unsent events
this.cmdr = None
this.multicrew = False # don't send captain's ship info to Inara while on a crew this.multicrew = False # don't send captain's ship info to Inara while on a crew
this.location = None this.started_docked = False # Skip Docked event after Location if started docked
this.cargo = None this.cargo = None
this.materials = None this.materials = None
this.needcredits = True # Send credit update soon after Startup / new game this.needcredits = True # Send credit update soon after Startup / new game
@ -46,7 +47,10 @@ def plugin_start():
this.thread.start() this.thread.start()
return 'Inara' return 'Inara'
def plugin_close(): def plugin_stop():
# Send any unsent events
if this.events:
call()
# Signal thread to close and wait for it # Signal thread to close and wait for it
this.queue.put(None) this.queue.put(None)
this.thread.join() this.thread.join()
@ -124,12 +128,17 @@ def credentials(cmdr):
def journal_entry(cmdr, is_beta, system, station, entry, state): def journal_entry(cmdr, is_beta, system, station, entry, state):
# Send any unsent events when switching accounts
if cmdr and cmdr != this.cmdr and this.events:
call()
this.cmdr = cmdr
this.multicrew = bool(state['Role']) this.multicrew = bool(state['Role'])
if entry['event'] == 'LoadGame': if entry['event'] == 'LoadGame':
# clear cached state # clear cached state
this.events = [] this.events = []
this.location = None this.started_docked = False
this.cargo = None this.cargo = None
this.materials = None this.materials = None
this.needcredits = True this.needcredits = True
@ -202,43 +211,43 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
# Update location # Update location
if entry['event'] == 'Location': if entry['event'] == 'Location':
if entry.get('Docked'): this.started_docked = entry.get('Docked')
if this.started_docked:
add_event('setCommanderTravelLocation', entry['timestamp'], add_event('setCommanderTravelLocation', entry['timestamp'],
OrderedDict([ OrderedDict([
('starsystemName', entry['StarSystem']), ('starsystemName', system),
('stationName', entry['StationName']), ('stationName', station),
('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])), ('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])),
('shipGameID', state['ShipID']), ('shipGameID', state['ShipID']),
])) ]))
this.location = (entry['StarSystem'], entry['StationName'])
else: else:
add_event('setCommanderTravelLocation', entry['timestamp'], add_event('setCommanderTravelLocation', entry['timestamp'],
OrderedDict([ OrderedDict([
('starsystemName', entry['StarSystem']), ('starsystemName', system),
('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])), ('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])),
('shipGameID', state['ShipID']), ('shipGameID', state['ShipID']),
])) ]))
this.location = (entry['StarSystem'], None)
elif entry['event'] == 'Docked' and this.location != (entry['StarSystem'], entry['StationName']): elif entry['event'] == 'Docked':
# Don't send docked event on new game - i.e. following 'Location' event if this.started_docked:
# Don't send Docked event on new game - i.e. following 'Location' event
this.started_docked = False
else:
add_event('addCommanderTravelDock', entry['timestamp'], add_event('addCommanderTravelDock', entry['timestamp'],
OrderedDict([ OrderedDict([
('starsystemName', entry['StarSystem']), ('starsystemName', system),
('stationName', entry['StationName']), ('stationName', station),
('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])), ('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])),
('shipGameID', state['ShipID']), ('shipGameID', state['ShipID']),
])) ]))
this.location = (entry['StarSystem'], entry['StationName'])
elif entry['event'] == 'Undocked' and this.location: elif entry['event'] == 'Undocked':
add_event('setCommanderTravelLocation', entry['timestamp'], add_event('setCommanderTravelLocation', entry['timestamp'],
OrderedDict([ OrderedDict([
('starsystemName', this.location[0]), ('starsystemName', system),
('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])), ('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])),
('shipGameID', state['ShipID']), ('shipGameID', state['ShipID']),
])) ]))
this.location = (this.location[0], None)
elif entry['event'] == 'FSDJump': elif entry['event'] == 'FSDJump':
add_event('addCommanderTravelFSDJump', entry['timestamp'], add_event('addCommanderTravelFSDJump', entry['timestamp'],
@ -248,7 +257,6 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])), ('shipType', companion.ship_map.get(state['ShipType'], state['ShipType'])),
('shipGameID', state['ShipID']), ('shipGameID', state['ShipID']),
])) ]))
this.location = (entry['StarSystem'], None)
if len(this.events) > old_events: if len(this.events) > old_events:
# We have new event(s) so send to Inara # We have new event(s) so send to Inara
@ -266,7 +274,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
this.materials = materials this.materials = materials
# Queue a call to Inara # Queue a call to Inara
call(cmdr) call()
except Exception as e: except Exception as e:
if __debug__: print_exc() if __debug__: print_exc()
@ -275,7 +283,9 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
def cmdr_data(data, is_beta): def cmdr_data(data, is_beta):
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(data['commander']['name']): this.cmdr = data['commander']['name']
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(this.cmdr):
if this.needcredits: if this.needcredits:
assets = (data['commander']['credits'] + assets = (data['commander']['credits'] +
@ -302,14 +312,14 @@ def add_event(name, timestamp, data):
# Queue a call to Inara, handled in Worker thread # Queue a call to Inara, handled in Worker thread
def call(cmdr, callback=None): def call(callback=None):
data = json.dumps(OrderedDict([ data = json.dumps(OrderedDict([
('header', OrderedDict([ ('header', OrderedDict([
('appName', applongname), ('appName', applongname),
('appVersion', appversion), ('appVersion', appversion),
('isDeveloped', True), # TODO: Remove before release ('isDeveloped', True), # TODO: Remove before release
('APIkey', credentials(cmdr)), ('APIkey', credentials(this.cmdr)),
('commanderName', cmdr.encode('utf-8')), ('commanderName', this.cmdr.encode('utf-8')),
])), ])),
('events', this.events), ('events', this.events),
]), separators = (',', ':')) ]), separators = (',', ':'))