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 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 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"])
def save_player_into_db(self, db_file): for player in self.current_tournament.get_player_list: try: self.current_tournament.save_player_into_db(db_file, player) except Warning as err: view.show_message(err)