Ejemplo n.º 1
0
def main():
    
    # Get configuration parameters
    cfg = set_config()
    
    # Get Player Database
    player_db = PlayerDatabase(cfg)
    
    # Add teams to database
    draftteams = DraftTeams(cfg)
    for team in cfg['teams']:
        draftteams.add_team(team)
        
    # Startup console
    print
    print ''
    print '*'*50
    print 'DraftMatic!!!'
    print '*'*50
    
    # Create draft list history
    draft_history = []
    
    # Initialize to print players for next team
    print_next_team = False
    
    # Main Loop
    while True:
        
        # Read in draft state
        draftteams.load_state(player_db)
        
        # Get round number for next team and for primary team
        n_round = draftteams.round()
        n_round_primary = draftteams.next_round_for_team(cfg['primary_team'])
        
        if n_round >= cfg['num_rounds']:
            print '-'*50
            print ''
            
            for team in cfg['teams']:
                print team+':'
                print_team_summary(cfg, player_db, draftteams, team)
                print ''
            
            print '-'*50
            
            exit()
        
        # Get pick number
        n_pick = draftteams.get_pick_num()
        n_pick_primary = draftteams.get_pick_num_for_team(cfg['primary_team'])
        n_overall_pick = draftteams.get_overall_pick_num()
        n_overall_pick_primary = draftteams.get_overall_pick_num_for_team(cfg['primary_team'])

        # Get drafter
        drafter = draftteams.get_drafter()
        
        # Get list of drafted players
        drafted_players = draftteams.drafted_players()

        # Get position weights for the next drafter
        pos_weights = draftteams.teams[drafter].get_pos_weights(n_round)
        if n_round_primary < cfg['num_rounds']:
            pos_weights_primary = draftteams.teams[cfg['primary_team']].get_pos_weights(n_round_primary)

        # Rank players
        player_db.rank_players(draftteams, drafted_players, n_round, pos_weights, n_overall_pick+1)
        if n_round_primary < cfg['num_rounds']:
            player_db_primary = copy.deepcopy(player_db)
            player_db_primary.rank_players(draftteams, drafted_players, n_round_primary, pos_weights_primary, n_overall_pick_primary+1)
	    
        # Only print 5 ranking by default
        n_print_players = 10
        print_round_summary = True

        # Console loop until valid draft command is received
        while True:
        
            # Print round number
            if print_round_summary:
                print ''
                print '-'*50
                print 'Round {}-{} ({})'.format(n_round+1,n_pick+1,n_overall_pick+1)
                print '{} is next to draft'.format(drafter)
                print_team_summary(cfg, player_db, draftteams, drafter)
                print '-'*50
                print ''
                print_round_summary = False
                
            # Set printed player database
            if print_next_team == True:
            
                print 'Picks for next drafter ({}), {}-{} ({}):'.format(drafter,n_round+1,n_pick+1,n_overall_pick+1)
                player_db_print = player_db
                
            else:
                
                if n_round_primary < cfg['num_rounds']:
                    print 'Picks for primary drafter ({}), {}-{} ({}):'.format(cfg['primary_team'],n_round_primary+1,n_pick_primary+1,n_overall_pick_primary+1)
                    player_db_print = player_db_primary
                else:
                    print 'Picks complete for primary drafter ({})'.format(cfg['primary_team'])
                    player_db_print = player_db
            
            # Print out rankings short list
            players_to_plot_vbd = pd.DataFrame(columns=cfg['starters'].keys())
            players_to_plot_rank = pd.DataFrame(columns=cfg['starters'].keys())
            players_to_plot_adp = pd.DataFrame(columns=cfg['starters'].keys())
            players_to_plot_ecr = pd.DataFrame(columns=cfg['starters'].keys())
            n_print_players = min(n_print_players, len(player_db_print.rank))
            for i in range(0, n_print_players):
                print_player_summary(player_db_print, n_overall_pick+i, player_db_print.rank[i])
                players_to_plot_vbd.set_value(n_overall_pick+i+1,
                                              player_db_print.position[player_db_print.rank[i]],
                                              player_db_print.get_vbd(player_db_print.rank[i]))
                players_to_plot_rank.set_value(n_overall_pick+i+1,
                                               player_db_print.position[player_db_print.rank[i]],
                                               player_db_print.get_ranking_score(player_db_print.rank[i]))
                if player_db_print.rank[i] in player_db_print.adp:
                    players_to_plot_adp.set_value(n_overall_pick+i+1,
                                                  player_db_print.position[player_db_print.rank[i]],
                                                  player_db_print.adp[player_db_print.rank[i]])
                if player_db_print.rank[i] in player_db_print.ecr:
                    players_to_plot_ecr.set_value(n_overall_pick+i+1,
                                                  player_db_print.position[player_db_print.rank[i]],
                                                  player_db_print.ecr[player_db_print.rank[i]])
            
            # Update player 
            if n_print_players > 0:
                
                plt.figure(1)
                plt.close()
                plt.subplot(221)
                legend_strings = []
                # Loop over each position
                for position in players_to_plot_rank:
                    # Get players in this position
                    players_to_plot_for_pos = players_to_plot_rank[position].dropna()
                    if players_to_plot_for_pos.size > 0:
                        # Plot position ranks and add position to legend
                        plt.plot(players_to_plot_for_pos,'-o')
                        legend_strings.append(position)
                for i in range(0, n_print_players):
                    plt.text(n_overall_pick+i+1,
                             player_db_print.get_ranking_score(player_db_print.rank[i]),
                             player_db_print.rank[i])
                # Append text for each player
                plt.xlabel('Rank')
                plt.ylabel('Weighted Ranking Score')
                plt.legend(legend_strings)
                plt.subplot(222)
                legend_strings = []
                # Loop over each position
                for position in players_to_plot_vbd:
                    # Get players in this position
                    players_to_plot_for_pos = players_to_plot_vbd[position].dropna()
                    if players_to_plot_for_pos.size > 0:
                        # Plot position ranks and add position to legend
                        plt.plot(players_to_plot_for_pos,'-o')
                        legend_strings.append(position)
                for i in range(0, n_print_players):
                    plt.text(n_overall_pick+i+1,
                             player_db_print.get_vbd(player_db_print.rank[i]),
                             player_db_print.rank[i])
                # Append text for each player
                plt.xlabel('Rank')
                plt.ylabel('Value Based Draft Score (VBD)')
                plt.legend(legend_strings)
                plt.subplot(223)
                legend_strings = []
                # Loop over each position
                for position in players_to_plot_adp:
                    # Get players in this position
                    players_to_plot_for_pos = players_to_plot_adp[position].dropna()
                    if players_to_plot_for_pos.size > 0:
                        # Plot position ranks and add position to legend
                        plt.plot(players_to_plot_for_pos,'-o')
                        legend_strings.append(position)
                for i in range(0, n_print_players):
                    if player_db_print.rank[i] in player_db_print.adp:
                        plt.text(n_overall_pick+i+1,
                                 player_db_print.adp[player_db_print.rank[i]],
                                 player_db_print.rank[i])
                # Append text for each player
                plt.xlabel('Rank')
                plt.ylabel('Average Draft Position (ADP)')
                plt.gca().invert_yaxis()
                plt.legend(legend_strings)
                plt.subplot(224)
                legend_strings = []
                # Loop over each position
                for position in players_to_plot_ecr:
                    # Get players in this position
                    players_to_plot_for_pos = players_to_plot_ecr[position].dropna()
                    if players_to_plot_for_pos.size > 0:
                        # Plot position ranks and add position to legend
                        plt.plot(players_to_plot_for_pos,'-o')
                        legend_strings.append(position)
                for i in range(0, n_print_players):
                    if player_db_print.rank[i] in player_db_print.ecr:
                        plt.text(n_overall_pick+i+1,
                                 player_db_print.ecr[player_db_print.rank[i]],
                                 player_db_print.rank[i])
                # Append text for each player
                plt.xlabel('Rank')
                plt.ylabel('Expert Consensus Ranking (ECR)')
                plt.gca().invert_yaxis()
                plt.legend(legend_strings)
                plt.show(block=False)
            
            # Prompt user for pick
            print ''
            console_inp = raw_input('Enter Pick (''h'' for help): ')
	        		    
            # Process command
            if console_inp == 'h':
            
                print ''
                print 'Type desired player to draft or one of the following commands'
                print 'exit/quit - exit program'
                print 'reset     - restart draft'
                print '*         - toggle showing players for next drafting team or primary team'
                print '#         - desired number of players to see'
                print 'teams     - print summary of teams'
                print 'undo      - undo draft pick'
            
                n_print_players = 0
            
            elif (console_inp == 'exit') or (console_inp == 'quit'):
                
                # Exit on command
                sys.exit()
                
            elif console_inp == 'reset':
            
                # Reset draft 
                draftteams.reset()
                
                # Reset draft history
                draft_history = []
                
                # Print 5 players
                n_print_players = min(10, len(player_db_print.rank))
                print_round_summary = True
                
                break
                
            elif console_inp == '*':
            
                # Toggle print mode
                print_next_team = not print_next_team
                
                # Print 5 players
                n_print_players = min(10, len(player_db_print.rank))
                print_round_summary = True
                
                break
                
            elif console_inp.isdigit():
                
                # Set number of rankings list to print
                n_print_players = int(console_inp)
                print_round_summary = True
                
                # Saturate at max ranking
                n_print_players = min(n_print_players, len(player_db_print.rank))
                
                print ''
                
            elif console_inp == 'teams':
            
                print '-'*50
                print ''
                
                for team in cfg['teams']:
                    print team+':'
                    print_team_summary(cfg, player_db, draftteams, team)
                    print ''
                
                print '-'*50
                
                n_print_players = 0
                
            elif console_inp == 'undo':
            
                # Undo last draft pick
                if len(draft_history) > 0:
                    drafter = draft_history[-1][0]
                    n_round = draft_history[-1][1]
                    draftteams.teams[drafter].undraft_player(n_round)
                    del draft_history[-1]
                
                    # Print 5 players
                    n_print_players = min(10, len(player_db_print.rank))
                    print_round_summary = True
                else:
                    print ''
                    print 'No draft history available'
                    
                    n_print_players = 0
                    
                break
                
            elif console_inp in drafted_players:
            
                print console_inp+' previously drafted'
                n_print_players = 0
                
            elif console_inp in player_db.position:
            
                # Draft player and break loop
                draftteams.teams[drafter].draft_player(n_round,
                                                       console_inp,
                                                       player_db.position[console_inp],
                                                       player_db)
                
                # Add drafter to history
                draft_history.append((drafter,n_round))
                
                # Print 5 players
                n_print_players = min(5, len(player_db.rank))
                print_round_summary = True
                
                print console_inp+' ('+player_db.position[console_inp]+') successfully drafted'  
                
                break
                
            else:
                
                # Don't reprint list
                n_print_players = 0
                
                # Print error message
                print ''
                print console_inp+' not recognized'
                console_verification = raw_input('Are you sure? [y/n]:')
                if console_verification == 'y':
                    console_pos = raw_input('Position:')
                    if console_pos in cfg['starters']:
                        # Draft player and break loop
                        draftteams.teams[drafter].draft_player(n_round,
                                                               console_inp,
                                                               console_pos,
                                                               player_db)
                                                               
                        # Add drafter to history
                        draft_history.append((drafter,n_round))
                        
                        # Print 5 players
                        n_print_players = min(10, len(player_db_print.rank))
                        print_round_summary = True
                        
                        print console_inp+' ('+player_db.position[console_inp]+') successfully drafted'  
                    else:
                        print console_pos + ' not a recognized position'
                        
                        n_print_players = 0
                
                break
                                               
        # Write out draft state
        draftteams.write_state()
