def load_player_menu(self): view.show_load_player_menu() while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.get_file_list() break elif resp == "2": db_file = view.ask_user_input("Nom du fichier : ") self.get_player_list_from_db(db_file) break elif resp == "3": db_file = view.ask_user_input("Nom du fichier : ") player_id = view.ask_user_input( "Entrer l'identifiant du joueur à ajouter (1000_ABCDEF) : " ) self.add_player_from_db(db_file, player_id) break elif resp == "q": break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"]) if resp in ("1", "2", "3", "4"): self.load_player_menu()
def rounds_menu(self): """Ask the user to choose between the various options available in the rounds menu, as displayed by view. """ view.show_rounds_menu() while True: choice = view.ask_user_input("Choix : ") print() if choice == "1": try: index = int( view.ask_user_input( "Numéro de la ronde à afficher (1, 2, 3 ou 4) : ")) except ValueError: view.show_warning(self.error_messages["INVALIDE_FORMAT"]) break try: self.describe_rounds(index - 1) break except IndexError: view.show_warning(self.error_messages["MISSING_INDEX"]) break elif choice == "2": self.describe_rounds() break elif choice == "3": self.play_round() break elif choice == "q": break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"]) if choice in ("1", "2", "3", "4"): self.rounds_menu()
def create_tournament(self): """Ask the user for the informations needed to make an instance of a class Tournament. """ view.show_create_tournament() name = view.ask_user_input("Nom du tournoi : ") localization = view.ask_user_input("Lieu du tournoi : ") while True: time_control = view.ask_user_input( "Type de contrôle du temps (Bullet, Blitz ou Coup rapide) : ") if time_control.lower() in ("bullet", "blitz", "coup rapide"): break else: view.show_warning(self.error_messages["INVALIDE_FORMAT"]) description = view.ask_user_input( "Description du tournoi (optionnel) : ") while True: beg_date = view.ask_user_input( "Date de début du tournoi (JJ/MM/AAAA HH:MM): ") try: dt.strptime(beg_date, "%d/%m/%Y %H:%M") break except ValueError: view.show_warning(self.error_messages["INVALID_DATE"]) chess_tournament = trn.Tournament(name, localization, time_control, description, beg_date) self.tournament_list.append(chess_tournament) self.current_tournament = chess_tournament
def add_player_to_tournament(self): """Ask the user for the required arguments needed to make a new Player instance and add it to the current tournament. """ if len(self.current_tournament.get_player_list ) < self.current_tournament.MAX_PLAYER_LIMIT: view.show_message( "Adding a new player. Please enter the following informations." ) l_name = view.ask_user_input("Last name: ") f_name = view.ask_user_input("First name: ") while True: date_birth = view.ask_user_input( "Date of birth (JJ/MM/AAAA): ") # check date validity. Numbers validity and calendar validity try: dt.strptime(date_birth, "%d/%m/%Y") break except ValueError: view.show_warning(self.error_messages["INVALID_DATE"]) while True: gender = view.ask_user_input("Gender (M/F): ") if gender.upper() in ("M", "F"): break else: view.show_warning(self.error_messages["INVALIDE_FORMAT"]) while True: try: rank = int( view.ask_user_input( "Rank (must be a positive integer): ")) if isinstance(rank, int) and rank > 0: break else: view.show_warning(self.error_messages["INVALID_INT"]) except ValueError: view.show_warning(self.error_messages["INVALID_INT"]) self.current_tournament.add_new_player(l_name, f_name, date_birth, gender, rank) view.show_done_action("Création terminée", len(self.current_tournament.get_player_list), self.current_tournament.MAX_PLAYER_LIMIT) else: view.show_warning(self.error_messages["MAX_PLAYER"])
def save_player_menu(self): view.show_save_player_menu() while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.get_file_list() break elif resp == "2": db_file = view.ask_user_input("Nom du fichier : ") self.save_player_into_db(db_file) break elif resp == "q": break if resp in ("1", "2"): self.save_player_menu()
def describe_players_menu(self): """Send the information needed by view to display the list of players can be displayed by : - Alphabetical order ; - Rank order ; - A unique player if index argument is given. """ view.show_players_report() while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.describe_players(by_name=True) break elif resp == "2": self.describe_players(by_rank=True) break elif resp == "3": index = int( view.ask_user_input( "Joueur à afficher (choisir entre 1 et 8) : ")) self.describe_players(index=index) break elif resp == "4": while True: index = int(view.ask_user_input("Numéro du joueur : ")) new_rank = int(view.ask_user_input("Nouveau rang : ")) if new_rank > 0: try: self.current_tournament.get_player_list[ index - 1].set_player_rank = new_rank break except IndexError: view.show_warning( self.error_messages["MISSING_INDEX"]) except ValueError: view.show_warning( self.error_messages["INVALIDE_FORMAT"]) else: view.show_warning(self.error_messages["INVALID_INT"]) break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"])
def play_round(self): """Play the last round added to the tournament by calling the play_round method of the current tournament instance. """ try: nb_rd = len(self.current_tournament.get_round_list) - 1 rd = self.current_tournament.get_round_list[nb_rd] if not rd.end_date: view.show_play_menu(rd.name) self.describe_rounds(nb_rd) for nb, game in enumerate(rd.get_match_list): while True: result = view.ask_user_input( f"Résultat match {nb+1} (J1, J2 ou nul) : ") if result.upper() in ("J1", "J2", "NUL"): self.current_tournament.play_round( rd, game, result) break else: view.show_warning( self.error_messages["UNKNOWN_COMMAND"]) while True: date = view.ask_user_input( "Date de fin de la ronde (JJ/MM/AAAA HH:MM) : ") try: dt.strptime(date, "%d/%m/%Y %H:%M") rd.end_date = date break except ValueError: view.show_warning( self.error_messages["INVALIDE_FORMAT"]) else: view.show_warning(self.error_messages["DONE_ROUND"]) except IndexError: view.show_warning(self.error_messages["MISSING_ROUND"]) except Warning: view.show_warning(self.error_messages["ONGOING_ROUND"])
def list_tournaments_in_db(self): """Send the tournaments data of a database file to be displayed by view. """ view.show_message("Fichiers disponibles :\n") self.get_file_list() file_name = view.ask_user_input("Nom du fichier : ") if file_name in self.get_file_list(): loader = trdb.TournamentDB(file_name) all_tournaments = loader.list_tournaments_in_db() for one_tournament in all_tournaments: view.show_key_val_data( one_tournament["tournament_data"]["tournament_info"]) else: view.show_warning(self.error_messages["MISSING_FILE"])
def set_tournament_end_date(self): while True: date = view.ask_user_input( "Choisir une date de fin (JJ/MM/AAAA) : ") try: self.current_tournament.end_tournament(date) view.show_message(f"Date de fin du tournoi : {date}") break except Warning: view.show_warning(self.error_messages["INVALID_DATE"]) except Exception: view.show_warning(self.error_messages["ONGOING_TOURNAMENT"]) break self.tournament_menu()
def save_tournament_in_db(self): file_name = view.ask_user_input("Nom du fichier de sauvegarde : ") update = False loader = trdb.TournamentDB(file_name) all_tournaments = loader.list_tournaments_in_db() for tournament in all_tournaments: if self.current_tournament.name in tournament["tournament_data"][ "tournament_info"].values(): update = True else: pass if update: self.current_tournament.save_tournament_in_db(file_name, update) else: self.current_tournament.save_tournament_in_db(file_name)
def tournament_menu(self): """Ask the user to chooses between the various options availables in the tournament_menu, as displayed by view. """ view.show_tournament_menu(self.current_tournament.name, self.current_tournament.localization, self.current_tournament.beg_date) while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.add_player_to_tournament() break elif resp == "2": self.add_round() break elif resp == "3": self.rounds_menu() break elif resp == "4": self.set_tournament_end_date() break elif resp == "5": self.describe_players_menu() break elif resp == "6": self.save_player_menu() break elif resp == "7": self.load_player_menu() break elif resp == "8": self.save_tournament_in_db() break elif resp == "q": break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"]) if resp in ("1", "2", "3", "4", "5", "6", "7", "8"): self.tournament_menu() elif resp == "q": self.start_menu()
def load_tournament_menu(self): view.show_load_tournament_menu() while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.get_file_list() break elif resp == "2": self.list_tournaments_in_db() break elif resp == "3": self.load_tournament_from_db() break elif resp == "q": break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"]) if resp in ("1", "2", "3"): self.load_tournament_menu() elif resp == "q": self.start_menu()
def start_menu(self): """Ask the user to choose between the various start menu options displayed by view. """ view.show_start_menu(self.today, self.current_time) while True: resp = view.ask_user_input("Choix : ") if resp == "1": self.create_tournament() break elif resp == "2": if self.tournament_list: self.tournament_menu() else: view.show_warning(self.error_messages["NO_TOURNAMENT"]) break elif resp == "3": self.load_tournament_menu() break elif resp == "4": if self.tournament_list: view.show_listed_data(self.tournament_list) break else: view.show_warning(self.error_messages["NO_TOURNAMENT"]) break elif resp == "5": index = int( view.ask_user_input("N° du tournoi à selectionner : ")) if self.tournament_list: try: self.current_tournament = self.tournament_list[index - 1] view.show_message( f"Tournoi selectionné : {self.tournament_list[index - 1]}" ) break except IndexError: view.show_warning(self.error_messages["MISSING_INDEX"]) break except ValueError: view.show_warning( self.error_messages["INVALIDE_FORMAT"]) break else: view.show_warning(self.error_messages["NO_TOURNAMENT"]) break elif resp == "q": break else: view.show_warning(self.error_messages["UNKNOWN_COMMAND"]) if resp == "1": self.tournament_menu() elif resp in ("2", "3", "4", "5"): self.start_menu() elif resp == "q": confirmation = view.ask_user_input( "êtes-vous sûr de vouloir quitter l'application ? (O/N)") if confirmation.lower() == "n": self.start_menu() elif confirmation.lower() == "o": quit()
def load_tournament_from_db(self): """Load a saved tournament avalable in the database. """ view.show_message("Fichiers existants : ") file_list = self.get_file_list() file_name = view.ask_user_input("Nom du fichier de chargement : ") if file_name in file_list: loader = trdb.TournamentDB(file_name) name = view.ask_user_input("Nom du tournoi à charger : ") try: tournament_data = loader.load_tournament_from_db(name) tournament_info = tournament_data[0] players_info = tournament_data[1] rounds_info = tournament_data[2] loaded_tournament = trn.Tournament( tournament_info["name"], tournament_info["localization"], tournament_info["time_control"], tournament_info["description"], tournament_info["beg_date"], tournament_info["end_date"]) if players_info: for e, players in enumerate(players_info): player = players[f"player{e}"] loaded_tournament.add_new_player( player["last_name"], player["first_name"], player["date_birth"], player["gender"], player["rank"]) # Saved rounds data only contains scores. The rounds are build # through the application internal logic. Scores are added only # if the saved rounds data has an end_date if rounds_info: for e, rnd in enumerate(rounds_info): if rnd[f"Round {e+1}"]["end_date"] is None: pass else: loaded_tournament.add_round_to_list(f"Round {e+1}") currrent_round = loaded_tournament.get_round_list[ e] for nb, game in enumerate( currrent_round.get_match_list): score1 = rnd[f"Round {e+1}"][f"game {nb + 1}"][ 0] score2 = rnd[f"Round {e+1}"][f"game {nb + 1}"][ 1] P1 = game[0][0] P2 = game[0][1] game[1][0] = score1 game[1][1] = score2 P1.set_player_score = score1 P2.set_player_score = score2 currrent_round.end_date = rnd[f"Round {e+1}"][ "end_date"] self.tournament_list.append(loaded_tournament) self.current_tournament = loaded_tournament except Warning: view.show_warning(self.error_messages["MISSING_TOURNAMENT"]) self.start_menu() else: view.show_warning(self.error_messages["MISSING_FILE"])