diff --git a/controllers/base.py b/controllers/base.py index 01db806..99069ee 100644 --- a/controllers/base.py +++ b/controllers/base.py @@ -1,5 +1,7 @@ from ChessTournament.models.models import Player, Tournament, Round, Match from ChessTournament.views.menu import Menu +from ChessTournament.views.base import View + from random import shuffle @@ -8,11 +10,11 @@ class Controller: # loading models self.players_list = [] self.score_list = [] - self.tournament = Tournament() - self.round = Round() self.match = Match() + self.tournament = Tournament() # loading views + self.view = View() self.menu = Menu() # for test @@ -26,7 +28,7 @@ class Controller: self.liste = [joueur1, joueur2, joueur3, joueur4, joueur5, joueur6] def sort_by_score(self): - return sorted(self.tournament.players_list, key=lambda t: t.score) + self.tournament.players_list.sort(key=lambda t: t.score, reverse = True) def shuffle_players(self): return shuffle(self.tournament.players_list) @@ -40,6 +42,7 @@ class Controller: self.tournament.description = input("Description ? : ") #self.tournament.players_list = input("Liste des joueurs : ") self.tournament.players_list = self.liste + total_round = input("Nombre de tours ? (4 par défaut) : ") or 4 if total_round != 4: self.tournament.total_round = int(total_round) @@ -48,32 +51,37 @@ class Controller: def run_tournament(self): input("Prêt à lancer le premier round ?\n") #print("tour", self.tournament.current_round, round1.start_time) + print("Liste des joueurs : ", self.tournament.players_list) + shuffle(self.tournament.players_list) for i in range(1, self.tournament.total_round): + self.round = Round() self.round.name = "Round " + str(i) self.tournament.current_round = self.round.name - - if i == 1: - # self.tournament.players_list = self.shuffle_players() - print(self.tournament.players_list) - else: - self.tournament.players_list = self.sort_by_score() - - #pour chaque tour : # set le temps de début # créer les matchs # afficher les matchs # attendre la saisie des scores # ajouter le tour à la liste des tours du tournoi - print(self.round.name) - self.round.start_time = self.round.turn_time() + + self.round.start_time = self.round.get_time() self.round.match_list = self.create_match() - #self.view.print_match_list() - print(self.round.match_list) + + print("Liste des joueurs, après tri : ", self.tournament.players_list) + print("Liste des matchs du Round : ", self.round.match_list) #self.view.prompt_for_scores() print("saisir les scores :") - input("Round suivant ?") + self.view.input_scores(self.round.match_list) + # append self.tournament.round_list with [round, match_list] + self.sort_by_score() + + print("scores du round :\n") + for i in self.tournament.players_list: + print(i.ine, " : ", i.score) + + input("Continuer ?") + def create_match(self): """Create match with two consecutive players. Check if match already happened in round @@ -82,50 +90,29 @@ class Controller: """ j = 0 k = 0 + print(self.tournament.players_list) for i in range(0, len(self.tournament.players_list), 2): j += 1 - self.match.name = "match" + str(j) - print(self.match.name) - self.match.player1 = self.tournament.players_list[i] - self.match.player2 = self.tournament.players_list[i+1] - print(self.match) - if self.match in self.round.match_list: - k += 1 - print(i, k) - print(self.tournament.players_list[i]) - print(i + k) - #print(self.tournament.players_list[i+k].ine) - #self.match.player2 = self.tournament.players_list[i+k].ine - self.round.match_list.append(self.match.get_data()) - else: - self.round.match_list.append(self.match.get_data()) + match_name = "match" + str(j) + match = Match() + match.player1 = self.tournament.players_list[i] + match.player2 = self.tournament.players_list[i+1] + self.round.match_list.append(match) return self.round.match_list - def record_new_player(self): - # get_player = {} print("Enregistrez un nouveau joueur :\n") - # get_player['lastname'] = input('Nom de famille :\n') - # get_player['name'] = input('Prénom :\n') - #get_player['birth_date'] = input('Date de naissance :\n') - self.lastname = input("Nom de famille ? : ") self.name = input("Prénom ? : ") self.birthdate = input("Date de naissance (jj/mm/aaaa) ? : ") #self.birthdate = self.check_date() self.ine = input("Identifiant National d'Echecs (ine) ? : ") #self.ine = self.test_ine() - - - # convert dict in json object and write it in players.json file (with "a" append to file) - # with open("players.json", "a") as output: - # output.write(json.dumps(get_player, indent=3)) return {"Nom": self.lastname, "Prénom": self.name, "Date de naissance": self.birthdate, "INE": self.ine} def run(self): - menu_choice = self.menu.items(1) if menu_choice == "4": print("Bye") @@ -137,8 +124,9 @@ class Controller: print("c'est parti") self.create_tournament() self.run_tournament() - - + print("Tournoi terminé.\n Les scores sont :\n") + for i in self.tournament.players_list: + print(i.ine, " : ", i.score) diff --git a/models/models.py b/models/models.py index d7cb450..833ced7 100644 --- a/models/models.py +++ b/models/models.py @@ -39,8 +39,8 @@ class Match: self.score2 = 0 def __str__(self): - return self.player1.name + " " + self.player1.lastname + " / " + self.player2.name + " " + self.player2.lastname - + #return self.player1.name + " " + self.player1.lastname + " / " + self.player2.name + " " + self.player2.lastname + return self.player1.ine + "/" + self.player2.ine def __repr__(self): return str(self) @@ -59,7 +59,7 @@ class Round: self.start_time = None self.end_time = None - def turn_time(self): + def get_time(self): return datetime.now().strftime("%d-%m-%Y, %H:%M:%S") def create_match2(self): diff --git a/views/base.py b/views/base.py index ae54370..b40e50b 100644 --- a/views/base.py +++ b/views/base.py @@ -32,3 +32,26 @@ class View: else: print("Mauvais format d'ine") + + def input_scores(self, match_list): + for match in match_list: + print(match) + print("Scores pour match", match_list.index(match) + 1, " :") + while True: + try: + result = input(f"1.{match.player1}, 2.{match.player2}, 3.Nul\n") + if result in ("1", "2", "3"): + if result == "1": + match.player1.score += 1 + elif result == "2": + match.player2.score += 1 + elif result == "3": + match.player1.score += 0.5 + match.player2.score += 0.5 + break + else: + print("Entrez un chiffre entre 1 et 3") + except ValueError: + print("Veuillez entrer un chiffre") + +