def create_round(self, id_round: str): """ Create a new round for a tournament. Arg: * *id_round* (str): ID of the round """ id_tournament = id_round.split(":")[0] tournament = Tournament.get(id_tournament) if tournament.tournament_open and len(tournament.rounds) < tournament.number_rounds: if not Round.get(id_round): times = self.get_times(id_tournament) name_round = f"Round {id_round.split(':')[1]}" begin_time = times[0] end_time = times[1] Round(id_round, name_round, begin_time, end_time) if tournament.players: pairs = self.create_pairs(id_tournament) matches = [] for index, (player_a, player_b) in enumerate(pairs): id_match = f"{id_round}:{index + 1}" match = Match(id_match, player_a, player_b) matches.append(match) Round.get(id_round).set_matches(matches) tournament.add_round(Round.get(id_round)) if len(tournament.rounds) >= tournament.number_rounds: tournament.tournament_open = False else: raise Exception(f"Round {id_round} already exists.") else: print("The tournament has already reach his maximum rounds number.")
def set_round(): """ Set the winners of a round. """ id_tournament = input("Give ID of the tournament : ") Report.show_all_rounds(id_tournament) id_round = Tournament.get(id_tournament).rounds[-1].id_round round_done = False for match in Round.get(id_round).matches: if (match.player_a_score and match.player_b_score) != match.match_in_progress: round_done = True break if not round_done: RoundManager().set_results(id_round) RoundManager().round_done(id_round) if len(Tournament.get(id_tournament).rounds) == Tournament.get( id_tournament).number_rounds: Tournament.get(id_tournament).set_tournament_finished() elif round_done and Tournament.get(id_tournament).tournament_open: print( f"The round {id_round} is already set, you have to create another round." ) else: print( f"The tournament {id_tournament} is already set and clear. You have to create another tournament." ) Tournament.load_from_db()
def new_round(): """ Create a new round for a tournament. """ id_tournament = input("Give ID of the tournament : ") last_round = Tournament.get(id_tournament).rounds[-1].id_round last_round_done = True if Tournament.get(id_tournament).tournament_open: for match in Round.get(last_round).matches: if (match.player_a_score or match.player_b_score) == match.match_in_progress: last_round_done = False break if last_round_done: next_round = int(last_round.split(":")[-1]) + 1 next_round = f"{id_tournament}:{next_round}" RoundManager().create_round(next_round) else: print( f"The round {last_round} is not finished yet, you can't create the next round." ) else: print( f"The tournament {id_tournament} is already finished. You cannot create another round." ) Tournament.load_from_db()
def set_results(id_round: str): """ Set the results of the round Arg: * *id_round* (str): ID of the round to set """ single_round = Round.get(id_round) for match in single_round.matches: id_match = match.id_match dict_result = {"0": -1, "1": match.player_a.id_player, "2": match.player_b.id_player} winner = input(f"Match {id_match} | Winner " f"is {match.player_a.first_name} " f"or {match.player_b.first_name}?" f" (Type 1 for {match.player_a.first_name}," f" 2 for {match.player_b.first_name}" f" and 0 if equality) : ") if match.player_a_score == match.match_in_progress and match.player_b_score == match.match_in_progress: MatchManager().set_winner(id_match, dict_result[winner]) if match.player_a.id_player == dict_result[winner]: match.set_player_a_score(1.) match.set_player_b_score(0.) elif match.player_b.id_player == dict_result[winner]: match.set_player_a_score(0.) match.set_player_b_score(1.) else: match.set_player_a_score(0.5) match.set_player_b_score(0.5) else: print("You cannot edit previous matches.")
def show_round(id_round: str): """ Show a specific round from a tournament. Arg: * *id_round* (str): ID of the round """ single_round = Round.get(id_round) print(single_round)
def show_all_rounds(id_tournament: str): """ Show all rounds from a tournament. Arg: * *id_tournament* (str): ID of the tournament """ all_rounds = Round.get_all_from_tournament(id_tournament) for single_round in all_rounds: print(single_round)
def round_done(id_round: str): """ Check if the round is already set Arg: * *id_round* (str): ID of the round. """ single_round = Round.get(id_round) for match in single_round.matches: if not MatchManager.match_done(match.id_match): return False return True