Also write member data as a members.csv
[fourmizzz.git] / members.py
index 56dccfba8d266c5afee8ec0e7cbe5b270317a075..4923a4878b7c24b3e77b0b72174456de812c17d2 100755 (executable)
@@ -20,7 +20,7 @@ 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,,,,,,,,,,,Pas de changement dans le TdC
@@ -36,7 +36,12 @@ 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:
@@ -307,7 +312,8 @@ def field_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 activity_compare(oldmembers, newmembers, csvfile):
     txtchanges2 = []
@@ -325,7 +331,27 @@ 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 = []
@@ -347,7 +373,36 @@ def colonized_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 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
@@ -429,11 +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()