def show_players_ranked(id_tournament: str): """ Show players sorted by rank in a tournament. Arg: * *id_tournament* (str): ID of the tournament """ players = Tournament.get(id_tournament).players sort_list = [] for player in players: sort_list.append({ "id_player": player.id_player, "points": player.points[id_tournament] if id_tournament in player.points.keys() else 0, "elo": int(player.elo) }) sort_list = sorted(sort_list, key=itemgetter("elo"), reverse=True) sort_list = sorted(sort_list, key=itemgetter("points"), reverse=True) sorted_players = [] for stats in sort_list: id_player = stats["id_player"] sorted_players.append(Player.get(id_player)) for player in sorted_players: print(player, end="") score = player.points[ id_tournament] if id_tournament in player.points.keys() else 0 print(f"\t\t\t\t Score : {score}")
def show_players_alpha(id_tournament: str): """ Show players sorted by alpha in a tournament. Arg: * *id_tournament* (str): ID of the tournament """ players = Tournament.get(id_tournament).players sort_list = [] for player in players: sort_list.append({ "id_player": player.id_player, "last_name": player.last_name, "first_name": player.first_name }) sort_list = sorted(sort_list, key=itemgetter("first_name"), reverse=False) sort_list = sorted(sort_list, key=itemgetter("last_name"), reverse=False) sorted_players = [] for stats in sort_list: id_player = stats["id_player"] sorted_players.append(Player.get(id_player)) for player in sorted_players: print(player, end="") score = player.points[ id_tournament] if id_tournament in player.points.keys() else 0 print(f"\t\t\t\t Score : {score}")
def edit_elo(id_player: str, elo: int): """ Edit the elo of a player. Args: * *id_player* (str): ID of the player * *elo* (int): New elo attributes to the player """ player = Player.get(id_player) player.set_elo(int(elo))
def give_points(id_player: str, id_tournament: str, points: float): """ Give points to a player in a specific tournament. Args: * *id_player* (str): ID of the player * *points* (float): Points given to the player """ player = Player.get(id_player) if player: player.set_points(id_tournament, points)
def create_player(id_player: str): """ Create a new player * *id_player* (str): ID of the player created """ id_player = str(id_player) last_name = input("Last name of the player : ") first_name = input("First name of the player : ") birthday = input("Birthday of the player : ") sex = input("Sex of the player : ") elo = int(input("Elo of the player: ")) if not Player.get(id_player): Player(id_player, last_name, first_name, birthday, sex, elo) else: raise Exception( f"The ID {id_player} already exists : {Player.get(id_player)}")
def add_players(id_tournament: str): """ Add players (new or not) to the tournament. Arg: * *id_tournament* (str): ID of the tournament """ tournament = Tournament.get(id_tournament) if tournament and not tournament.rounds: tournament = Tournament.get(id_tournament) list_players = [] for i in range(tournament.number_players): id_player = input( f"Tournament : {tournament.name_tournament}," f" player {i + 1}/{tournament.number_players}\n" f"Give the ID of the player that'll play. " f"If it's a new player, please give an unique" f"ID to register him : ") if not list_players: if Player.get(id_player): list_players.append(Player.get(id_player)) else: PlayerManager.create_player(id_player) list_players.append(Player.get(id_player)) else: for player in list_players: if player.id_player == id_player: print( "This player is already playing this tournament! Please retry : " ) id_player = input( f"Tournament : {tournament.name_tournament}," f" player {i + 1}/{tournament.number_players}\n" f"Give the ID of the player that'll play. " f"If it's a new player, please give an unique" f"ID to register him : ") if Player.get(id_player): list_players.append(Player.get(id_player)) else: PlayerManager().create_player(id_player) list_players.append(Player.get(id_player)) tournament.set_players(list_players) for player in list_players: player.tournaments_played.append(id_tournament) else: raise Exception(f"Tournament ID : {id_tournament} does not exist.")
def sort_players_by_score(id_tournament: str): """ Sort the players by score, including also the elo. score > elo Arg: * *id_tournament* (str): ID of the tournament """ players = Tournament.get(id_tournament).players list_stats = [] for player in players: list_stats.append({"id_player": player.id_player, "elo": int(player.elo), "score": player.points[id_tournament] if id_tournament in player.points.keys() else 0 }) list_stats = sorted(list_stats, key=itemgetter("elo"), reverse=True) list_stats = sorted(list_stats, key=itemgetter("score"), reverse=True) list_players = [] for item in list_stats: list_players.append(Player.get(item["id_player"])) return list_players