We work in members.csv.new file then rename it in the unlikely event that some script...
[fourmizzz.git] / members.py
index 29a939ef43e3592c8c9f1d11a691d03c5edc72c1..438413ad94c6d12fbf79009fb03f8e4a7a6c714b 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/env python3
 # -*- coding: utf-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
 
 import os
 import time
@@ -7,6 +10,7 @@ import re
 import logging
 from datetime import datetime
 import calendar
 import logging
 from datetime import datetime
 import calendar
+import random
 import urllib.request
 from http.cookiejar import CookieJar
 
 import urllib.request
 from http.cookiejar import CookieJar
 
@@ -16,26 +20,39 @@ import htmlentities
 """
 csv file format:
 DateTime,Action,Nick1,Grade1,OldField1,NewField1,Nick2,Grade2,OldField2,NewField2,FloodField,FieldLossPercent,Text
 """
 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:
 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² 
 LOSS,Nirgal,Passeur 806,553624,442900,,,,,110724,20.00,Nirgal a perdu 110 724 cm² (20.00%) 
 FLOOD,filoux,Passeur 003,55813865,74225093,yosemite,Passeur 004,51142304,32731076,18411228,36.00,filoux a pris 18 411 228 cm² à yosemite (36.00%)
 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² 
 LOSS,Nirgal,Passeur 806,553624,442900,,,,,110724,20.00,Nirgal a perdu 110 724 cm² (20.00%) 
 FLOOD,filoux,Passeur 003,55813865,74225093,yosemite,Passeur 004,51142304,32731076,18411228,36.00,filoux a pris 18 411 228 cm² à yosemite (36.00%)
