Also write member data as a members.csv
[fourmizzz.git] / members.py
index 7192c28907cae1cdc9ebe169770fc6cea0a97268..4923a4878b7c24b3e77b0b72174456de812c17d2 100755 (executable)
@@ -20,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, ACT
+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² 
@@ -32,9 +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%)
 
-ACT,Nick1,Grade1,100,100,,,,,,,Nick1 est en vacances
+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é
 
-Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
+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:
@@ -215,7 +222,6 @@ def field_compare(oldmembers, newmembers, csvfile):
     newfield = members_to_field(newmembers)
 
     changes = {}
-    txtchanges = []
     txtchanges2 = []
 
     for nick in newfield.keys():
@@ -235,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():
@@ -248,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', ''),
@@ -270,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', ''),
@@ -281,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', ''),
@@ -291,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', ''),
@@ -301,7 +302,6 @@ def field_compare(oldmembers, newmembers, csvfile):
                     nicedeltafield=number_format(delta),
                     ))
             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', ''),
@@ -310,11 +310,10 @@ def field_compare(oldmembers, newmembers, csvfile):
                     nicedeltafield=number_format(delta),
                     ))
 
-    for txtchange in txtchanges:
-        logging.info(txtchange)
-
     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')
 
 def activity_compare(oldmembers, newmembers, csvfile):
     txtchanges2 = []
@@ -332,7 +331,78 @@ def activity_compare(oldmembers, newmembers, csvfile):
 
     for txtchange in txtchanges2:
         logging.info(txtchange.split(',')[-1])
-        csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+        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
@@ -414,10 +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()