mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-05-31 15:49:41 +03:00
CAPI: Reinstate/format post-query exceptions/etc in AppWindow
This commit is contained in:
parent
407bc9f53a
commit
7f362be740
@ -942,164 +942,166 @@ class AppWindow(object):
|
|||||||
"""Handle the resulting data from a CAPI query."""
|
"""Handle the resulting data from a CAPI query."""
|
||||||
try:
|
try:
|
||||||
data = self.capi_response_queue.get(block=False)
|
data = self.capi_response_queue.get(block=False)
|
||||||
|
|
||||||
except queue.Empty:
|
|
||||||
logger.error('There was no response in the queue!')
|
|
||||||
# TODO: Set status text
|
|
||||||
return
|
|
||||||
|
|
||||||
else:
|
|
||||||
if isinstance(data, companion.CAPIFailedRequest):
|
if isinstance(data, companion.CAPIFailedRequest):
|
||||||
logger.trace_if('capi.worker', f'Failed Request: {data.message}')
|
logger.trace_if('capi.worker', f'Failed Request: {data.message}')
|
||||||
raise data.exception
|
if data.exception:
|
||||||
|
raise data.exception
|
||||||
|
|
||||||
# Validation
|
else:
|
||||||
if 'commander' not in data:
|
raise ValueError(data.message)
|
||||||
# This can happen with EGS Auth if no commander created yet
|
|
||||||
# LANG: No data was returned for the commander from the Frontier CAPI
|
|
||||||
err = self.status['text'] = _('CAPI: No commander data returned')
|
|
||||||
|
|
||||||
elif not data.get('commander', {}).get('name'):
|
# Validation
|
||||||
# LANG: We didn't have the commander name when we should have
|
if 'commander' not in data:
|
||||||
err = self.status['text'] = _("Who are you?!") # Shouldn't happen
|
# This can happen with EGS Auth if no commander created yet
|
||||||
|
# LANG: No data was returned for the commander from the Frontier CAPI
|
||||||
|
err = self.status['text'] = _('CAPI: No commander data returned')
|
||||||
|
|
||||||
elif (not data.get('lastSystem', {}).get('name')
|
elif not data.get('commander', {}).get('name'):
|
||||||
or (data['commander'].get('docked')
|
# LANG: We didn't have the commander name when we should have
|
||||||
and not data.get('lastStarport', {}).get('name'))):
|
err = self.status['text'] = _("Who are you?!") # Shouldn't happen
|
||||||
# LANG: We don't know where the commander is, when we should
|
|
||||||
err = self.status['text'] = _("Where are you?!") # Shouldn't happen
|
|
||||||
|
|
||||||
elif not data.get('ship', {}).get('name') or not data.get('ship', {}).get('modules'):
|
elif (not data.get('lastSystem', {}).get('name')
|
||||||
# LANG: We don't know what ship the commander is in, when we should
|
or (data['commander'].get('docked')
|
||||||
err = self.status['text'] = _("What are you flying?!") # Shouldn't happen
|
and not data.get('lastStarport', {}).get('name'))):
|
||||||
|
# LANG: We don't know where the commander is, when we should
|
||||||
|
err = self.status['text'] = _("Where are you?!") # Shouldn't happen
|
||||||
|
|
||||||
elif monitor.cmdr and data['commander']['name'] != monitor.cmdr:
|
elif not data.get('ship', {}).get('name') or not data.get('ship', {}).get('modules'):
|
||||||
# Companion API Commander doesn't match Journal
|
# LANG: We don't know what ship the commander is in, when we should
|
||||||
raise companion.CmdrError()
|
err = self.status['text'] = _("What are you flying?!") # Shouldn't happen
|
||||||
|
|
||||||
elif auto_update and not monitor.state['OnFoot'] and not data['commander'].get('docked'):
|
elif monitor.cmdr and data['commander']['name'] != monitor.cmdr:
|
||||||
# auto update is only when just docked
|
# Companion API Commander doesn't match Journal
|
||||||
logger.warning(f"{auto_update!r} and not {monitor.state['OnFoot']!r} and "
|
raise companion.CmdrError()
|
||||||
f"not {data['commander'].get('docked')!r}")
|
|
||||||
raise companion.ServerLagging()
|
|
||||||
|
|
||||||
elif data['lastSystem']['name'] != monitor.system:
|
elif auto_update and not monitor.state['OnFoot'] and not data['commander'].get('docked'):
|
||||||
# CAPI system must match last journal one
|
# auto update is only when just docked
|
||||||
logger.warning(f"{data['lastSystem']['name']!r} != {monitor.system!r}")
|
logger.warning(f"{auto_update!r} and not {monitor.state['OnFoot']!r} and "
|
||||||
raise companion.ServerLagging()
|
f"not {data['commander'].get('docked')!r}")
|
||||||
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
elif data['lastStarport']['name'] != monitor.station:
|
elif data['lastSystem']['name'] != monitor.system:
|
||||||
if monitor.state['OnFoot'] and monitor.station:
|
# CAPI system must match last journal one
|
||||||
logger.warning(f"({data['lastStarport']['name']!r} != {monitor.station!r}) AND "
|
logger.warning(f"{data['lastSystem']['name']!r} != {monitor.system!r}")
|
||||||
f"{monitor.state['OnFoot']!r} and {monitor.station!r}")
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
|
elif data['lastStarport']['name'] != monitor.station:
|
||||||
|
if monitor.state['OnFoot'] and monitor.station:
|
||||||
|
logger.warning(f"({data['lastStarport']['name']!r} != {monitor.station!r}) AND "
|
||||||
|
f"{monitor.state['OnFoot']!r} and {monitor.station!r}")
|
||||||
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
|
else:
|
||||||
|
last_station = None
|
||||||
|
if data['commander']['docked']:
|
||||||
|
last_station = data['lastStarport']['name']
|
||||||
|
|
||||||
|
if monitor.station is None:
|
||||||
|
# Likely (re-)Embarked on ship docked at an EDO settlement.
|
||||||
|
# Both Disembark and Embark have `"Onstation": false` in Journal.
|
||||||
|
# So there's nothing to tell us which settlement we're (still,
|
||||||
|
# or now, if we came here in Apex and then recalled ship) docked at.
|
||||||
|
logger.debug("monitor.station is None - so EDO settlement?")
|
||||||
|
raise companion.NoMonitorStation()
|
||||||
|
|
||||||
|
if last_station != monitor.station:
|
||||||
|
# CAPI lastStarport must match
|
||||||
|
logger.warning(f"({data['lastStarport']['name']!r} != {monitor.station!r}) AND "
|
||||||
|
f"{last_station!r} != {monitor.station!r}")
|
||||||
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
|
self.holdofftime = querytime + companion.holdoff
|
||||||
|
|
||||||
|
elif not monitor.state['OnFoot'] and data['ship']['id'] != monitor.state['ShipID']:
|
||||||
|
# CAPI ship must match
|
||||||
|
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||||
|
f"{data['ship']['id']!r} != {monitor.state['ShipID']!r}")
|
||||||
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
|
elif not monitor.state['OnFoot'] and data['ship']['name'].lower() != monitor.state['ShipType']:
|
||||||
|
# CAPI ship type must match
|
||||||
|
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||||
|
f"{data['ship']['name'].lower()!r} != {monitor.state['ShipType']!r}")
|
||||||
raise companion.ServerLagging()
|
raise companion.ServerLagging()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
last_station = None
|
if __debug__: # Recording
|
||||||
if data['commander']['docked']:
|
companion.session.dump_capi_data(data)
|
||||||
last_station = data['lastStarport']['name']
|
|
||||||
|
|
||||||
if monitor.station is None:
|
if not monitor.state['ShipType']: # Started game in SRV or fighter
|
||||||
# Likely (re-)Embarked on ship docked at an EDO settlement.
|
self.ship['text'] = ship_name_map.get(data['ship']['name'].lower(), data['ship']['name'])
|
||||||
# Both Disembark and Embark have `"Onstation": false` in Journal.
|
monitor.state['ShipID'] = data['ship']['id']
|
||||||
# So there's nothing to tell us which settlement we're (still,
|
monitor.state['ShipType'] = data['ship']['name'].lower()
|
||||||
# or now, if we came here in Apex and then recalled ship) docked at.
|
|
||||||
logger.debug("monitor.station is None - so EDO settlement?")
|
|
||||||
raise companion.NoMonitorStation()
|
|
||||||
|
|
||||||
if last_station != monitor.station:
|
if not monitor.state['Modules']:
|
||||||
# CAPI lastStarport must match
|
self.ship.configure(state=tk.DISABLED)
|
||||||
logger.warning(f"({data['lastStarport']['name']!r} != {monitor.station!r}) AND "
|
|
||||||
f"{last_station!r} != {monitor.station!r}")
|
|
||||||
raise companion.ServerLagging()
|
|
||||||
|
|
||||||
self.holdofftime = querytime + companion.holdoff
|
# We might have disabled this in the conditional above.
|
||||||
|
if monitor.state['Modules']:
|
||||||
|
self.ship.configure(state=True)
|
||||||
|
|
||||||
elif not monitor.state['OnFoot'] and data['ship']['id'] != monitor.state['ShipID']:
|
if monitor.state.get('SuitCurrent') is not None:
|
||||||
# CAPI ship must match
|
if (loadout := data.get('loadout')) is not None:
|
||||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
if (suit := loadout.get('suit')) is not None:
|
||||||
f"{data['ship']['id']!r} != {monitor.state['ShipID']!r}")
|
if (suitname := suit.get('edmcName')) is not None:
|
||||||
raise companion.ServerLagging()
|
# We've been paranoid about loadout->suit->suitname, now just assume loadouts is there
|
||||||
|
loadout_name = index_possibly_sparse_list(
|
||||||
|
data['loadouts'], loadout['loadoutSlotId']
|
||||||
|
)['name']
|
||||||
|
|
||||||
elif not monitor.state['OnFoot'] and data['ship']['name'].lower() != monitor.state['ShipType']:
|
self.suit['text'] = f'{suitname} ({loadout_name})'
|
||||||
# CAPI ship type must match
|
|
||||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
|
||||||
f"{data['ship']['name'].lower()!r} != {monitor.state['ShipType']!r}")
|
|
||||||
raise companion.ServerLagging()
|
|
||||||
|
|
||||||
else:
|
self.suit_show_if_set()
|
||||||
if __debug__: # Recording
|
|
||||||
companion.session.dump_capi_data(data)
|
|
||||||
|
|
||||||
if not monitor.state['ShipType']: # Started game in SRV or fighter
|
if data['commander'].get('credits') is not None:
|
||||||
self.ship['text'] = ship_name_map.get(data['ship']['name'].lower(), data['ship']['name'])
|
monitor.state['Credits'] = data['commander']['credits']
|
||||||
monitor.state['ShipID'] = data['ship']['id']
|
monitor.state['Loan'] = data['commander'].get('debt', 0)
|
||||||
monitor.state['ShipType'] = data['ship']['name'].lower()
|
|
||||||
|
|
||||||
if not monitor.state['Modules']:
|
# stuff we can do when not docked
|
||||||
self.ship.configure(state=tk.DISABLED)
|
err = plug.notify_newdata(data, monitor.is_beta)
|
||||||
|
self.status['text'] = err and err or ''
|
||||||
|
if err:
|
||||||
|
play_bad = True
|
||||||
|
|
||||||
# We might have disabled this in the conditional above.
|
# Export market data
|
||||||
if monitor.state['Modules']:
|
if not self.export_market_data(data):
|
||||||
self.ship.configure(state=True)
|
err = 'Error: Exporting Market data'
|
||||||
|
play_bad = True
|
||||||
|
|
||||||
if monitor.state.get('SuitCurrent') is not None:
|
self.holdofftime = querytime + companion.holdoff
|
||||||
if (loadout := data.get('loadout')) is not None:
|
|
||||||
if (suit := loadout.get('suit')) is not None:
|
|
||||||
if (suitname := suit.get('edmcName')) is not None:
|
|
||||||
# We've been paranoid about loadout->suit->suitname, now just assume loadouts is there
|
|
||||||
loadout_name = index_possibly_sparse_list(
|
|
||||||
data['loadouts'], loadout['loadoutSlotId']
|
|
||||||
)['name']
|
|
||||||
|
|
||||||
self.suit['text'] = f'{suitname} ({loadout_name})'
|
except queue.Empty:
|
||||||
|
logger.error('There was no response in the queue!')
|
||||||
|
# TODO: Set status text
|
||||||
|
return
|
||||||
|
|
||||||
self.suit_show_if_set()
|
# Companion API problem
|
||||||
|
except companion.ServerLagging as e:
|
||||||
if data['commander'].get('credits') is not None:
|
err = str(e)
|
||||||
monitor.state['Credits'] = data['commander']['credits']
|
if retrying:
|
||||||
monitor.state['Loan'] = data['commander'].get('debt', 0)
|
self.status['text'] = err
|
||||||
|
|
||||||
# stuff we can do when not docked
|
|
||||||
err = plug.notify_newdata(data, monitor.is_beta)
|
|
||||||
self.status['text'] = err and err or ''
|
|
||||||
if err:
|
|
||||||
play_bad = True
|
play_bad = True
|
||||||
|
|
||||||
# Export market data
|
else:
|
||||||
if not self.export_market_data(data):
|
# Retry once if Companion server is unresponsive
|
||||||
err = 'Error: Exporting Market data'
|
self.w.after(int(SERVER_RETRY * 1000), lambda: self.capi_request_data(event, True))
|
||||||
play_bad = True
|
return # early exit to avoid starting cooldown count
|
||||||
|
|
||||||
self.holdofftime = querytime + companion.holdoff
|
except companion.CmdrError as e: # Companion API return doesn't match Journal
|
||||||
|
err = self.status['text'] = str(e)
|
||||||
|
play_bad = True
|
||||||
|
companion.session.invalidate()
|
||||||
|
self.login()
|
||||||
|
|
||||||
# # Companion API problem
|
except companion.ServerConnectionError as e:
|
||||||
# except companion.ServerLagging as e:
|
logger.warning(f'Exception while contacting server: {e}')
|
||||||
# err = str(e)
|
err = self.status['text'] = str(e)
|
||||||
# if retrying:
|
play_bad = True
|
||||||
# self.status['text'] = err
|
|
||||||
# play_bad = True
|
except Exception as e: # Including CredentialsError, ServerError
|
||||||
#
|
logger.debug('"other" exception', exc_info=e)
|
||||||
# else:
|
err = self.status['text'] = str(e)
|
||||||
# # Retry once if Companion server is unresponsive
|
play_bad = True
|
||||||
# self.w.after(int(SERVER_RETRY * 1000), lambda: self.capi_request_data(event, True))
|
|
||||||
# return # early exit to avoid starting cooldown count
|
|
||||||
#
|
|
||||||
# except companion.CmdrError as e: # Companion API return doesn't match Journal
|
|
||||||
# err = self.status['text'] = str(e)
|
|
||||||
# play_bad = True
|
|
||||||
# companion.session.invalidate()
|
|
||||||
# self.login()
|
|
||||||
#
|
|
||||||
# except companion.ServerConnectionError as e:
|
|
||||||
# logger.warning(f'Exception while contacting server: {e}')
|
|
||||||
# err = self.status['text'] = str(e)
|
|
||||||
# play_bad = True
|
|
||||||
#
|
|
||||||
# except Exception as e: # Including CredentialsError, ServerError
|
|
||||||
# logger.debug('"other" exception', exc_info=e)
|
|
||||||
# err = self.status['text'] = str(e)
|
|
||||||
# play_bad = True
|
|
||||||
|
|
||||||
if not err: # not self.status['text']: # no errors
|
if not err: # not self.status['text']: # no errors
|
||||||
# LANG: Time when we last obtained Frontier CAPI data
|
# LANG: Time when we last obtained Frontier CAPI data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user