def sim_scheduled_games(sch_game_dicts, n):
    """
    
    """
#    print sch_game_dicts.keys()
#    print
    prediction_dict = {}
    CandP = CombosAndPermuts()
    trifs = CandP.get_trifectas()
    str_trifs = [str(x[0]) + str(x[1]) + str(x[2]) for x in trifs]
#[x for x in range(1, self.numPlayers +1)]
    win = [x[0] for x in trifs] ; place = [x[1] for x in trifs] ; show = [x[2] for x in trifs]
    wpsTable = pd.DataFrame(data = {'First':win,'Second':place, 'Third':show}, index = str_trifs)
    #print wpsTable.head()
    #print wpsTable.columns.values.tolist()
   

    for game_name,game_data in sch_game_dicts.items():

        pwin_dict = rds.get_pwin_dict()

        point_percentage_data = None
        player_list = []
        enh_player_list = []
        #print game
#        for el in game:
#            print el
        enh_player_list = get_enhanced_player_list(game_data)

        #print game_name, enh_player_list
        if enh_player_list != None:
            #print
            #print game_name
            for player_dict in enh_player_list:
                
                player_list.append(':'.join(ut.sorted_and_lowered(player_dict['name'])))

#            point_percentage_data = sc.runGame_get_WPS_frequencies(player_list, n)

            point_percentage_data = sc.runGame_get_WPS_frequencies(player_list, pwin_dict, n)
            


            prediction_dict[game_name] = point_percentage_data
    pred_dict_df = pd.DataFrame.from_dict(prediction_dict)
    #print pred_dict_df.head()
    full_df = pd.merge(wpsTable, pred_dict_df, left_index = True, right_index = True )
    #print full_df.head()

    #print prediction_dict
    fh.write_dict(fh.prediction_dict_file_path,prediction_dict)
    for game_name in sch_game_dicts.keys():
        try:
#        if game_name in full_df.colnames(full_df):
            full_df[game_name] = full_df[game_name]/n
        except:
            pass

    #print full_df.columns.values.tolist()   
    return full_df, prediction_dict
def get_prediction_actual_results_dict(prediction_dict, res_game_dicts):
    """
    This looks the similar to the prediction dict 
    ma-01-01-1996_res_Milford_14
[{'aragues:lasa': [0, 1, 0, 0]}, {'azpiri:zabala': [1, 0, 0, 0]}, {'acin:beitia': [0, 0, 0, 0]}................. for all players in the game]}]
    
    """
    actual_results_dict = {}
    # Get corresponding results file
        
    for game_name, game_data in prediction_dict.items():
        game_player_list = [game_data[x].keys() for x in range(len(game_data))] # game_data

        res_dict = res_game_dicts.get(game_name.replace("sch", "res"))

        if res_dict != None:
#            print
#            print game_name
#            print res_dict
            #print
            actual_results_dict_data = []
            #game_data_dict = {}
            for p in game_player_list:
                p_dict = {}
                #print game_name
                p = p[0].split(":")
                #print p, dp.get_player_game_result(res_dict, p)
                #p_dict[p[0]] = dp.get_player_game_result(res_dict, p)
                p_dict[':'.join(ut.sorted_and_lowered(p))] = dp.get_player_game_result(res_dict, p)
                #print p_dict[p[0]]
                p = ':'.join(ut.sorted_and_lowered(p))
                #print p
                actual_results_dict_data.append(p_dict)
            actual_results_dict[game_name] = actual_results_dict_data
            #print game_player_list
#    for k,v in actual_results_dict.items():
#        print
#        print k,v
#    print
#    print
#    for k, v in prediction_dict.items():
#        print
#        print k,v
    fh.write_dict(fh.prediction_actual_results_dict_file_path,actual_results_dict)
    return actual_results_dict
def get_enhanced_player_list(sch_game_dict):
    """
    Accepts a scheduled game dictionary such as:
    {'ma-01-01-1998_sch_Milford_1':{1: ['Ara', 'Arruti'], 2: ['Douglas', 'Edward'], 3: ['Liam', 'Fitz'], 4: ['Tino', 'Aritz'], 'S/D': 'Doubles', 6: ['Aitor', 'Sergio'], 7: ['Zarandona', 'Wayne'], 8: ['Eggy', 'Richard'], 'GAME': 1, 'DATE': '01/01/1998', 5: ['Aja', 'Alberto'], 'POS-SUB': [52, 11], 6: ['Altuna', 'Guisasola'], 'POINTS': 7, 'DAY': 'Thursday', 'GAME-COUNT': 15, 'POS-4-ID': [36, 17], 'POS-8-ID': [2, 12], 'POS-SUB-ID': [15, 18], 'POS-3-ID': [6, 9], 'POS-7-ID': [38, 20], 'FRONTON': 'Milford', 'POS-5-ID': [34, 13], 'ABSOLUTE-DATE': 35795, 'POS-2-ID': [42, 37], 'POS-1-ID': [4, 28]}}
    Looks up the probability of winning and returns a player list formatted like:    
    playerList = [{'name':['Ara', 'Arruti'],'pwin': 0.56}, {'name':['Douglas', 'Edward'],'pwin': 0.65}, {'name':['Liam', 'Fitz'],'pwin': 0.63}] where position [0] represents start post 1 through to start post # 8
    If any player in the scheduled game does not have a probability of winning associated with him then returns None 
    """ 
    #print sch_game_dict
    real_player_list = []
    player_dict = {}
    temp_list = []
    #print sch_game_dict[sch_game_dict.keys()[0]].get(3) #[sch_game_dict.keys()]
    # Some games have 6 players
    # Get the names in post position order
