def getHeadToHeadTable(tourney_id): """returns a table of the player's head to head results. see end of the function for exactly what is returned""" #Load all finished games finishedGames = clot.getFinishedGames(tourney_id) logging.info("finishedGames:") logging.info(finishedGames) #get player_id : name dict players_id_name_dict = clot.getPlayersIDNameDict(tourney_id) logging.info('players_id_name_dict') logging.info(players_id_name_dict) #get list of players, sorted by currentRank, highest First. players_sorted_by_rank = [[p.player_id, p.currentRank] for p in players.Player.all().filter("tourney_id =", tourney_id)] players_sorted_by_rank.sort(key=lambda x: x[1]) players_ids_sorted_by_rank = [int(p[0]) for p in players_sorted_by_rank] ##logging.info('players_ids_sorted_by_rank') ##logging.info(players_ids_sorted_by_rank) #Group finished games by who won finishedGamesGroupedByWinner = main.group(finishedGames, lambda g: g.winner) logging.info("finishedGamesGroupedByWinner:") logging.info(finishedGamesGroupedByWinner) #make the head-to-head table head_to_head_2d = [[getHeadToHead(p,o,finishedGamesGroupedByWinner) for o in players_ids_sorted_by_rank] for p in players_ids_sorted_by_rank] players_for_h2h = [p for p in players_ids_sorted_by_rank] logging.info('head_to_head_2d:') logging.info(head_to_head_2d) #now package it up for the html biggermat = deepcopy(head_to_head_2d) biggermat.insert(0,players_for_h2h) players_for_h2h_padded = deepcopy(players_for_h2h) players_for_h2h_padded_22 = [str(player) + '::'+str(players_id_name_dict[player]) for player in players_for_h2h_padded] logging.info(players_for_h2h_padded_22) players_for_h2h_padded_22.insert(0,'player_id') for i,j in zip(biggermat, players_for_h2h_padded_22): i.insert(0,j) logging.info(biggermat) #biggermat [x][y] for x,y>0 is the number of games player x won against player y. #biggermat [0][y] for y>0 is the player_id of player y. #biggermat [x][0] for x>0 is the player_id of player x. return biggermat, head_to_head_2d
def getTourneyRoundsAndGameInfo(tourney_id): """this function returns a table of strings, for display on the /tourneys/tourney_id html page. the information is specific to swiss tournaments. the information is just a list of the game in each round, showing who won and who lost""" #Load all finished games finished_games = clot.getFinishedGames(tourney_id) finished_games_sorted_by_creation_date = [game for game in finished_games] finished_games_sorted_by_creation_date.sort(key=lambda x: x.dateCreated) #logging.info("finished_games_sorted_by_creation_date:") #logging.info(finished_games_sorted_by_creation_date) #get player_id : name dict players_id_name_dict = clot.getPlayersIDNameDict(tourney_id) #logging.info('players_id_name_dict') #logging.info(players_id_name_dict) #get list of players, sorted by currentRank, highest First. players_sorted_by_rank = [[p.player_id, p.currentRank] for p in players.Player.all().filter("tourney_id =", tourney_id)] ###.run(batch_size=1000)] players_sorted_by_rank.sort(key=lambda x: x[1]) players_ids_sorted_by_rank = [int(p[0]) for p in players_sorted_by_rank] ##logging.info('players_ids_sorted_by_rank') ##logging.info(players_ids_sorted_by_rank) #~~~~~~ player_game_count = {} #key is player_id for player_id in players_ids_sorted_by_rank: player_game_count[player_id] = 0 #make the games_string_table num_games_per_round = len(players_ids_sorted_by_rank)/2 i_round = 1 i_game = 0 games_string_table = [['round 1']] tmp = [] for game in finished_games_sorted_by_creation_date: i_game += 1 if i_game > num_games_per_round: #we have moved on to the next round. so dump previous round's data into games_string_table. i_round += 1 i_game = 1 if i_round > 1: games_string_table.append(['round '+str(i_round)]) tmp.sort() #logging.info('tmp:') #logging.info(tmp) for g in tmp: games_string_table[i_round-2].append(g[1]) tmp = [] winner_id = game.winner loser_id = game.loser winner_name = players_id_name_dict[winner_id].name loser_name = players_id_name_dict[loser_id].name game_name_str = winner_name +'\n beat '+ loser_name player_game_count[winner_id] += 1 player_game_count[loser_id] += 1 best_rank = min(players_ids_sorted_by_rank.index(winner_id) , players_ids_sorted_by_rank.index(loser_id)) if tmp==[]: tmp = [[best_rank,game_name_str]] else: tmp.append([best_rank,game_name_str]) counter = 0 tmp.sort() for g in tmp: counter += 1 games_string_table[i_round-1].append(g[1]) while counter < num_games_per_round: counter += 1 games_string_table[i_round-1].append('---') #logging.info('games_string_table:') #logging.info(games_string_table) games_string_table_transposed = zip(*games_string_table) #transpose the table return games_string_table_transposed