We work in members.csv.new file then rename it in the unlikely event that some script...
[fourmizzz.git] / members.py
index 408037927fd997893f177330f620e4354fcb836c..438413ad94c6d12fbf79009fb03f8e4a7a6c714b 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, ANTHILL, TECHNO
+Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP, ACT, ANTHILL, TECHNO, GRADE
 
 Exemples:
 NOOP,,,,,,,,,,,Pas de changement dans le TdC
@@ -39,7 +39,9 @@ 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
 
-Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
+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:
@@ -332,6 +334,25 @@ def activity_compare(oldmembers, newmembers, csvfile):
         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():
@@ -463,6 +484,7 @@ 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)
@@ -472,3 +494,26 @@ if __name__ == '__main__':
         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)