def summary(state): """ Provides a summary of selected player :param state: current state of variables :return: prints a summary of the player """ # Initialize variables stat_rankings = defaultdict(lambda: (defaultdict(float))) for player in state.normalized_player_statistics[state.iteration]: for statistic_name in state.normalized_player_statistics[state.iteration][player]: stat_rankings[statistic_name][player] = \ state.normalized_player_statistics[state.iteration][player][statistic_name] # Decide which player to view the stats of while state.player_to_obtain is None: desired_player = raw_input("Enter a player name: ").lower() # If the input is valid, remove player from draft and add them to a team if desired_player in state.cumulative_player_statistics[state.iteration]: state.update_player_to_obtain(desired_player) # Suggests player names if input is incorrect else: Helper.check_incorrect_input(desired_player, state.normalized_player_statistics[state.iteration].keys()) return Helper.calculate_player_summary(stat_rankings, state.player_to_obtain)
def swap(state): """ Shows the change in statistics if a player is swapped from your team :param state: current state of variables :return: prints statistic changes """ # Initialize variables for user in state.teams: state.teams[user] = Helper.lowercase(state.teams[user]) state.teams_storage = copy.deepcopy(state.teams) if state.player_to_obtain is None and state.player_to_trade is None: state.update_player_to_trade(raw_input("Enter the player you want to remove from your team: ").lower()) state.update_player_to_obtain(raw_input("Enter the player you want to add to your team: ").lower()) print "" # If the input is valid, analyze statistic changes if state.player_to_trade in state.cumulative_player_statistics[state.iteration] \ and state.player_to_trade in state.teams[state.current_user] \ and state.player_to_obtain in state.cumulative_player_statistics[state.iteration] \ and state.player_to_obtain not in state.teams[state.current_user]: state.teams = copy.deepcopy(state.teams_storage) return Helper.evaluate_player_swap(state.teams, state.player_to_trade, state.player_to_obtain, state.normalized_player_statistics[state.iteration], state) # Notifies user if player is not in their team elif state.player_to_trade not in state.teams[state.current_user]: print state.player_to_trade.title(), "is not on your team" elif state.player_to_obtain in state.teams["Chris S"]: print state.player_to_obtain.title(), "is already on your team" # Suggests player names if input is incorrect elif state.player_to_trade not in state.cumulative_player_statistics[state.iteration]: Helper.check_incorrect_input(state.player_to_trade, state.normalized_player_statistics[state.iteration].keys()) else: Helper.check_incorrect_input(state.player_to_obtain, state.normalized_player_statistics[state.iteration].keys())
def trade(state): """ Suggests players on your team to trade for a given player :param state: current state of variables :return: prints players to trade and their rankings """ # Initialize variables players_on_team = Helper.lowercase(state.teams[state.current_user]) for x in state.teams: state.teams[x] = Helper.lowercase(state.teams[x]) while state.player_to_obtain is None: desired_player = raw_input("Enter the player you are trying to move: ").lower() if desired_player in state.cumulative_player_statistics[state.iteration]: state.update_player_to_obtain(desired_player) else: Helper.check_incorrect_input(desired_player, state.normalized_player_statistics[state.iteration].keys()) return Helper.find_trade_possibilities(state.cumulative_player_statistics[state.iteration], state.normalized_player_statistics[state.iteration], state.player_to_obtain, players_on_team, state)
def draft(state): """ Suggests draft picks to user and creates a file containing all drafted state.teams :param state: current state of variables :return: .txt file containing players on every team """ # Establish database connection connection = sqlite3.connect("FantasyBasketball.db") cursor = connection.cursor() # Get user-inputted variables pick_num = int(raw_input("What is your first round pick?: ")) rounds_num = int(raw_input("How many rounds are in the draft?: ")) participants = int(raw_input("How many players are in the league?: ")) # Determine which pick numbers a user has pick_numbers = [(participants * (r - 1)) + (pick_num if r % 2 else (participants + 1 - pick_num)) for r in range(1, rounds_num + 1)] # Initialize variables taken = [] removed = [] all_users = [] my_team = {} end = False contribution = defaultdict(lambda: defaultdict(float)) ME = 'chris' # Use only top players to speed up integer linear program top_players = [] row_names = [stat_name[0] for stat_name in cursor.execute("""SELECT * FROM Contributions;""").description][1:] for row in cursor.execute("""SELECT * FROM Contributions ORDER BY contribution DESC LIMIT {};""".format( str(participants * rounds_num))).fetchall(): name = str(row[0]).lower() for i, stat in enumerate(row[1:]): contribution[name][row_names[i]] = stat top_players.append(name.lower()) # Store player names for input error checking all_players = [str(row[0]).lower() for row in cursor.execute("""SELECT name FROM Contributions;""")] # Determine where players are projected to be picked projected = defaultdict(int) with open('files/draft/projected_picks.txt') as f2: for i, line in enumerate(f2.readlines()): name = line.lower().rstrip() projected[name] = i + 1 for pick in range(1, (participants * rounds_num) + 1): # Breaks upon user request if end: break # Re-initialize success variable success = False print '\nPick {} in progress...'.format(pick) while not success: # Print draft recommendations on user's pick if pick in pick_numbers or pick == 1: print '' for draft_pick in Helper.optimize_draft(pick_numbers, projected, contribution, top_players, my_team, taken + removed + my_team.keys(), pick): if draft_pick[1] == '0': print '{}. {} - DRAFTED'.format(str(my_team[draft_pick[0]]), draft_pick[0].title()) else: print '{}. {}'.format(draft_pick[1], draft_pick[0].title()) # \ before a name indicates a new player player = raw_input("\nEnter a player name: ").lower() # Allows user to end function if player == "end": end = True success = True # Removes players from optimal draft calculation elif player == 'remove': removed.append(raw_input('Who would you like to remove?: ')) print '' # Allows user to swap picks elif player == 'swap': old_pick = int(raw_input('Which pick are you losing?: ')) new_pick = int(raw_input('Which pick are you getting?: ')) print '' if old_pick in pick_numbers: pick_numbers.pop(pick_numbers.index(old_pick)) pick_numbers.append(new_pick) # Calculates optimal draft upon request elif player == 'draft' and pick not in pick_numbers and pick != 1: print '' for draft_pick in Helper.optimize_draft(pick_numbers, projected, contribution, top_players, my_team, taken + removed + my_team.keys(), pick): if draft_pick[1] == '0': print '{}. {} - DRAFTED'.format(str(my_team[draft_pick[0]]), draft_pick[0].title()) else: print '{}. {}'.format(draft_pick[1], draft_pick[0].title()) else: # If the input is valid, remove player from draft and add them to a team if (player in all_players or player[0] == '\\') and player not in taken: player = player.replace('\\', '') success = True if pick in pick_numbers: # Update which players are on user's team my_team[player] = pick # Add the player to the team database cursor.execute("""INSERT INTO Teams VALUES ("{}", "{}")""".format(player, ME)) else: user = raw_input(("Who drafted " + player.title() + "?: ")).lower() taken.append(player) # If the user is new, add them to the database if user not in all_users: all_users.append(user) cursor.execute("""INSERT INTO Participants VALUES ("{}");""".format(user)) # Add the player to the team database cursor.execute("""INSERT INTO Teams VALUES ("{}", "{}")""".format(player, user)) elif player in taken: print '{} has already been drafted'.format(player.title()) # Suggests player names if input is incorrect else: Helper.check_incorrect_input(player, set(all_players) - set(taken)) # Close database connection cursor.close() connection.close()