From 27fe43bf8ca5db0f69b42a6b50c85fbd1c008e9f Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Mon, 9 Aug 2021 04:37:23 +0300 Subject: [PATCH] WIP: implement journal_entry_cqc() journal_entry_cqc function in plugin's api allow plugins to recieve journal events when player are in CQC --- EDMarketConnector.py | 13 ++++++++++++- monitor.py | 7 ++++++- plug.py | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index d6eaa750..9a076a73 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -872,7 +872,8 @@ class AppWindow(object): play_bad = False err: Optional[str] = None - if not monitor.cmdr or not monitor.mode or monitor.state['Captain'] or not monitor.system: + if not monitor.cmdr or not monitor.mode or monitor.state['Captain'] or not monitor.system or \ + monitor.mode == 'CQC': return # In CQC or on crew - do nothing if companion.session.state == companion.Session.STATE_AUTH: @@ -1171,6 +1172,15 @@ class AppWindow(object): config.set('cmdrs', config.get_list('cmdrs', default=[]) + [monitor.cmdr]) self.login() + if monitor.mode == 'CQC': + err = plug.notify_journal_entry_cqc(monitor.cmdr, monitor.is_beta, entry, monitor.state) + if err: + self.status['text'] = err + if not config.get_int('hotkey_mute'): + hotkeymgr.play_bad() + + return # in CQC + if not entry['event'] or not monitor.mode: # logger.trace('Startup or in CQC, returning') return # Startup or in CQC @@ -1335,6 +1345,7 @@ class AppWindow(object): self.button['text'] = self.theme_button['text'] = _('Update') # LANG: Update button in main window self.button['state'] = self.theme_button['state'] = (monitor.cmdr and monitor.mode and + # and monitor.mode == 'CQC' and not monitor.state['Captain'] and monitor.system and tk.NORMAL or tk.DISABLED) diff --git a/monitor.py b/monitor.py index 01a614e7..98d152b5 100644 --- a/monitor.py +++ b/monitor.py @@ -494,6 +494,7 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below entry['timestamp'] # we expect this to exist # TODO: replace with assert? or an if key in check event_type = entry['event'].lower() + # logger.debug(f'Monitor event: {entry["event"]}') if event_type == 'fileheader': self.live = False @@ -529,7 +530,11 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below # Odyssey: bool self.cmdr = entry['Commander'] # 'Open', 'Solo', 'Group', or None for CQC (and Training - but no LoadGame event) - self.mode = entry.get('GameMode') + if not entry.get('Ship'): # TODO: Test with loading to CQC right from main game + logger.debug(f'loadgame to cqc: {entry}') + self.mode = 'CQC' + else: + self.mode = entry.get('GameMode') self.group = entry.get('Group') self.planet = None self.system = None diff --git a/plug.py b/plug.py index af4a50ac..ec31ad54 100644 --- a/plug.py +++ b/plug.py @@ -312,6 +312,29 @@ def notify_newdata(data, is_beta): return error +def notify_journal_entry_cqc(cmdr, is_beta, entry, state): + """ + Send a journal entry to each plugin. + :param cmdr: The Cmdr name, or None if not yet known + :param entry: The journal entry as a dictionary + :param state: A dictionary containing info about the Cmdr, current ship and cargo + :param is_beta: whether the player is in a Beta universe. + :returns: Error message from the first plugin that returns one (if any) + """ + + error = None + for plugin in PLUGINS: + journal_entry = plugin._get_func('journal_entry_cqc') + if journal_entry: + try: + # Pass a copy of the journal entry in case the callee modifies it + newerror = journal_entry(cmdr, is_beta, dict(entry), dict(state)) + error = error or newerror + except Exception as e: + logger.exception(f'Plugin "{plugin.name}" failed') + return error + + def show_error(err): """ Display an error message in the status line of the main window.