Also write member data as a members.csv
[fourmizzz.git] / members.py
index d3d3aa63261dd6ed774abd06e9f56a73c41b6428..4923a4878b7c24b3e77b0b72174456de812c17d2 100755 (executable)
@@ -10,6 +10,7 @@ import re
 import logging
 from datetime import datetime
 import calendar
+import random
 import urllib.request
 from http.cookiejar import CookieJar
 
@@ -19,10 +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, NOOP
+Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP, ACT, ANTHILL, TECHNO, GRADE
 
 Exemples:
-NOOP,,,,,,,,,,,No changes
+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² 
@@ -31,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:
@@ -212,7 +222,6 @@ def field_compare(oldmembers, newmembers, csvfile):
     newfield = members_to_field(newmembers)
 
     changes = {}
-    txtchanges = []
     txtchanges2 = []
 
     for nick in newfield.keys():
@@ -232,7 +241,6 @@ def field_compare(oldmembers, newmembers, csvfile):
     #print
 
     if not len(changes):
-        txtchanges.append('No changes')
         txtchanges2.append('NOOP,,,,,,,,,,,No changes')
  
     for nick in changes.keys():
@@ -245,7 +253,6 @@ def field_compare(oldmembers, newmembers, csvfile):
             if changes[nick2]['delta'] != -delta:
                 continue # not the good one
             percent = float(oldfield[nick2] - newfield[nick2]) / oldfield[nick2] * 100.
-            txtchanges.append('%s a pris %s cm² à %s (%.2f%%)' % (nick, number_format(delta), nick2, percent))
             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', ''),
@@ -267,7 +274,6 @@ def field_compare(oldmembers, newmembers, csvfile):
         if delta < 0:
             if nick in newfield:
                 percent = float(-delta) / oldfield[nick] * 100.
-                txtchanges.append('%s a perdu %s cm² (%.2f%%)' % (nick, number_format(-delta), percent))
                 txtchanges2.append('LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{percent},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)'.format(
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
@@ -278,7 +284,6 @@ def field_compare(oldmembers, newmembers, csvfile):
                     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', ''),
@@ -288,7 +293,6 @@ def field_compare(oldmembers, newmembers, csvfile):
                     ))
         elif delta > 0:
             if nick in oldfield:
-                txtchanges.append('%s a gagné %s cm²' % (nick, number_format(delta)))
                 txtchanges2.append('WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},,{nick1} a gagné {nicedeltafield} cm²'.format(
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
@@ -296,10 +300,8 @@ def field_compare(oldmembers, newmembers, csvfile):
                     newfield1=newfield[nick],
                     deltafield=delta,
                     nicedeltafield=number_format(delta),
-                    percent=percent,
                     ))
             else:
-                txtchanges.append("%s a rejoint l'alliance avec %s cm²" % (nick, number_format(delta)))
                 txtchanges2.append('JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},,{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²'.format(
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
@@ -308,11 +310,99 @@ def field_compare(oldmembers, newmembers, csvfile):
                     nicedeltafield=number_format(delta),
                     ))
 
-    for txtchange in txtchanges:
-        logging.info(txtchange)
+    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:
-        csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+        logging.info(txtchange.split(',')[-1])
+        if not options.dryrun:
+            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
 
 if __name__ == '__main__':
     from optparse import OptionParser
@@ -331,6 +421,11 @@ if __name__ == '__main__':
         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. "
@@ -343,9 +438,6 @@ if __name__ == '__main__':
     parser.add_option('--dry-run',
         action='store_true', dest='dryrun', default=False,
         help="don't store result in archives.")
-    parser.add_option('--stdout',
-        action='store_true', dest='stdout', default=False,
-        help="Print result on stdout rather that in results.log")
     options, args = parser.parse_args()
 
     if options.debug:
@@ -356,16 +448,15 @@ 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)
 
-    if options.stdout:
-        logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
-    else:
-        result_filename = os.path.join(BASE_DIRECTORY, 'results.log')
-        logging.basicConfig(filename=result_filename, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
+    if not options.quick:
+        delay = random.randint(0,59)
+        sleep(delay)
 
     read_config(BASE_DIRECTORY)
 
@@ -393,9 +484,31 @@ if __name__ == '__main__':
         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()
+
+        members_csvfilename = os.path.join(BASE_DIRECTORY, "members.csv")
+        f = open(members_csvfilename, 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()