From 1dc6d04954c7b9b0bad537510700299db50611c6 Mon Sep 17 00:00:00 2001
From: Jonathan Harris <github@marginal.org.uk>
Date: Fri, 25 Jan 2019 18:05:42 +0000
Subject: [PATCH] Factor in Horizons flag when de-duplicating

---
 plugins/eddn.py | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/plugins/eddn.py b/plugins/eddn.py
index 827d36cc..c866945d 100644
--- a/plugins/eddn.py
+++ b/plugins/eddn.py
@@ -198,9 +198,13 @@ class EDDN:
     def export_outfitting(self, data, is_beta):
         economies = data['lastStarport'].get('economies') or {}
         modules = data['lastStarport'].get('modules') or {}
-        horizons = any(economy['name'] == 'Colony' for economy in economies.itervalues()) or any(module.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for module in modules.itervalues())	# Should always hit Int_PlanetApproachSuite other than at engineer bases
+        ships = data['lastStarport'].get('ships') or { 'shipyard_list': {}, 'unavailable_list': [] }
+        # Horizons flag - will hit at least Int_PlanetApproachSuite other than at engineer bases ("Colony"), prison or rescue Megaships, or under Pirate Attack etc
+        horizons = (any(economy['name'] == 'Colony' for economy in economies.itervalues()) or
+                    any(module.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for module in modules.itervalues()) or
+                    any(ship.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for ship in (ships['shipyard_list'] or {}).values()))
         outfitting = sorted([self.MODULE_RE.sub(lambda m: m.group(0).capitalize(), module['name'].lower()) for module in modules.itervalues() if self.MODULE_RE.search(module['name']) and module.get('sku') in [None, 'ELITE_HORIZONS_V_PLANETARY_LANDINGS'] and module['name'] != 'Int_PlanetApproachSuite'])
-        if outfitting and this.outfitting != outfitting:	# Don't send empty modules list - schema won't allow it
+        if outfitting and this.outfitting != (horizons, outfitting):	# Don't send empty modules list - schema won't allow it
             self.send(data['commander']['name'], {
                 '$schemaRef' : 'https://eddn.edcd.io/schemas/outfitting/2' + (is_beta and '/test' or ''),
                 'message'    : OrderedDict([
@@ -212,15 +216,17 @@ class EDDN:
                     ('modules',     outfitting),
                 ]),
             })
-        this.outfitting = outfitting
+        this.outfitting = (horizons, outfitting)
 
     def export_shipyard(self, data, is_beta):
         economies = data['lastStarport'].get('economies') or {}
         modules = data['lastStarport'].get('modules') or {}
         ships = data['lastStarport'].get('ships') or { 'shipyard_list': {}, 'unavailable_list': [] }
-        horizons = any(economy['name'] == 'Colony' for economy in economies.itervalues()) or any(module.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for module in modules.itervalues()) or any(ship.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for ship in (ships['shipyard_list'] or {}).values())
+        horizons = (any(economy['name'] == 'Colony' for economy in economies.itervalues()) or
+                    any(module.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for module in modules.itervalues()) or
+                    any(ship.get('sku') == 'ELITE_HORIZONS_V_PLANETARY_LANDINGS' for ship in (ships['shipyard_list'] or {}).values()))
         shipyard = sorted([ship['name'].lower() for ship in (ships['shipyard_list'] or {}).values() + ships['unavailable_list']])
-        if shipyard and this.shipyard != shipyard:	# Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard.
+        if shipyard and this.shipyard != (horizons, shipyard):	# Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard.
             self.send(data['commander']['name'], {
                 '$schemaRef' : 'https://eddn.edcd.io/schemas/shipyard/2' + (is_beta and '/test' or ''),
                 'message'    : OrderedDict([
@@ -232,7 +238,7 @@ class EDDN:
                     ('ships',       shipyard),
                 ]),
             })
-        this.shipyard = shipyard
+        this.shipyard = (horizons, shipyard)
 
     def export_journal_commodities(self, cmdr, is_beta, entry):
         items = entry.get('Items') or []
@@ -262,8 +268,9 @@ class EDDN:
 
     def export_journal_outfitting(self, cmdr, is_beta, entry):
         modules = entry.get('Items') or []
+        horizons = entry.get('Horizons', False)
         outfitting = sorted([self.MODULE_RE.sub(lambda m: m.group(0).capitalize(), module['Name']) for module in modules if module['Name'] != 'int_planetapproachsuite'])
-        if outfitting and this.outfitting != outfitting:	# Don't send empty modules list - schema won't allow it
+        if outfitting and this.outfitting != (horizons, outfitting):	# Don't send empty modules list - schema won't allow it
             self.send(cmdr, {
                 '$schemaRef' : 'https://eddn.edcd.io/schemas/outfitting/2' + (is_beta and '/test' or ''),
                 'message'    : OrderedDict([
@@ -271,16 +278,17 @@ class EDDN:
                     ('systemName',  entry['StarSystem']),
                     ('stationName', entry['StationName']),
                     ('marketId',    entry['MarketID']),
-                    ('horizons',    entry['Horizons']),
+                    ('horizons',    horizons),
                     ('modules',     outfitting),
                 ]),
             })
-        this.outfitting = outfitting
+        this.outfitting = (horizons, outfitting)
 
     def export_journal_shipyard(self, cmdr, is_beta, entry):
         ships = entry.get('PriceList') or []
+        horizons = entry.get('Horizons', False)
         shipyard = sorted([ship['ShipType'] for ship in ships])
-        if shipyard and this.shipyard != shipyard:	# Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard.
+        if shipyard and this.shipyard != (horizons, shipyard):	# Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard.
             self.send(cmdr, {
                 '$schemaRef' : 'https://eddn.edcd.io/schemas/shipyard/2' + (is_beta and '/test' or ''),
                 'message'    : OrderedDict([
@@ -288,11 +296,11 @@ class EDDN:
                     ('systemName',  entry['StarSystem']),
                     ('stationName', entry['StationName']),
                     ('marketId',    entry['MarketID']),
-                    ('horizons',    entry['Horizons']),
+                    ('horizons',    horizons),
                     ('ships',       shipyard),
                 ]),
             })
-        this.shipyard = shipyard
+        this.shipyard = (horizons, shipyard)
 
     def export_journal_entry(self, cmdr, is_beta, entry):
         msg = {