#programma in python per la raccolta dei pgn e degli abbinamenti
#delle scacchiere da mandare online con le scacchiere digitali
#Il programma richiede l'installazione di python 3

round = input("Inserisci il turno corrente: ")

#ordine di lettura dei file pgn nelle varie cartelle
nometorneo = [ \
    ".\\c16\\U16\\wwwU16\\U16" + str(round)+ ".pgn", \
    ".\\c16\\U16F\\wwwU16F\\U16F" + str(round)+ ".pgn", \
    ".\\c16\\U14\\wwwU14\\U14" + str(round)+ ".pgn", \
    ".\\c16\\U14F\\wwwU14F\\U14F" + str(round)+ ".pgn", \
    ".\\c16\\U10\\wwwU10\\U10" + str(round)+ ".pgn", \
    ".\\c16\\U10F\\wwwU10F\\U10F" + str(round)+ ".pgn", \
    ".\\c16\\U8\\wwwU8\\U8" + str(round)+ ".pgn", \
    ".\\c16\\U8F\\wwwU8F\\U8F" + str(round)+ ".pgn", \
    ".\\c16\\U12F\\wwwU12F\\U12F" + str(round)+ ".pgn", \
    ".\\c16\\U12\\wwwU12\\U12" + str(round)+ ".pgn"
    ]


#ordine di lettura degli abbinamenti nelle varie cartelle
#l'ordine delle cartelle deve essere identico a quello precedente
nometorneo2 = [ \
    ".\\c16\\U16\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U16F\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U14\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U14F\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U10\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U10F\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U8\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U8F\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U12F\\pairs" + str(round)+ ".txt", \
    ".\\c16\\U12\\pairs" + str(round)+ ".txt"
    ]

	
#numero di scacchiere da copiare per ciascun torneo
num_games = [4, 2, 4, 1, 1, 1, 1, 1, 1, 2]

#file pgn
#lettura di ciascun file pgn
fileout = open(".\\games"+str(round)+".pgn", "w")
for k in range(1,11):
    with open(nometorneo[k-1]) as f:
        print(nometorneo[k-1])
        pgn = f.read()
    f.closed
    games = pgn.split("\n\n")
    stop = num_games[k-1]
    for i in range(0, stop):
        g = games[i]
        fileout.write( g + "\n\n" )     
fileout.close()

#pulitura file
with open(".\\games"+str(round)+".pgn") as f:
    pgn = f.read()
f.closed

pgn = pgn.replace( ".1", "" )
pgn = pgn.replace( ".2", "" )
pgn = pgn.replace( ".3", "" )
pgn = pgn.replace( ".4", "" )

pgn = pgn.replace( "U16F", "U16" )
pgn = pgn.replace( "U14F", "U16" )
pgn = pgn.replace( "U14", "U16" )
pgn = pgn.replace( "U12F", "U16" )
pgn = pgn.replace( "U12", "U16" )
pgn = pgn.replace( "U10F", "U16" )
pgn = pgn.replace( "U10", "U16" )
pgn = pgn.replace( "U8F", "U16" )
pgn = pgn.replace( "U8", "U16" )

fileout = open(".\\games"+str(round)+".pgn", "w")
fileout.write( pgn + "\n\n" )      
fileout.close()

#abbinamenti sala digitale	
fileout = open(".\\pairs"+str(round)+".txt", "w")
#lettura di ciascun file pairs
for k in range(1,11):
    with open(nometorneo2[k-1]) as f:
        print(nometorneo2[k-1])
        pgn = f.read()
    f.closed
    fileout.write( nometorneo2[k-1] + "\n\n" ) 
    pairs = pgn.split("\n")
    stop = num_games[k-1]
    for a in pairs:
        for i in range(0, stop):
            #print(a)
            s = " " + str(i+1) + " ="
            if a.find(s,0,10)>0 :
                fileout.write( a + "\n" )
    fileout.write( "\n\n" ) 
fileout.close()
