mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-06-05 09:53:33 +03:00
autoformat with autopep8
This commit is contained in:
parent
3dab0e9d91
commit
eec4c2ebd6
177
plugins/inara.py
177
plugins/inara.py
@ -23,32 +23,32 @@ logger = logging.getLogger(appname)
|
|||||||
|
|
||||||
|
|
||||||
_TIMEOUT = 20
|
_TIMEOUT = 20
|
||||||
FAKE = ['CQC', 'Training', 'Destination'] # Fake systems that shouldn't be sent to Inara
|
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
|
CREDIT_RATIO = 1.05 # Update credits if they change by 5% over the course of a session
|
||||||
|
|
||||||
|
|
||||||
this: Any = sys.modules[__name__] # For holding module globals
|
this: Any = sys.modules[__name__] # For holding module globals
|
||||||
this.session = requests.Session()
|
this.session = requests.Session()
|
||||||
this.queue = Queue() # Items to be sent to Inara by worker thread
|
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.lastlocation = None # eventData from the last Commander's Flight Log event
|
||||||
this.lastship = None # eventData from the last addCommanderShip or setCommanderShip event
|
this.lastship = None # eventData from the last addCommanderShip or setCommanderShip event
|
||||||
|
|
||||||
# Cached Cmdr state
|
# Cached Cmdr state
|
||||||
this.events = [] # Unsent events
|
this.events = [] # Unsent events
|
||||||
this.cmdr = None
|
this.cmdr = None
|
||||||
this.FID = None # Frontier ID
|
this.FID = None # Frontier ID
|
||||||
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.newuser = False # just entered API Key - send state immediately
|
this.newuser = False # just entered API Key - send state immediately
|
||||||
this.newsession = True # starting a new session - wait for Cargo event
|
this.newsession = True # starting a new session - wait for Cargo event
|
||||||
this.undocked = False # just undocked
|
this.undocked = False # just undocked
|
||||||
this.suppress_docked = False # Skip initial Docked event if started docked
|
this.suppress_docked = False # Skip initial Docked event if started docked
|
||||||
this.cargo = None
|
this.cargo = None
|
||||||
this.materials = None
|
this.materials = None
|
||||||
this.lastcredits = 0 # Send credit update soon after Startup / new game
|
this.lastcredits = 0 # Send credit update soon after Startup / new game
|
||||||
this.storedmodules = None
|
this.storedmodules = None
|
||||||
this.loadout = None
|
this.loadout = None
|
||||||
this.fleet = None
|
this.fleet = None
|
||||||
this.shipswap = False # just swapped ship
|
this.shipswap = False # just swapped ship
|
||||||
|
|
||||||
# last time we updated, if unset in config this is 0, which means an instant update
|
# last time we updated, if unset in config this is 0, which means an instant update
|
||||||
LAST_UPDATE_CONF_KEY = 'inara_last_update'
|
LAST_UPDATE_CONF_KEY = 'inara_last_update'
|
||||||
@ -67,6 +67,7 @@ this.station = None
|
|||||||
this.station_marketid = None
|
this.station_marketid = None
|
||||||
STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00D7
|
STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00D7
|
||||||
|
|
||||||
|
|
||||||
def system_url(system_name):
|
def system_url(system_name):
|
||||||
if this.system_address:
|
if this.system_address:
|
||||||
return requests.utils.requote_uri(f'https://inara.cz/galaxy-starsystem/?search={this.system_address}')
|
return requests.utils.requote_uri(f'https://inara.cz/galaxy-starsystem/?search={this.system_address}')
|
||||||
@ -76,6 +77,7 @@ def system_url(system_name):
|
|||||||
|
|
||||||
return this.system
|
return this.system
|
||||||
|
|
||||||
|
|
||||||
def station_url(system_name, station_name):
|
def station_url(system_name, station_name):
|
||||||
if system_name:
|
if system_name:
|
||||||
if station_name:
|
if station_name:
|
||||||
@ -86,7 +88,7 @@ def station_url(system_name, station_name):
|
|||||||
|
|
||||||
|
|
||||||
def plugin_start3(plugin_dir):
|
def plugin_start3(plugin_dir):
|
||||||
this.thread = Thread(target = worker, name = 'Inara worker')
|
this.thread = Thread(target=worker, name='Inara worker')
|
||||||
this.thread.daemon = True
|
this.thread.daemon = True
|
||||||
this.thread.start()
|
this.thread.start()
|
||||||
|
|
||||||
@ -95,12 +97,14 @@ def plugin_start3(plugin_dir):
|
|||||||
this.timer_thread.start()
|
this.timer_thread.start()
|
||||||
return 'Inara'
|
return 'Inara'
|
||||||
|
|
||||||
|
|
||||||
def plugin_app(parent):
|
def plugin_app(parent):
|
||||||
this.system_link = parent.children['system'] # system label in main window
|
this.system_link = parent.children['system'] # system label in main window
|
||||||
this.station_link = parent.children['station'] # station 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('<<InaraLocation>>', update_location)
|
||||||
this.system_link.bind_all('<<InaraShip>>', update_ship)
|
this.system_link.bind_all('<<InaraShip>>', update_ship)
|
||||||
|
|
||||||
|
|
||||||
def plugin_stop():
|
def plugin_stop():
|
||||||
# Send any unsent events
|
# Send any unsent events
|
||||||
call()
|
call()
|
||||||
@ -112,25 +116,25 @@ def plugin_stop():
|
|||||||
|
|
||||||
this.timer_run = False
|
this.timer_run = False
|
||||||
|
|
||||||
def plugin_prefs(parent, cmdr, is_beta):
|
|
||||||
|
|
||||||
|
def plugin_prefs(parent, cmdr, is_beta):
|
||||||
PADX = 10
|
PADX = 10
|
||||||
BUTTONX = 12 # indent Checkbuttons and Radiobuttons
|
BUTTONX = 12 # indent Checkbuttons and Radiobuttons
|
||||||
PADY = 2 # close spacing
|
PADY = 2 # close spacing
|
||||||
|
|
||||||
frame = nb.Frame(parent)
|
frame = nb.Frame(parent)
|
||||||
frame.columnconfigure(1, weight=1)
|
frame.columnconfigure(1, weight=1)
|
||||||
|
|
||||||
HyperlinkLabel(frame, text='Inara', background=nb.Label().cget('background'), url='https://inara.cz/', underline=True).grid(columnspan=2, padx=PADX, sticky=tk.W) # Don't translate
|
HyperlinkLabel(frame, text='Inara', background=nb.Label().cget('background'), url='https://inara.cz/', underline=True).grid(columnspan=2, padx=PADX, sticky=tk.W) # Don't translate
|
||||||
this.log = tk.IntVar(value = config.getint('inara_out') and 1)
|
this.log = tk.IntVar(value = config.getint('inara_out') and 1)
|
||||||
this.log_button = nb.Checkbutton(frame, text=_('Send flight log and Cmdr status to Inara'), variable=this.log, command=prefsvarchanged)
|
this.log_button = nb.Checkbutton(frame, text=_('Send flight log and Cmdr status to Inara'), variable=this.log, command=prefsvarchanged)
|
||||||
this.log_button.grid(columnspan=2, padx=BUTTONX, pady=(5,0), sticky=tk.W)
|
this.log_button.grid(columnspan=2, padx=BUTTONX, pady=(5,0), sticky=tk.W)
|
||||||
|
|
||||||
nb.Label(frame).grid(sticky=tk.W) # big spacer
|
nb.Label(frame).grid(sticky=tk.W) # big spacer
|
||||||
this.label = HyperlinkLabel(frame, text=_('Inara credentials'), background=nb.Label().cget('background'), url='https://inara.cz/settings-api', underline=True) # Section heading in settings
|
this.label = HyperlinkLabel(frame, text=_('Inara credentials'), background=nb.Label().cget('background'), url='https://inara.cz/settings-api', underline=True) # Section heading in settings
|
||||||
this.label.grid(columnspan=2, padx=PADX, sticky=tk.W)
|
this.label.grid(columnspan=2, padx=PADX, sticky=tk.W)
|
||||||
|
|
||||||
this.apikey_label = nb.Label(frame, text=_('API Key')) # EDSM setting
|
this.apikey_label = nb.Label(frame, text=_('API Key')) # EDSM setting
|
||||||
this.apikey_label.grid(row=12, padx=PADX, sticky=tk.W)
|
this.apikey_label.grid(row=12, padx=PADX, sticky=tk.W)
|
||||||
this.apikey = nb.Entry(frame)
|
this.apikey = nb.Entry(frame)
|
||||||
this.apikey.grid(row=12, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
|
this.apikey.grid(row=12, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
|
||||||
@ -139,6 +143,7 @@ def plugin_prefs(parent, cmdr, is_beta):
|
|||||||
|
|
||||||
return frame
|
return frame
|
||||||
|
|
||||||
|
|
||||||
def prefs_cmdr_changed(cmdr, is_beta):
|
def prefs_cmdr_changed(cmdr, is_beta):
|
||||||
this.log_button['state'] = cmdr and not is_beta and tk.NORMAL or tk.DISABLED
|
this.log_button['state'] = cmdr and not is_beta and tk.NORMAL or tk.DISABLED
|
||||||
this.apikey['state'] = tk.NORMAL
|
this.apikey['state'] = tk.NORMAL
|
||||||
@ -149,9 +154,11 @@ def prefs_cmdr_changed(cmdr, is_beta):
|
|||||||
this.apikey.insert(0, cred)
|
this.apikey.insert(0, cred)
|
||||||
this.label['state'] = this.apikey_label['state'] = this.apikey['state'] = cmdr and not is_beta and this.log.get() and tk.NORMAL or tk.DISABLED
|
this.label['state'] = this.apikey_label['state'] = this.apikey['state'] = cmdr and not is_beta and this.log.get() and tk.NORMAL or tk.DISABLED
|
||||||
|
|
||||||
|
|
||||||
def prefsvarchanged():
|
def prefsvarchanged():
|
||||||
this.label['state'] = this.apikey_label['state'] = this.apikey['state'] = this.log.get() and this.log_button['state'] or tk.DISABLED
|
this.label['state'] = this.apikey_label['state'] = this.apikey['state'] = this.log.get() and this.log_button['state'] or tk.DISABLED
|
||||||
|
|
||||||
|
|
||||||
def prefs_changed(cmdr, is_beta):
|
def prefs_changed(cmdr, is_beta):
|
||||||
changed = config.getint('inara_out') != this.log.get()
|
changed = config.getint('inara_out') != this.log.get()
|
||||||
config.set('inara_out', this.log.get())
|
config.set('inara_out', this.log.get())
|
||||||
@ -179,10 +186,11 @@ def prefs_changed(cmdr, is_beta):
|
|||||||
config.set('inara_apikeys', apikeys)
|
config.set('inara_apikeys', apikeys)
|
||||||
|
|
||||||
if this.log.get() and changed:
|
if this.log.get() and changed:
|
||||||
this.newuser = True # Send basic info at next Journal event
|
this.newuser = True # Send basic info at next Journal event
|
||||||
add_event('getCommanderProfile', time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), { 'searchName': cmdr })
|
add_event('getCommanderProfile', time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), {'searchName': cmdr})
|
||||||
call()
|
call()
|
||||||
|
|
||||||
|
|
||||||
def credentials(cmdr):
|
def credentials(cmdr):
|
||||||
# Credentials for cmdr
|
# Credentials for cmdr
|
||||||
if not cmdr:
|
if not cmdr:
|
||||||
@ -260,7 +268,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
|
|
||||||
if (this.newuser or
|
if (this.newuser or
|
||||||
entry['event'] == 'StartUp' or
|
entry['event'] == 'StartUp' or
|
||||||
(this.newsession and entry['event'] == 'Cargo')):
|
(this.newsession and entry['event'] == 'Cargo')):
|
||||||
this.newuser = False
|
this.newuser = False
|
||||||
this.newsession = False
|
this.newsession = False
|
||||||
|
|
||||||
@ -271,23 +279,23 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
('rankName', k.lower()),
|
('rankName', k.lower()),
|
||||||
('rankValue', v[0]),
|
('rankValue', v[0]),
|
||||||
('rankProgress', v[1] / 100.0),
|
('rankProgress', v[1] / 100.0),
|
||||||
]) for k,v in state['Rank'].items() if v is not None
|
]) for k, v in state['Rank'].items() if v is not None
|
||||||
])
|
])
|
||||||
add_event('setCommanderReputationMajorFaction', entry['timestamp'],
|
add_event('setCommanderReputationMajorFaction', entry['timestamp'],
|
||||||
[
|
[
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
('majorfactionName', k.lower()),
|
('majorfactionName', k.lower()),
|
||||||
('majorfactionReputation', v / 100.0),
|
('majorfactionReputation', v / 100.0),
|
||||||
]) for k,v in state['Reputation'].items() if v is not None
|
]) for k, v in state['Reputation'].items() if v is not None
|
||||||
])
|
])
|
||||||
if state['Engineers']: # Not populated < 3.3
|
if state['Engineers']: # Not populated < 3.3
|
||||||
add_event('setCommanderRankEngineer', entry['timestamp'],
|
add_event('setCommanderRankEngineer', entry['timestamp'],
|
||||||
[
|
[
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
('engineerName', k),
|
('engineerName', k),
|
||||||
type(v) is tuple and ('rankValue', v[0]) or ('rankStage', v),
|
type(v) is tuple and ('rankValue', v[0]) or ('rankStage', v),
|
||||||
]) for k,v in state['Engineers'].items()
|
]) for k, v in state['Engineers'].items()
|
||||||
])
|
])
|
||||||
|
|
||||||
# Update location
|
# Update location
|
||||||
add_event('setCommanderTravelLocation', entry['timestamp'],
|
add_event('setCommanderTravelLocation', entry['timestamp'],
|
||||||
@ -297,12 +305,12 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
# Update ship
|
# Update ship
|
||||||
if state['ShipID']: # Unknown if started in Fighter or SRV
|
if state['ShipID']: # Unknown if started in Fighter or SRV
|
||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
('shipType', state['ShipType']),
|
('shipType', state['ShipType']),
|
||||||
('shipGameID', state['ShipID']),
|
('shipGameID', state['ShipID']),
|
||||||
('shipName', state['ShipName']), # Can be None
|
('shipName', state['ShipName']), # Can be None
|
||||||
('shipIdent', state['ShipIdent']), # Can be None
|
('shipIdent', state['ShipIdent']), # Can be None
|
||||||
('isCurrentShip', True),
|
('isCurrentShip', True),
|
||||||
])
|
])
|
||||||
if state['HullValue']:
|
if state['HullValue']:
|
||||||
@ -314,13 +322,12 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
|
|
||||||
this.loadout = make_loadout(state)
|
this.loadout = make_loadout(state)
|
||||||
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
||||||
|
|
||||||
call() # Call here just to be sure that if we can send, we do, otherwise it'll get it in the next tick
|
|
||||||
|
|
||||||
|
call() # Call here just to be sure that if we can send, we do, otherwise it'll get it in the next tick
|
||||||
|
|
||||||
# Promotions
|
# Promotions
|
||||||
elif entry['event'] == 'Promotion':
|
elif entry['event'] == 'Promotion':
|
||||||
for k,v in state['Rank'].items():
|
for k, v in state['Rank'].items():
|
||||||
if k in entry:
|
if k in entry:
|
||||||
add_event('setCommanderRankPilot', entry['timestamp'],
|
add_event('setCommanderRankPilot', entry['timestamp'],
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
@ -360,8 +367,8 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
('shipType', state['ShipType']),
|
('shipType', state['ShipType']),
|
||||||
('shipGameID', state['ShipID']),
|
('shipGameID', state['ShipID']),
|
||||||
('shipName', state['ShipName']), # Can be None
|
('shipName', state['ShipName']), # Can be None
|
||||||
('shipIdent', state['ShipIdent']), # Can be None
|
('shipIdent', state['ShipIdent']), # Can be None
|
||||||
('isCurrentShip', True),
|
('isCurrentShip', True),
|
||||||
])
|
])
|
||||||
if state['HullValue']:
|
if state['HullValue']:
|
||||||
@ -421,7 +428,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
('minorfactionName', f['Name']),
|
('minorfactionName', f['Name']),
|
||||||
('minorfactionReputation', f['MyReputation']/100.0),
|
('minorfactionReputation', f['MyReputation']/100.0),
|
||||||
]) for f in entry['Factions']
|
]) for f in entry['Factions']
|
||||||
])
|
])
|
||||||
elif entry['event'] == 'CarrierJump':
|
elif entry['event'] == 'CarrierJump':
|
||||||
add_event('addCommanderTravelCarrierJump', entry['timestamp'],
|
add_event('addCommanderTravelCarrierJump', entry['timestamp'],
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
@ -438,11 +445,10 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
('minorfactionName', f['Name']),
|
('minorfactionName', f['Name']),
|
||||||
('minorfactionReputation', f['MyReputation']/100.0),
|
('minorfactionReputation', f['MyReputation']/100.0),
|
||||||
]) for f in entry['Factions']
|
]) for f in entry['Factions']
|
||||||
])
|
])
|
||||||
# Ignore the following 'Docked' event
|
# Ignore the following 'Docked' event
|
||||||
this.suppress_docked = True
|
this.suppress_docked = True
|
||||||
|
|
||||||
|
|
||||||
cargo = [OrderedDict([('itemName', k), ('itemCount', state['Cargo'][k])]) for k in sorted(state['Cargo'])]
|
cargo = [OrderedDict([('itemName', k), ('itemCount', state['Cargo'][k])]) for k in sorted(state['Cargo'])]
|
||||||
|
|
||||||
# Send cargo and materials if changed
|
# Send cargo and materials if changed
|
||||||
@ -451,7 +457,8 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
this.cargo = cargo
|
this.cargo = cargo
|
||||||
materials = []
|
materials = []
|
||||||
for category in ['Raw', 'Manufactured', 'Encoded']:
|
for category in ['Raw', 'Manufactured', 'Encoded']:
|
||||||
materials.extend([ OrderedDict([('itemName', k), ('itemCount', state[category][k])]) for k in sorted(state[category]) ])
|
materials.extend([OrderedDict([('itemName', k), ('itemCount', state[category][k])])
|
||||||
|
for k in sorted(state[category])])
|
||||||
if this.materials != materials:
|
if this.materials != materials:
|
||||||
add_event('setCommanderInventoryMaterials', entry['timestamp'], materials)
|
add_event('setCommanderInventoryMaterials', entry['timestamp'], materials)
|
||||||
this.materials = materials
|
this.materials = materials
|
||||||
@ -469,7 +476,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
]))
|
]))
|
||||||
this.lastcredits = state['Credits']
|
this.lastcredits = state['Credits']
|
||||||
elif entry['event'] == 'Statistics':
|
elif entry['event'] == 'Statistics':
|
||||||
add_event('setCommanderGameStatistics', entry['timestamp'], state['Statistics']) # may be out of date
|
add_event('setCommanderGameStatistics', entry['timestamp'], state['Statistics']) # may be out of date
|
||||||
|
|
||||||
# Selling / swapping ships
|
# Selling / swapping ships
|
||||||
if entry['event'] == 'ShipyardNew':
|
if entry['event'] == 'ShipyardNew':
|
||||||
@ -478,11 +485,11 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
('shipType', entry['ShipType']),
|
('shipType', entry['ShipType']),
|
||||||
('shipGameID', entry['NewShipID']),
|
('shipGameID', entry['NewShipID']),
|
||||||
]))
|
]))
|
||||||
this.shipswap = True # Want subsequent Loadout event to be sent immediately
|
this.shipswap = True # Want subsequent Loadout event to be sent immediately
|
||||||
|
|
||||||
elif entry['event'] in ['ShipyardBuy', 'ShipyardSell', 'SellShipOnRebuy', 'ShipyardSwap']:
|
elif entry['event'] in ['ShipyardBuy', 'ShipyardSell', 'SellShipOnRebuy', 'ShipyardSwap']:
|
||||||
if entry['event'] == 'ShipyardSwap':
|
if entry['event'] == 'ShipyardSwap':
|
||||||
this.shipswap = True # Don't know new ship name and ident 'til the following Loadout event
|
this.shipswap = True # Don't know new ship name and ident 'til the following Loadout event
|
||||||
if 'StoreShipID' in entry:
|
if 'StoreShipID' in entry:
|
||||||
add_event('setCommanderShip', entry['timestamp'],
|
add_event('setCommanderShip', entry['timestamp'],
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
@ -503,8 +510,8 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
OrderedDict([
|
OrderedDict([
|
||||||
('shipType', state['ShipType']),
|
('shipType', state['ShipType']),
|
||||||
('shipGameID', state['ShipID']),
|
('shipGameID', state['ShipID']),
|
||||||
('shipName', state['ShipName']), # Can be None
|
('shipName', state['ShipName']), # Can be None
|
||||||
('shipIdent', state['ShipIdent']), # Can be None
|
('shipIdent', state['ShipIdent']), # Can be None
|
||||||
('isCurrentShip', True),
|
('isCurrentShip', True),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
@ -535,14 +542,14 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
'shipGameID': x['ShipID'],
|
'shipGameID': x['ShipID'],
|
||||||
'shipName': x.get('Name'),
|
'shipName': x.get('Name'),
|
||||||
'isHot': x['Hot'],
|
'isHot': x['Hot'],
|
||||||
'starsystemName': x.get('StarSystem'), # Not present for ships in transit
|
'starsystemName': x.get('StarSystem'), # Not present for ships in transit
|
||||||
'marketID': x.get('ShipMarketID'), # "
|
'marketID': x.get('ShipMarketID'), # "
|
||||||
} for x in entry['ShipsRemote']],
|
} for x in entry['ShipsRemote']],
|
||||||
key = itemgetter('shipGameID')
|
key=itemgetter('shipGameID')
|
||||||
)
|
)
|
||||||
if this.fleet != fleet:
|
if this.fleet != fleet:
|
||||||
this.fleet = fleet
|
this.fleet = fleet
|
||||||
this.events = [x for x in this.events if x['eventName'] != 'setCommanderShip'] # Remove any unsent
|
this.events = [x for x in this.events if x['eventName'] != 'setCommanderShip'] # Remove any unsent
|
||||||
for ship in this.fleet:
|
for ship in this.fleet:
|
||||||
add_event('setCommanderShip', entry['timestamp'], ship)
|
add_event('setCommanderShip', entry['timestamp'], ship)
|
||||||
|
|
||||||
@ -551,12 +558,12 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
loadout = make_loadout(state)
|
loadout = make_loadout(state)
|
||||||
if this.loadout != loadout:
|
if this.loadout != loadout:
|
||||||
this.loadout = loadout
|
this.loadout = loadout
|
||||||
this.events = [x for x in this.events if x['eventName'] != 'setCommanderShipLoadout' or x['shipGameID'] != this.loadout['shipGameID']] # Remove any unsent for this ship
|
this.events = [x for x in this.events if x['eventName'] != 'setCommanderShipLoadout' or x['shipGameID'] != this.loadout['shipGameID']] # Remove any unsent for this ship
|
||||||
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
||||||
|
|
||||||
# Stored modules
|
# Stored modules
|
||||||
if entry['event'] == 'StoredModules':
|
if entry['event'] == 'StoredModules':
|
||||||
items = dict([(x['StorageSlot'], x) for x in entry['Items']]) # Impose an order
|
items = dict([(x['StorageSlot'], x) for x in entry['Items']]) # Impose an order
|
||||||
modules = []
|
modules = []
|
||||||
for slot in sorted(items):
|
for slot in sorted(items):
|
||||||
item = items[slot]
|
item = items[slot]
|
||||||
@ -584,7 +591,8 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
if this.storedmodules != modules:
|
if this.storedmodules != modules:
|
||||||
# Only send on change
|
# Only send on change
|
||||||
this.storedmodules = modules
|
this.storedmodules = modules
|
||||||
this.events = [x for x in this.events if x['eventName'] != 'setCommanderStorageModules'] # Remove any unsent
|
this.events = [x for x in this.events if x['eventName']
|
||||||
|
!= 'setCommanderStorageModules'] # Remove any unsent
|
||||||
add_event('setCommanderStorageModules', entry['timestamp'], this.storedmodules)
|
add_event('setCommanderStorageModules', entry['timestamp'], this.storedmodules)
|
||||||
|
|
||||||
# Missions
|
# Missions
|
||||||
@ -600,7 +608,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
])
|
])
|
||||||
# optional mission-specific properties
|
# optional mission-specific properties
|
||||||
for (iprop, prop) in [
|
for (iprop, prop) in [
|
||||||
('missionExpiry', 'Expiry'), # Listed as optional in the docs, but always seems to be present
|
('missionExpiry', 'Expiry'), # Listed as optional in the docs, but always seems to be present
|
||||||
('starsystemNameTarget', 'DestinationSystem'),
|
('starsystemNameTarget', 'DestinationSystem'),
|
||||||
('stationNameTarget', 'DestinationStation'),
|
('stationNameTarget', 'DestinationStation'),
|
||||||
('minorfactionNameTarget', 'TargetFaction'),
|
('minorfactionNameTarget', 'TargetFaction'),
|
||||||
@ -619,29 +627,31 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
add_event('addCommanderMission', entry['timestamp'], data)
|
add_event('addCommanderMission', entry['timestamp'], data)
|
||||||
|
|
||||||
elif entry['event'] == 'MissionAbandoned':
|
elif entry['event'] == 'MissionAbandoned':
|
||||||
add_event('setCommanderMissionAbandoned', entry['timestamp'], { 'missionGameID': entry['MissionID'] })
|
add_event('setCommanderMissionAbandoned', entry['timestamp'], {'missionGameID': entry['MissionID']})
|
||||||
|
|
||||||
elif entry['event'] == 'MissionCompleted':
|
elif entry['event'] == 'MissionCompleted':
|
||||||
for x in entry.get('PermitsAwarded', []):
|
for x in entry.get('PermitsAwarded', []):
|
||||||
add_event('addCommanderPermit', entry['timestamp'], { 'starsystemName': x })
|
add_event('addCommanderPermit', entry['timestamp'], {'starsystemName': x})
|
||||||
|
|
||||||
data = OrderedDict([ ('missionGameID', entry['MissionID']) ])
|
data = OrderedDict([('missionGameID', entry['MissionID'])])
|
||||||
if 'Donation' in entry:
|
if 'Donation' in entry:
|
||||||
data['donationCredits'] = entry['Donation']
|
data['donationCredits'] = entry['Donation']
|
||||||
if 'Reward' in entry:
|
if 'Reward' in entry:
|
||||||
data['rewardCredits'] = entry['Reward']
|
data['rewardCredits'] = entry['Reward']
|
||||||
if 'PermitsAwarded' in entry:
|
if 'PermitsAwarded' in entry:
|
||||||
data['rewardPermits'] = [{ 'starsystemName': x } for x in entry['PermitsAwarded']]
|
data['rewardPermits'] = [{'starsystemName': x} for x in entry['PermitsAwarded']]
|
||||||
if 'CommodityReward' in entry:
|
if 'CommodityReward' in entry:
|
||||||
data['rewardCommodities'] = [{ 'itemName': x['Name'], 'itemCount': x['Count'] } for x in entry['CommodityReward']]
|
data['rewardCommodities'] = [{'itemName': x['Name'], 'itemCount': x['Count']}
|
||||||
|
for x in entry['CommodityReward']]
|
||||||
if 'MaterialsReward' in entry:
|
if 'MaterialsReward' in entry:
|
||||||
data['rewardMaterials'] = [{ 'itemName': x['Name'], 'itemCount': x['Count'] } for x in entry['MaterialsReward']]
|
data['rewardMaterials'] = [{'itemName': x['Name'], 'itemCount': x['Count']}
|
||||||
|
for x in entry['MaterialsReward']]
|
||||||
factioneffects = []
|
factioneffects = []
|
||||||
for faction in entry.get('FactionEffects', []):
|
for faction in entry.get('FactionEffects', []):
|
||||||
effect = OrderedDict([ ('minorfactionName', faction['Faction']) ])
|
effect = OrderedDict([('minorfactionName', faction['Faction'])])
|
||||||
for influence in faction.get('Influence', []):
|
for influence in faction.get('Influence', []):
|
||||||
if 'Influence' in influence:
|
if 'Influence' in influence:
|
||||||
effect['influenceGain'] = len(effect.get('influenceGain', '')) > len(influence['Influence']) and effect['influenceGain'] or influence['Influence'] # pick highest
|
effect['influenceGain'] = len(effect.get('influenceGain', '')) > len(influence['Influence']) and effect['influenceGain'] or influence['Influence'] # pick highest
|
||||||
if 'Reputation' in faction:
|
if 'Reputation' in faction:
|
||||||
effect['reputationGain'] = faction['Reputation']
|
effect['reputationGain'] = faction['Reputation']
|
||||||
factioneffects.append(effect)
|
factioneffects.append(effect)
|
||||||
@ -650,11 +660,11 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
add_event('setCommanderMissionCompleted', entry['timestamp'], data)
|
add_event('setCommanderMissionCompleted', entry['timestamp'], data)
|
||||||
|
|
||||||
elif entry['event'] == 'MissionFailed':
|
elif entry['event'] == 'MissionFailed':
|
||||||
add_event('setCommanderMissionFailed', entry['timestamp'], { 'missionGameID': entry['MissionID'] })
|
add_event('setCommanderMissionFailed', entry['timestamp'], {'missionGameID': entry['MissionID']})
|
||||||
|
|
||||||
# Combat
|
# Combat
|
||||||
if entry['event'] == 'Died':
|
if entry['event'] == 'Died':
|
||||||
data = OrderedDict([ ('starsystemName', system) ])
|
data = OrderedDict([('starsystemName', system)])
|
||||||
if 'Killers' in entry:
|
if 'Killers' in entry:
|
||||||
data['wingOpponentNames'] = [x['Name'] for x in entry['Killers']]
|
data['wingOpponentNames'] = [x['Name'] for x in entry['Killers']]
|
||||||
elif 'KillerName' in entry:
|
elif 'KillerName' in entry:
|
||||||
@ -665,7 +675,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
data = OrderedDict([('starsystemName', system),
|
data = OrderedDict([('starsystemName', system),
|
||||||
('isPlayer', entry['IsPlayer']),
|
('isPlayer', entry['IsPlayer']),
|
||||||
('isSubmit', entry['Submitted']),
|
('isSubmit', entry['Submitted']),
|
||||||
])
|
])
|
||||||
if 'Interdictor' in entry:
|
if 'Interdictor' in entry:
|
||||||
data['opponentName'] = entry['Interdictor']
|
data['opponentName'] = entry['Interdictor']
|
||||||
elif 'Faction' in entry:
|
elif 'Faction' in entry:
|
||||||
@ -678,7 +688,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
data = OrderedDict([('starsystemName', system),
|
data = OrderedDict([('starsystemName', system),
|
||||||
('isPlayer', entry['IsPlayer']),
|
('isPlayer', entry['IsPlayer']),
|
||||||
('isSuccess', entry['Success']),
|
('isSuccess', entry['Success']),
|
||||||
])
|
])
|
||||||
if 'Interdicted' in entry:
|
if 'Interdicted' in entry:
|
||||||
data['opponentName'] = entry['Interdicted']
|
data['opponentName'] = entry['Interdicted']
|
||||||
elif 'Faction' in entry:
|
elif 'Faction' in entry:
|
||||||
@ -692,17 +702,18 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
OrderedDict([('starsystemName', system),
|
OrderedDict([('starsystemName', system),
|
||||||
('opponentName', entry['Interdictor']),
|
('opponentName', entry['Interdictor']),
|
||||||
('isPlayer', entry['IsPlayer']),
|
('isPlayer', entry['IsPlayer']),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
elif entry['event'] == 'PVPKill':
|
elif entry['event'] == 'PVPKill':
|
||||||
add_event('addCommanderCombatKill', entry['timestamp'],
|
add_event('addCommanderCombatKill', entry['timestamp'],
|
||||||
OrderedDict([('starsystemName', system),
|
OrderedDict([('starsystemName', system),
|
||||||
('opponentName', entry['Victim']),
|
('opponentName', entry['Victim']),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
# Community Goals
|
# Community Goals
|
||||||
if entry['event'] == 'CommunityGoal':
|
if entry['event'] == 'CommunityGoal':
|
||||||
this.events = [x for x in this.events if x['eventName'] not in ['setCommunityGoal', 'setCommanderCommunityGoalProgress']] # Remove any unsent
|
this.events = [x for x in this.events if x['eventName'] not in [
|
||||||
|
'setCommunityGoal', 'setCommanderCommunityGoalProgress']] # Remove any unsent
|
||||||
for goal in entry['CurrentGoals']:
|
for goal in entry['CurrentGoals']:
|
||||||
|
|
||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
@ -741,12 +752,12 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
add_event('addCommanderFriend', entry['timestamp'],
|
add_event('addCommanderFriend', entry['timestamp'],
|
||||||
OrderedDict([('commanderName', entry['Name']),
|
OrderedDict([('commanderName', entry['Name']),
|
||||||
('gamePlatform', 'pc'),
|
('gamePlatform', 'pc'),
|
||||||
]))
|
]))
|
||||||
elif entry['Status'] in ['Declined', 'Lost']:
|
elif entry['Status'] in ['Declined', 'Lost']:
|
||||||
add_event('delCommanderFriend', entry['timestamp'],
|
add_event('delCommanderFriend', entry['timestamp'],
|
||||||
OrderedDict([('commanderName', entry['Name']),
|
OrderedDict([('commanderName', entry['Name']),
|
||||||
('gamePlatform', 'pc'),
|
('gamePlatform', 'pc'),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
this.newuser = False
|
this.newuser = False
|
||||||
|
|
||||||
@ -761,6 +772,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
this.station_link['url'] = station_url(this.system, this.station)
|
this.station_link['url'] = station_url(this.system, this.station)
|
||||||
this.station_link.update_idletasks()
|
this.station_link.update_idletasks()
|
||||||
|
|
||||||
|
|
||||||
def cmdr_data(data, is_beta):
|
def cmdr_data(data, is_beta):
|
||||||
this.cmdr = data['commander']['name']
|
this.cmdr = data['commander']['name']
|
||||||
|
|
||||||
@ -789,7 +801,7 @@ def cmdr_data(data, is_beta):
|
|||||||
|
|
||||||
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(this.cmdr):
|
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(this.cmdr):
|
||||||
if not (CREDIT_RATIO > this.lastcredits / data['commander']['credits'] > 1/CREDIT_RATIO):
|
if not (CREDIT_RATIO > this.lastcredits / data['commander']['credits'] > 1/CREDIT_RATIO):
|
||||||
this.events = [x for x in this.events if x['eventName'] != 'setCommanderCredits'] # Remove any unsent
|
this.events = [x for x in this.events if x['eventName'] != 'setCommanderCredits'] # Remove any unsent
|
||||||
add_event('setCommanderCredits', data['timestamp'],
|
add_event('setCommanderCredits', data['timestamp'],
|
||||||
OrderedDict([
|
OrderedDict([
|
||||||
('commanderCredits', data['commander']['credits']),
|
('commanderCredits', data['commander']['credits']),
|
||||||
@ -846,6 +858,7 @@ def make_loadout(state):
|
|||||||
('shipLoadout', modules),
|
('shipLoadout', modules),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def add_event(name, timestamp, data):
|
def add_event(name, timestamp, data):
|
||||||
this.events.append(OrderedDict([
|
this.events.append(OrderedDict([
|
||||||
('eventName', name),
|
('eventName', name),
|
||||||
@ -853,6 +866,7 @@ def add_event(name, timestamp, data):
|
|||||||
('eventData', data),
|
('eventData', data),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
|
|
||||||
def call_timer(wait=FLOOD_LIMIT_SECONDS):
|
def call_timer(wait=FLOOD_LIMIT_SECONDS):
|
||||||
while this.timer_run:
|
while this.timer_run:
|
||||||
time.sleep(wait)
|
time.sleep(wait)
|
||||||
@ -860,11 +874,13 @@ def call_timer(wait=FLOOD_LIMIT_SECONDS):
|
|||||||
call()
|
call()
|
||||||
|
|
||||||
# Queue a call to Inara, handled in Worker thread
|
# Queue a call to Inara, handled in Worker thread
|
||||||
|
|
||||||
|
|
||||||
def call(callback=None, force=False):
|
def call(callback=None, force=False):
|
||||||
if not this.events:
|
if not this.events:
|
||||||
return
|
return
|
||||||
|
|
||||||
if (time.time() - config.getint(LAST_UPDATE_CONF_KEY)) <= FLOOD_LIMIT_SECONDS and not force:
|
if (time.time() - config.getint(LAST_UPDATE_CONF_KEY)) <= FLOOD_LIMIT_SECONDS and not force:
|
||||||
return
|
return
|
||||||
|
|
||||||
config.set(LAST_UPDATE_CONF_KEY, int(time.time()))
|
config.set(LAST_UPDATE_CONF_KEY, int(time.time()))
|
||||||
@ -877,24 +893,26 @@ def call(callback=None, force=False):
|
|||||||
('commanderName', this.cmdr),
|
('commanderName', this.cmdr),
|
||||||
('commanderFrontierID', this.FID),
|
('commanderFrontierID', this.FID),
|
||||||
])),
|
])),
|
||||||
('events', list(this.events)), # shallow copy
|
('events', list(this.events)), # shallow copy
|
||||||
])
|
])
|
||||||
this.events = []
|
this.events = []
|
||||||
this.queue.put(('https://inara.cz/inapi/v1/', data, None))
|
this.queue.put(('https://inara.cz/inapi/v1/', data, None))
|
||||||
|
|
||||||
# Worker thread
|
# Worker thread
|
||||||
|
|
||||||
|
|
||||||
def worker():
|
def worker():
|
||||||
while True:
|
while True:
|
||||||
item = this.queue.get()
|
item = this.queue.get()
|
||||||
if not item:
|
if not item:
|
||||||
return # Closing
|
return # Closing
|
||||||
else:
|
else:
|
||||||
(url, data, callback) = item
|
(url, data, callback) = item
|
||||||
|
|
||||||
retrying = 0
|
retrying = 0
|
||||||
while retrying < 3:
|
while retrying < 3:
|
||||||
try:
|
try:
|
||||||
r = this.session.post(url, data=json.dumps(data, separators = (',', ':')), timeout=_TIMEOUT)
|
r = this.session.post(url, data=json.dumps(data, separators=(',', ':')), timeout=_TIMEOUT)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
reply = r.json()
|
reply = r.json()
|
||||||
status = reply['header']['eventStatus']
|
status = reply['header']['eventStatus']
|
||||||
@ -944,10 +962,13 @@ def update_location(event=None):
|
|||||||
for plugin in plug.provides('inara_notify_location'):
|
for plugin in plug.provides('inara_notify_location'):
|
||||||
plug.invoke(plugin, None, 'inara_notify_location', this.lastlocation)
|
plug.invoke(plugin, None, 'inara_notify_location', this.lastlocation)
|
||||||
|
|
||||||
|
|
||||||
def inara_notify_location(eventData):
|
def inara_notify_location(eventData):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Call inara_notify_ship() in interested plugins with Inara's response when changing ship
|
# Call inara_notify_ship() in interested plugins with Inara's response when changing ship
|
||||||
|
|
||||||
|
|
||||||
def update_ship(event=None):
|
def update_ship(event=None):
|
||||||
if this.lastship:
|
if this.lastship:
|
||||||
for plugin in plug.provides('inara_notify_ship'):
|
for plugin in plug.provides('inara_notify_ship'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user