From 88f323d36e1757cbd20d0ca6b877fac01f021ace Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Sat, 10 Feb 2018 19:21:55 +0000 Subject: [PATCH] Switch EDShipyard import to Loadout event --- EDMarketConnector.py | 16 +++++++++------- edshipyard.py | 7 +++++-- monitor.py | 19 +++++++++++++++++++ plugins/edsm.py | 13 +++++++++---- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index aed8b007..b3498fb8 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -709,6 +709,14 @@ class AppWindow: if not monitor.cmdr or not monitor.mode: return False # In CQC - do nothing + if config.getint('shipyard') == config.SHIPYARD_EDSHIPYARD: + return edshipyard.url(monitor.is_beta) + elif config.getint('shipyard') == config.SHIPYARD_CORIOLIS: + pass # Fall through + else: + assert False, config.getint('shipyard') + return False + self.status['text'] = _('Fetching data...') self.w.update_idletasks() try: @@ -734,13 +742,7 @@ class AppWindow: self.status['text'] = _('Error: Frontier server is lagging') # Raised when Companion API server is returning old data, e.g. when the servers are too busy else: self.status['text'] = '' - if config.getint('shipyard') == config.SHIPYARD_EDSHIPYARD: - return edshipyard.url(data, monitor.is_beta) - elif config.getint('shipyard') == config.SHIPYARD_CORIOLIS: - return coriolis.url(data, monitor.is_beta) - else: - assert False, config.getint('shipyard') - return False + return coriolis.url(data, monitor.is_beta) def cooldown(self): if time() < self.holdofftime: diff --git a/edshipyard.py b/edshipyard.py index 8274ab51..31a1b5b9 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -13,6 +13,7 @@ import gzip from config import config import companion import outfitting +from monitor import monitor # Map API ship names to E:D Shipyard ship names ship_map = dict(companion.ship_map) @@ -162,9 +163,11 @@ def export(data, filename=None): # Return a URL for the current ship -def url(data, is_beta): +def url(is_beta): - string = json.dumps(companion.ship(data), ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8') # most compact representation + string = json.dumps(monitor.ship(), ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8') # most compact representation + if not string: + return False out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode='w') as f: diff --git a/monitor.py b/monitor.py index 6fbc2115..179d07cc 100644 --- a/monitor.py +++ b/monitor.py @@ -625,5 +625,24 @@ class EDLogs(FileSystemEventHandler): return False + # Return a subset of the received data describing the current ship as a Loadout event + def ship(self): + if not self.state['Modules']: + return None + + d = OrderedDict([ + ('timestamp', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())), + ('event', 'Loadout'), + ('Ship', self.state['ShipType']), + ('ShipID', self.state['ShipID']), + ]) + for thing in ['ShipName', 'ShipIdent', 'HullValue', 'ModulesValue', 'Rebuy']: + if self.state[thing]: + d[thing] = self.state[thing] + d['Modules'] = self.state['Modules'].values() + + return d + + # singleton monitor = EDLogs() diff --git a/plugins/edsm.py b/plugins/edsm.py index 5cb0b824..05cfb004 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -34,6 +34,7 @@ this.session = requests.Session() this.queue = Queue() # Items to be sent to EDSM by worker thread this.discardedEvents = [] # List discarded events from EDSM this.lastship = None # Description of last ship that we sent to EDSM +this.lastloadout = None # Description of last ship that we sent to EDSM this.lastlookup = False # whether the last lookup succeeded # Game state @@ -235,6 +236,14 @@ def journal_entry(cmdr, is_beta, system, station, entry, state): this.queue.put((cmdr, entry)) + if entry['event'] == 'Loadout' and 'EDShipyard' not in this.discardedEvents: + url = edshipyard.url(is_beta) + if this.lastloadout != url: + this.lastloadout = url + this.queue.put((cmdr, { + 'event': 'EDShipyard', 'timestamp': entry['timestamp'], '_shipId': state['ShipID'], 'url': this.lastloadout + })) + # Update system data def cmdr_data(data, is_beta): @@ -262,10 +271,6 @@ def cmdr_data(data, is_beta): this.queue.put((cmdr, { 'event': 'Coriolis', 'timestamp': timestamp, '_shipId': data['ship']['id'], 'url': coriolis.url(data, is_beta) })) - if 'EDShipyard' not in this.discardedEvents: - this.queue.put((cmdr, { - 'event': 'EDShipyard', 'timestamp': timestamp, '_shipId': data['ship']['id'], 'url': edshipyard.url(data, is_beta) - })) this.lastship = ship