From 979162ea3d621c6d1badc2c9e8b00b58eb3ee641 Mon Sep 17 00:00:00 2001
From: Jonathan Harris <jonathan@marginal.org.uk>
Date: Tue, 31 Oct 2017 10:37:24 +0000
Subject: [PATCH] Include ship id and name in ship locations

---
 companion.py |  2 +-
 stats.py     | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/companion.py b/companion.py
index 4f3ce0e8..9221e482 100644
--- a/companion.py
+++ b/companion.py
@@ -86,7 +86,7 @@ def listify(thing):
     if thing is None:
         return []	# data is not present
     elif isinstance(thing, list):
-        return thing	# array is not sparse
+        return list(thing)	# array is not sparse
     elif isinstance(thing, dict):
         retval = []
         for k,v in thing.iteritems():
diff --git a/stats.py b/stats.py
index 95eb6a3c..dd006f24 100644
--- a/stats.py
+++ b/stats.py
@@ -148,6 +148,7 @@ def export_status(data, filename):
     h.close()
 
 
+# Returns id,name,shipName,system,station,value
 def ships(data):
 
     ships = companion.listify(data.get('ships'))
@@ -158,13 +159,14 @@ def ships(data):
 
         if not data['commander'].get('docked'):
             # Set current system, not last docked
-            return [ (ship_map.get(ships[0]['name'].lower(), ships[0]['name']), data['lastSystem']['name'], '') ] + [ (ship_map.get(ship['name'].lower(), ship['name']), ship['starsystem']['name'], ship['station']['name'], str(ship['value']['total'])) for ship in ships[1:] if ship]
+            return ([ (str(ships[0]['id']), ship_map.get(ships[0]['name'].lower(), ships[0]['name']), ships[0].get('shipName', ''), data['lastSystem']['name'], '', str(ships[0]['value']['total'])) ] +
+                    [ (str(ship['id']), ship_map.get(ship['name'].lower(), ship['name']), ship.get('shipName', ''), ship['starsystem']['name'], ship['station']['name'], str(ship['value']['total'])) for ship in ships[1:] if ship])
 
-    return [ (ship_map.get(ship['name'].lower(), ship['name']), ship['starsystem']['name'], ship['station']['name'], str(ship['value']['total'])) for ship in ships if ship]
+    return [ (str(ship['id']), ship_map.get(ship['name'].lower(), ship['name']), ship.get('shipName', ''), ship['starsystem']['name'], ship['station']['name'], str(ship['value']['total'])) for ship in ships if ship]
 
 def export_ships(data, filename):
     h = open(filename, 'wt')
-    h.write('Ship,System,Station,Value\n')
+    h.write('Id,Ship,Name,System,Station,Value\n')
     for thing in ships(data):
         h.write(','.join(thing) + '\n')
     h.close()
@@ -249,13 +251,14 @@ class StatsResults(tk.Toplevel):
 
         page = self.addpage(notebook, [
             _('Ship'),		# Status dialog subtitle
+            '',
             _('System'),	# Main window
             _('Station'),	# Status dialog subtitle
             _('Value'),		# Status dialog subtitle - CR value of ship
         ])
         shiplist = ships(data)
         for thing in shiplist:
-            self.addpagerow(page, thing)
+            self.addpagerow(page, thing[1:])		# skip id
         ttk.Frame(page).grid(pady=5)			# bottom spacer
         notebook.add(page, text=_('Ships'))		# Status dialog title