-"""
+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,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:
+#BASE_DIRECTORY
 #SERVER
 #LOGIN
 #PASSWORD
 #BASE_URL
 #SERVER
 #LOGIN
 #PASSWORD
 #BASE_URL
-HOME = os.environ['HOME']
-ARCHIVE = HOME + '/.fourmizzz/archive.py'
-RESULT = HOME + '/.fourmizzz/results.log'
+DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
 
 
-def read_config():
+def read_config(config_dir):
     '''
     That function will read config.py in .fourmizzz directory
     and set up globals SERVER, LOGIN, PASSWORD, and BASE_URL.
     '''
     That function will read config.py in .fourmizzz directory
     and set up globals SERVER, LOGIN, PASSWORD, and BASE_URL.
@@ -48,13 +65,13 @@ LOGIN = 'MonIdentifiant'
 PASSWORD = 'MonMotDePasse'
     '''
 
 PASSWORD = 'MonMotDePasse'
     '''
 
-    sys.path.append(HOME+'/.fourmizzz')
+    sys.path.append(config_dir)
     try:
         import config
     except ImportError:
     try:
         import config
     except ImportError:
-        CONFIG = HOME+'/.fourmizzz/config.py'
-        logging.fatal("No configuration file. Creating %s", CONFIG)
-        f = open(CONFIG, mode='w+', encoding='utf-8')
+        config_filename = os.path.join(config_dir, 'config.py')
+        logging.fatal("No configuration file. Creating %s", config_filename)
+        f = open(config_filename, mode='w+', encoding='utf-8')
         f.write(CONFIG_TEMPLATE)
         f.close()
         logging.fatal("Please update this file with your details.")
         f.write(CONFIG_TEMPLATE)
         f.close()
         logging.fatal("Please update this file with your details.")
@@ -97,14 +114,14 @@ def number_format(i):
             result = ' ' + result
         d3 = i % 1000
         i = i//1000
             result = ' ' + result
         d3 = i % 1000
         i = i//1000
-        if i:
+        if i: # there will be something else left of that
             result = ('%03d' % d3) + result
         else:
             result = ('%d' % d3) + result
     return result
 
 
             result = ('%03d' % d3) + result
         else:
             result = ('%d' % d3) + result
     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)
     #httpresponse = httpopen(BASE_URL + '/')
     #html = httpresponse.read().decode('utf-8')
     #print(html)
@@ -145,42 +162,78 @@ def tdc_get(alliance_tag=None):
         #print('-'*80)
         #print_idented_tree(table)
 
         #print('-'*80)
         #print_idented_tree(table)
 
-    summary = {}
+    members = {} # NEW
     for tr in get_elem(table, 'tr'):
         row = get_elem(tr, 'td')
     for tr in get_elem(table, 'tr'):
         row = get_elem(tr, 'td')
-        if not alliance_tag and len(row) != 10:
-            continue
-        if alliance_tag and len(row) != 6:
-            continue
         #print('-'*10)
         #print_idented_tree(tr)
         if not alliance_tag:
         #print('-'*10)
         #print_idented_tree(tr)
         if not alliance_tag:
+            if len(row) != 10:
+                continue
+            grade = get_merged_leaf_content(row[2])
             nick = get_merged_leaf_content(row[3])
             nick = get_merged_leaf_content(row[3])
-            tdc = get_merged_leaf_content(row[4]).replace(' ', '')
+            field = int(get_merged_leaf_content(row[4]).replace(' ', ''))
+            techno = int(get_merged_leaf_content(row[5]))
+            anthill = int(get_merged_leaf_content(row[6]))
+            active = get_elem(row[7], 'img')[0].attributes['alt']
+            colonized = len(get_elem(row[8], 'img')) == 1
+            #print(nick, grade, field, techno, anthill, active, colonized)
+
         else:
         else:
+            if len(row) != 6:
+                continue
+            grade = get_merged_leaf_content(row[1])
             nick = get_merged_leaf_content(row[2])
             nick = get_merged_leaf_content(row[2])
-            tdc = get_merged_leaf_content(row[3]).replace(' ', '')
+            field = int(get_merged_leaf_content(row[3]).replace(' ', ''))
+            techno = int(get_merged_leaf_content(row[4]))
+            anthill = int(get_merged_leaf_content(row[5]))
+            active = None
+            colonized = None
+            #print(nick, grade, field, techno, anthill)
 
 
-        tdc = int(tdc)
-        #print(nick, tdc)
-        summary[nick] = tdc
-    logging.debug('%s members - total tdc = %s cm²', len(summary), number_format(sum(summary.values())))
-    return summary
+        members[nick] = {
+            'grade': grade,
+            'field': field,
+            'techno': techno,
+            'anthill': anthill,
+            'active': active,
+            'colonized': colonized,
+        }
+        #print(members[nick])
 
 
+    logging.debug('%s members - total field = %s cm²', len(members), number_format(sum([x['field'] for x in members.values()])))
+    return members
+
+
+def members_to_field(members):
+    """
+    Takes a full information dict "members"
+    Returns a simple dict with nick->field
+    """
+    fields  = {}
+    for nick, data in members.items():
+        fields[nick] = data['field']
+    return fields
+
+
+def field_compare(oldmembers, newmembers, csvfile):
+    
+    oldfield = members_to_field(oldmembers)
+    newfield = members_to_field(newmembers)
 
 
-def tdc_compare(oldtdc, newtdc):
     changes = {}
     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 }
     
         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():
             tnew = 0
             changes[nick] = { 'old': told, 'new': 0, 'delta': -told }
     #for nick, change in changes.items():
@@ -188,8 +241,7 @@ def tdc_compare(oldtdc, newtdc):
     #print
 
     if not len(changes):
     #print
 
     if not len(changes):
-        logging.info('No changes')
-        return
+        txtchanges2.append('NOOP,,,,,,,,,,,No changes')
  
     for nick in changes.keys():
         delta = changes[nick]['delta']
  
     for nick in changes.keys():
         delta = changes[nick]['delta']
@@ -200,29 +252,157 @@ def tdc_compare(oldtdc, newtdc):
                 continue # already done
             if changes[nick2]['delta'] != -delta:
                 continue # not the good one
                 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
 
             changes[nick]['delta'] = 0
             changes[nick2]['delta'] = 0
 
-                
     for nick, change in changes.items():
         delta = change['delta']
         if 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:
             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:
         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:
+                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:
             else:
-                txtchanges.append("%s a rejoint l'alliance avec %s cm²" % (nick, number_format(delta)))
+                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')
+
 
 
-    for txtchange in txtchanges:
-        logging.info(txtchange)
+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
 
 if __name__ == '__main__':
     from optparse import OptionParser
@@ -234,8 +414,27 @@ if __name__ == '__main__':
         action='store_true', dest='quiet', default=False,
         help="quiet mode")
     parser.add_option('-a', '--alliance',
         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.")
     parser.add_option('--dry-run',
         action='store_true', dest='dryrun', default=False,
         help="don't store result in archives.")
@@ -247,28 +446,74 @@ if __name__ == '__main__':
         loglevel = logging.WARNING
     else:
         loglevel = logging.INFO
         loglevel = logging.WARNING
     else:
         loglevel = logging.INFO
-    logging.basicConfig(filename=RESULT, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
+    
+    HOME = os.environ['HOME']
+    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)
 
 
-    read_config()
+    if not options.quick:
+        delay = random.randint(0,59)
+        sleep(delay)
 
 
-    oldtdc = None
+    read_config(BASE_DIRECTORY)
+
+    members_filename = os.path.join(BASE_DIRECTORY, options.archive_filename)
+    oldmembers = None
     try:
     try:
-        f = open(ARCHIVE, mode='r+', encoding='utf-8')
+        f = open(members_filename, mode='r+', encoding='utf-8')
     except IOError as err:
         if err.errno == 2: # No such file or directory
             logging.warning("No archive file, creating one.")
     except IOError as err:
         if err.errno == 2: # No such file or directory
             logging.warning("No archive file, creating one.")
-            f = open(ARCHIVE, mode='w+', encoding='utf-8')
+            f = open(members_filename, mode='w+', encoding='utf-8')
         else:
             raise
     else:
         else:
             raise
     else:
-        oldtdc = eval(f.read())
+        oldmembers = eval(f.read())
     
     
-    newtdc = tdc_get(options.alliance)
-    if oldtdc is not None:
-        tdc_compare(oldtdc, newtdc)
+    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:
+        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)
     
     # Save archive only after processing, just in case it crashes
     if not options.dryrun:
         f.seek(0)
-        f.write(repr(newtdc))
+        f.write(repr(newmembers))
         f.truncate()
         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)