Ejemplo n.º 2
0
def main():
    
    # Get configuration parameters
    cfg = set_config()
    
    # Get Player Database
    player_db = PlayerDatabase(cfg)
    
    # Add teams to database
    draftteams = DraftTeams(cfg)
    for team in cfg['teams']:
        draftteams.add_team(team)
        
    # Get yahoo registration info
    if False:
        yhandler = YHandler('auth.csv')
        yhandler.reg_user()
    
        pdb.set_trace()
    
        temp = yhandler.api_req("select * from fantasysports.draftresults where league_key='238.l.627060'")
    
    # Startup console
    print
    print ''
    print '*'*50
    print 'DraftMatic!!!'
    print '*'*50
    
    # Create draft list history
    draft_history = []
    
    # Main Loop
    while True:
        
        # Read in draft state
        draftteams.load_state(player_db)
        
        # Get round number
        n_round = draftteams.round()
        
        if n_round >= len(cfg['baseline_depth']):
            print '-'*50
            print ''
            
            for team in cfg['teams']:
                print team+':'
                print_team_summary(cfg, player_db, draftteams, team)
                print ''
            
            print '-'*50
            
            exit()
        
        # Get pick number
        n_pick = draftteams.get_pick_num()
        n_overall_pick = draftteams.get_overall_pick_num()

        # Get drafter
        drafter = draftteams.get_drafter()
        
        # Get list of drafted players
        drafted_players = draftteams.drafted_players()

        # Get position weights for the next drafter
        pos_weights = draftteams.teams[drafter].get_pos_weights(n_round)

        # Rank players
        player_db.rank_players(draftteams, drafted_players, n_round, pos_weights, n_overall_pick+1)
	
        # Only print 5 ranking by default
        n_print_players = min(5, len(player_db.rank))
        print_round_summary = True

        # Console loop until valid draft command is received
        while True:
        
            # Print round number
            if print_round_summary:
                print ''
                print '-'*50
                print 'Round {}-{} ({})'.format(n_round+1,n_pick+1,n_overall_pick+1)
                print '{} is next to draft'.format(drafter)
                print_team_summary(cfg, player_db, draftteams, drafter)
                print '-'*50
                print ''
                print_round_summary = False
        
            # Print out rankings short list
            n_print_players = min(n_print_players, len(player_db.rank))
            for i in range(0, n_print_players):
                print_player_summary(player_db, n_overall_pick+i, player_db.rank[i])
            
            # Prompt user for pick
            print ''
            console_inp = raw_input('Enter Pick (''h'' for help): ')
		
            # Process command
            if console_inp == 'h':
            
                print ''
                print 'Type desired player to draft or one of the following commands'
                print 'exit    - exit program'
                print 'reset   - restart draft'
                print '#       - desired number of players to see'
                print 'teams   - print summary of teams'
                print 'undo    - undo draft pick'
            
                n_print_players = 0
            
            elif console_inp == 'exit':
                
                # Exit on command
                sys.exit()
                
            elif console_inp == 'reset':
            
                # Reset draft 
                draftteams.reset()
                
                # Reset draft history
                draft_history = []
                
                # Print 5 players
                n_print_players = min(5, len(player_db.rank))
                print_round_summary = True
                
                break
                
            elif console_inp.isdigit():
                
                # Set number of rankings list to print
                n_print_players = int(console_inp)
                print_round_summary = True
                
                # Saturate at max ranking
                n_print_players = min(n_print_players, len(player_db.rank))
                
                print ''
                
            elif console_inp == 'teams':
            
                print '-'*50
                print ''
                
                for team in cfg['teams']:
                    print team+':'
                    print_team_summary(cfg, player_db, draftteams, team)
                    print ''
                
                print '-'*50
                
                n_print_players = 0
                
            elif console_inp == 'undo':
            
                # Undo last draft pick
                if len(draft_history) > 0:
                    drafter = draft_history[-1][0]
                    n_round = draft_history[-1][1]
                    draftteams.teams[drafter].undraft_player(n_round)
                    del draft_history[-1]
                
                    # Print 5 players
                    n_print_players = min(5, len(player_db.rank))
                    print_round_summary = True
                else:
                    print ''
                    print 'No draft history available'
                    
                    n_print_players = 0
                    
                break
                
            elif console_inp in drafted_players:
            
                print console_inp+' previously drafted'
                n_print_players = 0
                
            elif console_inp in player_db.position:
            
                # Draft player and break loop
                draftteams.teams[drafter].draft_player(n_round,
                                                       console_inp,
                                                       player_db.position[console_inp],
                                                       player_db)
                
                # Add drafter to history
                draft_history.append((drafter,n_round))
                
                # Print 5 players
                n_print_players = min(5, len(player_db.rank))
                print_round_summary = True
                
                print console_inp+' ('+player_db.position[console_inp]+') successfully drafted'  
                
                break
                
            else:
                
                # Don't reprint list
                n_print_players = 0
                
                # Print error message
                print ''
                print console_inp+' not recognized'
                console_verification = raw_input('Are you sure? [y/n]:')
                if console_verification == 'y':
                    console_pos = raw_input('Position:')
                    if console_pos in cfg['starters']:
                        # Draft player and break loop
                        draftteams.teams[drafter].draft_player(n_round,
                                                               console_inp,
                                                               console_pos,
                                                               player_db)
                                                               
                        # Add drafter to history
                        draft_history.append((drafter,n_round))
                        
                        # Print 5 players
                        n_print_players = min(5, len(player_db.rank))
                        print_round_summary = True
                        
                        print console_inp+' ('+player_db.position[console_inp]+') successfully drafted'  
                    else:
                        print console_pos + ' not a recognized position'
                        
                        n_print_players = 0
                
                break
                                               
        # Write out draft state
        draftteams.write_state()