More influence reports.
authorCMDR furrycat <elite@furrycat.net>
Thu, 13 Apr 2017 09:15:15 +0000 (10:15 +0100)
committerCMDR furrycat <elite@furrycat.net>
Thu, 13 Apr 2017 09:16:42 +0000 (10:16 +0100)
Show influence in one or more systems.
Show influence for the system without requiring a faction name to check.
Report influence to tracked channel when new data is received.

plugin/faction/faction.py

index d44d177..c59ec9a 100644 (file)
@@ -175,7 +175,7 @@ class Faction(DBConnection):
         'faction [show] FACTION',
         'faction cache',
         'faction edit ID',
-        'faction influence FACTION [SYSTEM]',
+        'faction influence FACTION [SYSTEM ...]',
         'faction list',
         'faction track FACTION',
         'faction untrack FACTION',
@@ -192,10 +192,11 @@ class Faction(DBConnection):
       ]
     elif command == 'influence':
       lines = [
-        'Show influence of faction:',
+        'Show influence of faction or system:',
         '```',
         'faction influence FACTION',
-        'faction influence FACTION SYSTEM',
+        'faction influence FACTION SYSTEM [SYSTEM ...]',
+        'faction influence in SYSTEM [SYSTEM ...]',
         '```',
         'If no `SYSTEM` is supplied the influence in all known systems will be reported.'
       ]
@@ -738,12 +739,28 @@ class Faction(DBConnection):
 
     log.info('Updating influence in {}: {}'.format(system, influence))
     update = False
+    announce = {}
     for faction in factions:
       state_id = eddb.get_state_id(faction['FactionState'])
       if state_id is None:
         state_id = eddb.get_state_id('None')
       name = faction['Name']
       faction_id = faction_ids[name]
+      for tracked in self.tracked_factions:
+        if tracked.name == name:
+          channel = None
+          if tracked.is_private:
+            for server in client.servers:
+              try:
+                member = server.get_member(tracked.member_id)
+                channel = await client.start_private_message(member)
+              except:
+                pass
+          else:
+            channel = client.get_channel(tracked.channel_id)
+          if channel is not None:
+            announce[system] = channel
+            break
       cursor = self.query('insert or ignore into faction_influence values (?, ?, ?, ?, ?, ?)', [checksum, system_id, timestamp, faction_id, faction['Influence'], state_id])
       if not cursor.rowcount:
         log.debug('No update for {} with checksum {}'.format(system, checksum))
@@ -753,23 +770,31 @@ class Faction(DBConnection):
       cursor = self.query('update faction_influence set timestamp=? where checksum=? and eddb_id=? and faction_id>0', [timestamp, checksum, system_id])
       if not cursor.rowcount:
         log.warning('Failed to update timestamp for {} with checksum {}'.format(system, checksum))
+    else:
+      for system, channel in announce.items():
+        await self.report_faction_influence(channel, system = system, update = True, yelp_if_no_data = False)
     self.dbh.commit()
 
     self.close_db()
     return True
 
-  async def report_faction_influence(self, destination, *, id = None, name = None, system = None, yelp_if_no_data = True):
+  async def report_faction_influence(self, destination, *, id = None, name = None, system = None, update = False, yelp_if_no_data = True):
     if id is None:
       if name is None:
-        log.error("Can't report influence without a name or ID!")
-        return False
-      id = self.get_faction_id(name)
-    if id is None:
-      log.error("Can't report influence of unknown faction {}".format(name))
-      return False
+        if system is None:
+          log.error("Can't report influence without a faction name/ID or system!")
+          return False
+      else:
+        id = self.get_faction_id(name)
+        if id is None:
+          log.error("Can't report influence of unknown faction {}".format(name))
+          return False
 
-    sql = 'select s.name as system_name, i.timestamp as timestamp, i.faction_id as faction_id, n.name as faction_name, i.influence as influence, i.state_id as state_id from faction_influence i, faction_systems s, faction_names n, (select eddb_id, max(timestamp) as timestamp from faction_influence group by eddb_id) j where i.faction_id=n.faction_id and i.eddb_id=s.eddb_id and i.timestamp=j.timestamp and i.eddb_id in (select distinct eddb_id from faction_influence where faction_id=?)'
-    params = [id]
+    sql = 'select s.name as system_name, i.timestamp as timestamp, i.faction_id as faction_id, n.name as faction_name, i.influence as influence, i.state_id as state_id from faction_influence i, faction_systems s, faction_names n, (select eddb_id, max(timestamp) as timestamp from faction_influence group by eddb_id) j where i.faction_id=n.faction_id and i.eddb_id=s.eddb_id and i.timestamp=j.timestamp'
+    params = []
+    if id is not None:
+      sql += ' and i.eddb_id in (select distinct eddb_id from faction_influence where faction_id=?)'
+      params.append(id)
     if system is not None:
       sql += ' and s.name=?'
       params.append(system)
@@ -780,9 +805,10 @@ class Faction(DBConnection):
     for row in cursor.fetchall():
       if row['system_name'] != last_system:
         if len(lines):
+          lines.append('')
           await bot.say_many(destination, lines)
           lines = []
-        lines.append('Influence in **{}** at {}:'.format(row['system_name'], bot.iso8601(row['timestamp'])))
+        lines.append('{}__Influence in **{}** at *{}*__'.format('News flash: ' if update else '', row['system_name'], bot.iso8601(row['timestamp'])))
         lines.append('')
       last_system = row['system_name']
       line = '{} {:.2f}% ({})'.format(row['faction_name'], row['influence'] * 100.0, eddb.state_name(row['state_id']))
@@ -790,6 +816,7 @@ class Faction(DBConnection):
         line = '**{}**'.format(line)
       lines.append(line)
     if len(lines):
+      lines.append('')
       await bot.say_many(destination, lines)
       ret = True
     else:
@@ -801,9 +828,13 @@ class Faction(DBConnection):
 
   async def show_influence_report(self, message, args):
     name = args[0]
+    if name == 'in':
+      name = None
     if len(args) > 1:
-      system = args[1]
+      systems = args[1:]
     else:
-      system = None
-    result = await self.report_faction_influence(message.channel, name = name, system = system)
+      systems = [None]
+    result = True
+    for system in systems:
+      result &= await self.report_faction_influence(message.channel, name = name, system = system)
     return result