We work in members.csv.new file then rename it in the unlikely event that some script...
[fourmizzz.git] / members.py
index 404e3049958a7566d4cc59b702dea72fc44408f6..438413ad94c6d12fbf79009fb03f8e4a7a6c714b 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+#
+# Last version is available at svn://svn.nirgal.com/kod/fourmizzz/
+# Licensed under GPL
 
 import os
 import time
@@ -7,6 +10,7 @@ import re
 import logging
 from datetime import datetime
 import calendar
+import random
 import urllib.request
 from http.cookiejar import CookieJar
 
@@ -16,9 +20,10 @@ import htmlentities
 """
 csv file format:
 DateTime,Action,Nick1,Grade1,OldField1,NewField1,Nick2,Grade2,OldField2,NewField2,FloodField,FieldLossPercent,Text
-Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD
+Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP, ACT, ANTHILL, TECHNO, GRADE
 
 Exemples:
+NOOP,,,,,,,,,,,Pas de changement dans le TdC
 JOIN,Nirgal,Recrue,0,571628,,,,,,,Nirgal a rejoint l'alliance avec 571 628 cm² 
 QUIT,oncleblu,Chasseur,9940181,0,,,,,,,oncleblu a quité l'alliance avec 9 940 181 cm² 
 WIN,Nirgal,Chasseur,200000,257900,,,,,,,Nirgal a gagné 57 900 cm² 
@@ -27,7 +32,16 @@ FLOOD,filoux,Passeur 003,55813865,74225093,yosemite,Passeur 004,51142304,3273107
 FLOOD,Nick1,Grade1,100,102,Nick2,Grade2,100,99,1,1.00,Nick1 a pris 1 cm² à Nick2 (1.00%)
 FLOOD,Nick1,Grade1,100,102,Nick3,Grade3,100,99,1,1.00,Nick1 a pris 1 cm² à Nick3 (1.00%)
 
-Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
+ACT,Nick1,Grade1,Actif,En vacances,,,,,,,Nick1 est maintenant en vacances
+COLON,Nick1,Grade1,False,True,,,,,,,Nick1 s'est fait colonisé
+COLON,Nick1,Grade1,True,False,,,,,,,Nick1 n'est plus colonisé
+
+ANTHILL,Nick1,Grade1,199,200,,,,,,,La fourmilière de Nick1 a atteint le niveau 200
+TECHNO,Nick1,Grade1,199,200,,,,,,,La technologie Nick1 a atteint le niveau 200
+
+GRADE,Nick1,Recrue,Recrue,Passer 806,,,,,,,Nick1 est maintenant Passeur 806
+
+Note that in last 2 FLOOD lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
 """
 
 # globals:
@@ -36,6 +50,7 @@ Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1
 #LOGIN
 #PASSWORD
 #BASE_URL
+DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
 
 def read_config(config_dir):
     '''
@@ -106,7 +121,7 @@ def number_format(i):
     return result
 
 
-def tdc_get(alliance_tag=None):
+def members_fetch(alliance_tag=None):
     #httpresponse = httpopen(BASE_URL + '/')
     #html = httpresponse.read().decode('utf-8')
     #print(html)
@@ -201,23 +216,24 @@ def members_to_field(members):
     return fields
 
 
-def field_compare(oldmembers, newmembers):
+def field_compare(oldmembers, newmembers, csvfile):
     
-    oldtdc = members_to_field(oldmembers)
-    newtdc = members_to_field(newmembers)
+    oldfield = members_to_field(oldmembers)
+    newfield = members_to_field(newmembers)
 
     changes = {}
-    txtchanges = []
-    for nick in newtdc.keys():
-        told = oldtdc.get(nick, 0)
-        tnew = newtdc[nick]
+    txtchanges2 = []
+
+    for nick in newfield.keys():
+        told = oldfield.get(nick, 0)
+        tnew = newfield[nick]
         if told == tnew:
             continue
         changes[nick] = { 'old': told, 'new': tnew, 'delta': tnew-told }
     
-    for nick in oldtdc.keys():
-        if nick not in newtdc:
-            told = oldtdc[nick]
+    for nick in oldfield.keys():
+        if nick not in newfield:
+            told = oldfield[nick]
             tnew = 0
             changes[nick] = { 'old': told, 'new': 0, 'delta': -told }
     #for nick, change in changes.items():
@@ -225,8 +241,7 @@ def field_compare(oldmembers, newmembers):
     #print
 
     if not len(changes):
-        logging.info('No changes')
-        return
+        txtchanges2.append('NOOP,,,,,,,,,,,No changes')
  
     for nick in changes.keys():
         delta = changes[nick]['delta']
@@ -237,29 +252,157 @@ def field_compare(oldmembers, newmembers):
                 continue # already done
             if changes[nick2]['delta'] != -delta:
                 continue # not the good one
-            percent = float(oldtdc[nick2] - newtdc[nick2]) / oldtdc[nick2] * 100.
-            txtchanges.append('%s a pris %s cm² à %s (%.2f%%)' % (nick, number_format(delta), nick2, percent))
+            percent = float(oldfield[nick2] - newfield[nick2]) / oldfield[nick2] * 100.
+            txtchanges2.append('FLOOD,{nick1},{grade1},{oldfield1},{newfield1},{nick2},{grade2},{oldfield2},{newfield2},{deltafield},{percent},{nick1} a pris {nicedeltafield} cm² à {nick2} ({percent:.2f}%)'.format(
+                nick1=nick,
+                grade1=newmembers[nick].get('grade', ''),
+                oldfield1=oldfield[nick],
+                newfield1=newfield[nick],
+                nick2=nick2,
+                grade2=newmembers[nick2].get('grade', ''),
+                oldfield2=oldfield[nick2],
+                newfield2=newfield[nick2],
+                deltafield=delta,
+                nicedeltafield= number_format(delta),
+                percent=percent,
+                ))
             changes[nick]['delta'] = 0
             changes[nick2]['delta'] = 0
 
-                
     for nick, change in changes.items():
         delta = change['delta']
         if delta < 0:
-            if nick in newtdc:
-                percent = float(-delta) / oldtdc[nick] * 100.
-                txtchanges.append('%s a perdu %s cm² (%.2f%%)' % (nick, number_format(-delta), percent))
+            if nick in newfield:
+                percent = float(-delta) / oldfield[nick] * 100.
+                txtchanges2.append('LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{percent},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    newfield1=newfield[nick],
+                    deltafield=-delta,
+                    nicedeltafield=number_format(-delta),
+                    percent=percent,
+                    ))
             else:
-                txtchanges.append("%s a quité l'alliance avec %s cm²" % (nick, number_format(-delta)))
+                txtchanges2.append('QUIT,{nick1},{grade1},{oldfield1},0,,,,,{deltafield},,{nick1} a quité l\'alliance avec {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=oldmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    deltafield=-delta,
+                    nicedeltafield=number_format(-delta),
+                    ))
         elif delta > 0:
-            if nick in oldtdc:
-                txtchanges.append('%s a gagné %s cm²' % (nick, number_format(delta)))
+            if nick in oldfield:
+                txtchanges2.append('WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},,{nick1} a gagné {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    newfield1=newfield[nick],
+                    deltafield=delta,
+                    nicedeltafield=number_format(delta),
+                    ))
             else:
-                txtchanges.append("%s a rejoint l'alliance avec %s cm²" % (nick, number_format(delta)))
-
-    for txtchange in txtchanges:
-        logging.info(txtchange)
-
+                txtchanges2.append('JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},,{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    newfield1=newfield[nick],
+                    deltafield=delta,
+                    nicedeltafield=number_format(delta),
+                    ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+
+def activity_compare(oldmembers, newmembers, csvfile):
+    txtchanges2 = []
+    for nick, data in newmembers.items():
+        newa = data.get('active', None)
+        olda = oldmembers.get(nick, {}).get('active', None)
+        if newa and olda and newa != olda:
+            txtchanges2.append('ACT,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est maintenant {lowernewdata1}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=olda,
+                newdata1=newa,
+                lowernewdata1=newa.lower(),
+                ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+
+def grade_compare(oldmembers, newmembers, csvfile):
+    txtchanges2 = []
+    for nick, data in newmembers.items():
+        newdata = data.get('grade', None)
+        olddata = oldmembers.get(nick, {}).get('grade', None)
+        if newdata is not None and olddata is not None and newdata != olddata:
+            txtchanges2.append('GRADE,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est désormais {grade1}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=olddata,
+                newdata1=newdata,
+                ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+
+
+def colonized_compare(oldmembers, newmembers, csvfile):
+    txtchanges2 = []
+    for nick, data in newmembers.items():
+        newc = data.get('colonized', None)
+        oldc = oldmembers.get(nick, {}).get('colonized', None)
+        if newc is not None and oldc is not None and newc != oldc:
+            if newc:
+                txt = "s'est fait colonisé"
+            else:
+                txt = "n'est plus colonisé"
+            txtchanges2.append('COLON,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} {txt}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=str(oldc),
+                newdata1=str(newc),
+                txt=txt,
+                ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+
+
+def evolution_compare(oldmembers, newmembers, csvfile):
+    txtchanges2 = []
+    for nick, data in newmembers.items():
+        newdata = data['anthill']
+        olddata = oldmembers.get(nick, {}).get('anthill', None)
+        if newdata is not None and olddata is not None and newdata != olddata:
+            txtchanges2.append('ANTHILL,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La fourmilière de {nick1} a atteint le niveau {newdata1}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=olddata,
+                newdata1=newdata,
+                ))
+        newdata = data['techno']
+        olddata = oldmembers.get(nick, {}).get('techno', None)
+        if newdata is not None and olddata is not None and newdata != olddata:
+            txtchanges2.append('TECHNO,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La technologie de {nick1} a atteint le niveau {newdata1}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=olddata,
+                newdata1=newdata,
+                ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
 
 if __name__ == '__main__':
     from optparse import OptionParser
@@ -271,13 +414,27 @@ if __name__ == '__main__':
         action='store_true', dest='quiet', default=False,
         help="quiet mode")
     parser.add_option('-a', '--alliance',
-        dest='alliance',
-        help="alliance tag. default is to process player own alliance.")
+        dest='alliance', metavar="TAG",
+        help="Default is to process player own alliance.")
     parser.add_option('--dir',
         dest='directory', default='.fourmizzz',
         help="Working directory, including config.py, archives and results. "
              "Relative to HOME unless it starts with /. "
              "Default is '%default'.")
+    parser.add_option('--quick',
+        action='store_true', dest='quick', default=False,
+        help="Don't sleep a random delay before hiting the server. "
+             "The programme usually wait between 0 and 59s, so that "
+             "there is no pick usage on a round minute")
+    parser.add_option('--members-name',
+        dest='archive_filename', metavar='FILENAME', default="archive.py",
+        help="Override filename containing old data. "
+        "Default is '%default' in Working directory. "
+        "See --dir.")
+    parser.add_option('--new-members-name',
+        dest='new_archive_filename', metavar='FILENAME',
+        help="Read new members data from a file. "
+        "Default is to fetch data from antzzz server.")
     parser.add_option('--dry-run',
         action='store_true', dest='dryrun', default=False,
         help="don't store result in archives.")
@@ -291,17 +448,19 @@ if __name__ == '__main__':
         loglevel = logging.INFO
     
     HOME = os.environ['HOME']
-    if options.directory[0] == '/':
+    if options.directory[0] == os.path.sep: # '/' on linux
         BASE_DIRECTORY = options.directory
     else:
         BASE_DIRECTORY = os.path.join(HOME, options.directory)
+    logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
 
-    result_filename = os.path.join(BASE_DIRECTORY, 'results.log')
-    logging.basicConfig(filename=result_filename, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
+    if not options.quick:
+        delay = random.randint(0,59)
+        sleep(delay)
 
     read_config(BASE_DIRECTORY)
 
-    members_filename = os.path.join(BASE_DIRECTORY, 'archive.py')
+    members_filename = os.path.join(BASE_DIRECTORY, options.archive_filename)
     oldmembers = None
     try:
         f = open(members_filename, mode='r+', encoding='utf-8')
@@ -314,13 +473,47 @@ if __name__ == '__main__':
     else:
         oldmembers = eval(f.read())
     
-    newmembers = tdc_get(options.alliance)
+    if options.new_archive_filename:
+        new_archive_filename = os.path.join(BASE_DIRECTORY, options.new_archive_filename)
+        newf = open(new_archive_filename, mode='r', encoding='utf8')
+        newmembers = eval(newf.read())
+    else:
+        newmembers = members_fetch(options.alliance)
 
     if oldmembers is not None:
-        field_compare(oldmembers, newmembers)
+        csvfilename = os.path.join(BASE_DIRECTORY, 'results.csv')
+        csvfile = open(csvfilename, mode='a', encoding='utf-8')
+        field_compare(oldmembers, newmembers, csvfile)
+        grade_compare(oldmembers, newmembers, csvfile)
+        activity_compare(oldmembers, newmembers, csvfile)
+        colonized_compare(oldmembers, newmembers, csvfile)
+        evolution_compare(oldmembers, newmembers, csvfile)
     
     # Save archive only after processing, just in case it crashes
     if not options.dryrun:
         f.seek(0)
         f.write(repr(newmembers))
         f.truncate()
+        f.close()
+
+        # We work in members.csv.new file then rename it
+        # In the unlikely event that some script is reading it
+        # while we updated it
+        members_csvfilename = os.path.join(BASE_DIRECTORY, "members.csv")
+        members_csvfilename_new = members_csvfilename + '.new'
+        f = open(members_csvfilename_new, mode='w', encoding='utf-8')
+        f.write('grade,nick,field,techno,anthill,active,colonized\n')
+        for nick, data in newmembers.items():
+            data = [
+                data['grade'],
+                nick,
+                data['field'],
+                data['techno'],
+                data['anthill'],
+                data['active'],
+                data['colonized'],
+                ]
+            data = [ str(x) for x in data ]
+            f.write(','.join(data) + '\n')
+        f.close()
+        os.rename(members_csvfilename_new, members_csvfilename)