diff --git a/plugins/inara.py b/plugins/inara.py index 761c78eb..a72246e9 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -21,6 +21,8 @@ import plug if __debug__: from traceback import print_exc +STATION_UNDOCKED: str = u'×' # "Station" name to display when not docked = U+00D7 + _TIMEOUT = 20 FAKE = ['CQC', 'Training', 'Destination'] # Fake systems that shouldn't be sent to Inara CREDIT_RATIO = 1.05 # Update credits if they change by 5% over the course of a session @@ -56,13 +58,20 @@ FLOOD_LIMIT_SECONDS = 45 # minimum time between sending non-major cargo trigger # Main window clicks this.system_link = None this.system = None +this.system_address = None +this.system_population = None this.station_link = None this.station = None +this.station_marketid = None def system_url(system_name): - if system_name: - # TODO: Switch this to https://inara.cz/galaxy-starsystem/?search=3932277478106 + if this.system_address: + # TODO: Switch this to https://inara.cz/galaxy-starsystem/?search=3932277478106 + return requests.utils.requote_uri(f'https://inara.cz/galaxy-starsystem/?search={this.system_address}') + + elif system_name: return requests.utils.requote_uri(f'https://inara.cz/galaxy-starsystem/?search={system_name}') + return this.system def station_url(system_name, station_name): @@ -140,9 +149,9 @@ def prefs_changed(cmdr, is_beta): # Override standard URL functions if config.get('system_provider') == 'Inara': - this.system_link['url'] = this.system + this.system_link['url'] = system_url(this.system) if config.get('station_provider') == 'Inara': - this.station_link['url'] = this.station or this.system + this.station_link['url'] = station_url(this.system, this.station) if cmdr and not is_beta: this.cmdr = cmdr @@ -206,13 +215,25 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): this.fleet = None this.shipswap = False this.system = None + this.system_address = None this.station = None + this.station_marketid = None elif entry['event'] in ['Resurrect', 'ShipyardBuy', 'ShipyardSell', 'SellShipOnRebuy']: # Events that mean a significant change in credits so we should send credits after next "Update" this.lastcredits = 0 elif entry['event'] in ['ShipyardNew', 'ShipyardSwap'] or (entry['event'] == 'Location' and entry['Docked']): this.suppress_docked = True + # Always update, even if we're not the *current* system or station provider. + this.system_address = entry.get('SystemAddress') or this.system_address + this.system = entry.get('StarSystem') or this.system + this.system_population = entry.get('Population') or this.system_population + this.station = entry.get('StationName') or this.station + this.station_marketid = entry.get('MarketID') or this.station_marketid + # We might pick up StationName in DockingRequested, make sure we clear it if leaving + if entry['event'] in ('Undocked', 'FSDJump', 'SupercruiseEntry'): + this.station = None + this.station_marketid = None # Send location and status on new game or StartUp. Assumes Cargo is the last event on a new game (other than Docked). # Always send an update on Docked, FSDJump, Undocked+SuperCruise, Promotion, EngineerProgress and PowerPlay affiliation. @@ -370,7 +391,6 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): this.undocked = False elif entry['event'] == 'FSDJump': this.undocked = False - this.system = None add_event('addCommanderTravelFSDJump', entry['timestamp'], OrderedDict([ ('starsystemName', entry['StarSystem']), @@ -388,7 +408,6 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): ]) for f in entry['Factions'] ]) elif entry['event'] == 'CarrierJump': - this.system = None add_event('addCommanderTravelCarrierJump', entry['timestamp'], OrderedDict([ ('starsystemName', entry['StarSystem']), @@ -408,13 +427,6 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): # Ignore the following 'Docked' event this.suppress_docked = True - # Override standard URL functions - if config.get('system_provider') == 'Inara': - #this.system_link['text'] = this.system - this.system_link['url'] = this.system - if config.get('station_provider') == 'Inara': - this.station_link['text'] = this.station - this.station_link['url'] = this.station or this.system cargo = [OrderedDict([('itemName', k), ('itemCount', state['Cargo'][k])]) for k in sorted(state['Cargo'])] @@ -737,19 +749,40 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): this.newuser = False -def cmdr_data(data, is_beta): + # Only actually change URLs if we are current provider. + if config.get('system_provider') == 'Inara': + this.system_link['text'] = this.system + this.system_link['url'] = system_url(this.system) + this.system_link.update_idletasks() + if config.get('station_provider') == 'Inara': + this.station_link['text'] = this.station or (this.system_population and this.system_population > 0 and STATION_UNDOCKED or '') + this.station_link['url'] = station_url(this.system, this.station) + this.station_link.update_idletasks() + +def cmdr_data(data, is_beta): this.cmdr = data['commander']['name'] + # Always store initially, even if we're not the *current* system provider. + if not this.station_marketid: + this.station_marketid = data['commander']['docked'] and data['lastStarport']['id'] + # Only trust CAPI if these aren't yet set this.system = this.system or data['lastSystem']['name'] this.station = this.station or data['commander']['docked'] and data['lastStarport']['name'] + # Override standard URL functions if config.get('system_provider') == 'Inara': this.system_link['text'] = this.system this.system_link['url'] = system_url(this.system) this.system_link.update_idletasks() if config.get('station_provider') == 'Inara': - this.station_link['text'] = this.station or this.system + if data['commander']['docked']: + this.station_link['text'] = this.station + elif data['lastStarport']['name'] and data['lastStarport']['name'] != "": + this.station_link['text'] = STATION_UNDOCKED + else: + this.station_link['text'] = '' + this.station_link['url'] = station_url(this.system, this.station) this.station_link.update_idletasks() @@ -895,13 +928,14 @@ def update_location(event=None): 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': - # TODO: Bother with this? Just use systemaddress URL version when we detect it - 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 + pass +# this.system = eventData.get('starsystemInaraURL') +# if config.get('system_provider') == 'Inara': +# # TODO: Bother with this? Just use systemaddress URL version when we detect it +# 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):