#    for n in range(1,14):
#        if sch_game_dict[sch_game_dict.keys()[0]].get(n) != None:
#            temp_list.append(sch_game_dict[sch_game_dict.keys()[0]].get(n))
    for n in range(1,14):
        #print sch_game_dict.keys()
        if sch_game_dict.get(n) != None:
            temp_list.append(sch_game_dict.get(n))
    #print temp_list
    pwin_dict = rds.get_pwin_dict()
    #print pwin_dict.keys()
    for pos,player in enumerate(temp_list):
        player_dict = {}
        # break out this comparison as a function
        #print player,':'.join(ut.sorted_and_lowered(player)),
        if ':'.join(ut.sorted_and_lowered(player)) in pwin_dict.keys():
            #print 'in'#,':'.join(ut.sorted_and_lowered(player))
            if pwin_dict.get(':'.join(ut.sorted_and_lowered(player))) != None:
                player_dict['name'] = player ; player_dict['pwin'] = pwin_dict.get(':'.join(ut.sorted_and_lowered(player)))

                real_player_list.append(player_dict)
        # If there is no probability of winning for any player
        #        return None
        else:
            return None
    #print real_player_list
    return real_player_list
def simulate_scheduled_games(sch_game_dicts, ji, n = 12):
    """
    
    """

    prediction_dict = {}
    # Setr the variable here?
    #PointSetPlayer.gamma = 0.4
#    game_dict = {}
    #print sch_game_dicts.keys()
    for game_name,game_data in sch_game_dicts.items():

        point_percentage_data = None
        player_list = []
        enh_player_list = []
        #print game
#        for el in game:
#            print el
        enh_player_list = get_enhanced_player_list(game_data)

        #print enh_player_list
        if enh_player_list != None:
            #print
            #print game_name
            for player_dict in enh_player_list:
                
                player_list.append(':'.join(ut.sorted_and_lowered(player_dict['name'])))
    #        for team in player_list:
    #            team_name = ':'.join(ut.sorted_and_lowered(player))
            #print player_list
            # run the game
            point_percentage_data = sc.runGame_get_WPSL_percentages(player_list, n)
#            for player,position_totals in sorted(point_percentage_data.items()):   
#                print str(player) + ': ' + str(position_totals)
#            for p in player_list:
#                print point_percentage_data[p]
#            for d in point_percentage_data:
#                print d
            prediction_dict[game_name] = point_percentage_data
            #print point_percentage_data
    # Find a way to sort this dictionary in starting post order !!!!!!
    #fh.write_dict(os.path.join(fh.prediction_dict_file_path, str(n)),prediction_dict)
    #print os.path.join(fh.prediction_dict_file_path, str(j))
    ji += 1
#    path, filename = os.path.split(fh.prediction_dict_file_path)
#    filename = os.path.splitext(filename)[0]
#    newfilename = 'ok_%s_%s.txt' % (filename, str(j))
#    newpath = os.path.join(path, newfilename)
#    fh.write_dict(newpath,prediction_dict)
    fh.write_dict(fh.prediction_dict_file_path,prediction_dict)
    return prediction_dict
def simulate_scheduled_games_freq(sch_game_dicts, n = 12):
    """
    
    """

    prediction_dict = {}

    for game_name,game_data in sch_game_dicts.items():

        pwin_dict = rds.get_pwin_dict()

        point_percentage_data = None
        player_list = []
        enh_player_list = []
        #print game
#        for el in game:
#            print el
        enh_player_list = get_enhanced_player_list(game_data)

        #print enh_player_list
        if enh_player_list != None:
            #print
            #print game_name
            for player_dict in enh_player_list:
                
                player_list.append(':'.join(ut.sorted_and_lowered(player_dict['name'])))

#            point_percentage_data = sc.runGame_get_WPS_frequencies(player_list, n)

            point_percentage_data = sc.runGame_get_WPS_frequencies(player_list, pwin_dict, n)


            prediction_dict[game_name] = point_percentage_data

    #print prediction_dict
    fh.write_dict(fh.prediction_dict_file_path,prediction_dict)
    return prediction_dict