########################################################################
##############Probleme: gestion d'une champiannat de  football############ #########################################################################

def init_match():
    try:
        f=open('equipes.txt','r')
        teams=f.readlines()
        f.close()
    except:
        print('fichier inexistant')
    
    match0='{};{};0:0\n'
    list_match=[]
    for team  in teams:
        for vs in teams:
             if team!=vs:
                 list_match.append(match0.format(team.strip(),vs))
    
    f=open('match.txt','w')
    f.writelines(list_match)
    f.close()

def maj_match(loc,vs,result):
    
    f=open('match.txt','r')
    content=f.read()
    f.close()
    
    motif='{};{};{}'
    
    toReplace=motif.format(loc,vs,"0:0")
    replacement=motif.format(loc,vs,result)
    
    content.replace(toReplace,replacement)
    
    f=open('match.txt','w')
    content=f.write(content)
    f.close()

#Task: do the same funtion but using readlines instead of read.

def res_match(line):
    line_details=line.strip().split(';')
    res=line_details[-1].strip().split(':')
    if res[0]>res[1]:return '1'
    elif res[0]<res[1]:return '2'
    else:return 'X'

def but_marque(team):
    f=open('match.txt','r')
    list_match=f.readlines()
    f.close()
    
    for match in list_match:
        nb_buts=0
        match_details=match.strip().split(';')
        match_res=match_details[-1].strip().split(':')
        if match_details[0]==team:
            nb_buts+=int(match_res[0])
        elif match_details[1]==team:
            nb_buts+=int(match_res[1])
        return nb_buts
#question 5: same as q4
def nb_point(team):
    f=open('match.txt','r')
    list_match=f.readlines()
    f.close()
    
    nb_pt=0
    for match in list_match:
        match_detail=match.strip().split(';')
        if match_detail[0]==team:
            if res_match(match)=='1':
                nb_pt+=3
            elif res_match(match)=='X':
                nb_pt+=1
        if match_detail[1]==team:
            if res_match(match)=='2':
                nb_pt+=3
            elif res_match(match)=='X':
                nb_pt+=1
    return nb_pt

def tartib():
    f=open('equipes.txt','r')
    teams=f.readlines()
    f.close()
    
    d={}
    for team in teams:
        d[team.strip()]=(nb_point(team),but_marque(team),but_recu(team))             
    
    return d
            
    
             
             
    
    
    
    
    
    
                 
    
    
    
    
    
    
    
