Parse (only first) state from multi-state faction data.
authorCMDR furrycat <elite@furrycat.net>
Sun, 23 Dec 2018 16:00:30 +0000 (16:00 +0000)
committerCMDR furrycat <elite@furrycat.net>
Sun, 23 Dec 2018 16:00:30 +0000 (16:00 +0000)
plugin/faction/faction.py

index 1885154..3318b41 100644 (file)
@@ -1134,8 +1134,21 @@ class Faction(DBConnection):
       channels.add(channel)
       announce[system] = channels
 
+  async def parse_states(self, data):
+    if 'ActiveStates' in data:
+      return [state['State'] for state in data['ActiveStates']]
+    elif 'FactionState' in data:
+      return [data['FactionState']]
+    else:
+      log.error("Can't find ActiveStates or FactionState in {}".format(data))
+      return ['None']
+
   async def update_factions(self, system, factions, iso8601):
-    influence = ', '.join(['{} {:.2f}% {}'.format(faction['Name'], faction['Influence'] * 100.0, faction['FactionState']) for faction in reversed(sorted(factions, key = lambda faction: faction['Influence']))])
+    influences = []
+    for faction in reversed(sorted(factions, key = lambda faction: faction['Influence'])):
+      states = await self.parse_states(faction)
+      influences.append('{} {:.2f}% {}'.format(faction['Name'], faction['Influence'] * 100.0, states[0]))
+    influence = ', '.join(influences)
     log.debug('{} factions: {}'.format(system, influence))
     try:
       timestamp = bot.parse_iso8601(iso8601)
@@ -1183,7 +1196,8 @@ class Faction(DBConnection):
         trending_states += map(lambda s: (eddb.get_state_id(s['State']), s['Trend'], 'true'), faction['PendingStates'])
       if 'RecoveringStates' in faction:
         trending_states += map(lambda s: (eddb.get_state_id(s['State']), s['Trend'], 'false'), faction['RecoveringStates'])
-      state_id = eddb.get_state_id(faction['FactionState'])
+      active_states = await self.parse_states(faction)
+      state_id = [eddb.get_state_id(state) for state in active_states][0]
       if state_id is None:
         state_id = eddb.get_state_id('None')
       name = faction['Name']