def generate_print_output(league): players, alternates, teams, team_rating_bounds, alt_rating_bounds, alts_split =\ league['players'], league['alternates'], league['teams'], \ league['team_rating_bounds'], league['alt_rating_bounds'], league['alts_split'] boards = len(teams[0].boards) num_teams = len(teams) terminal.separator() print("Team rating range: ", team_rating_range(teams)) print("Team rating variance: ", team_rating_variance(teams)) print("Total happiness: ", total_happiness(teams)) print(f"Using: {len(players)} players and {len(alternates)} alternates") print(terminal.green(f"Previous Season Alternates")) print(terminal.blue(f"Requested Alternate")) print("TEAMS") terminal.smallheader("Team #") for i in range(boards): n,x = team_rating_bounds[i] terminal.largeheader(f"Board #{i+1} [{n},{x})") terminal.largeheader("Mean rating") print() for team_i in range(num_teams): terminal.smallcol(f"#{team_i+1}") for board_i in range(boards): team = teams[team_i] player = team.boards[board_i] short_name = player.name[:20] player_name = f"{short_name} ({player.rating})" terminal.largecol(player_name, terminal.green if player.previous_season_alt else None) terminal.largecol("{0:.2f}".format(team.getMean())) print() print() print("ALTERNATES") terminal.smallheader(" ") for i in range(boards): n,x = alt_rating_bounds[i] terminal.largeheader(f"Board #{i+1} [{n},{x})") print() for player_i in range(max([len(a) for a in alts_split])): terminal.smallcol(" ") for board_i in range(boards): board = alts_split[board_i] player_name = "" if player_i < len(board): player = board[player_i] short_name = player.name short_name = player.name[:20] player_name = f"{short_name} ({player.rating})" terminal.largecol(player_name, terminal.blue if player.alt else None) print()
def test_colors(): print(terminal.cyan('cyan color')) print(terminal.blue('blue color')) print(terminal.yellow('yellow color')) print(terminal.magenta('magenta color')) print(terminal.black('black color')) print(terminal.white('white color')) print(terminal.gray('gray color')) print(terminal.grey('grey color')) print(terminal.red(terminal.green('green color'))) print(terminal.cyan_bg('cyan background')) print(terminal.blue_bg('blue background')) print(terminal.yellow_bg('yellow background')) print(terminal.magenta_bg('magenta background')) print(terminal.black_bg('black background')) print(terminal.white_bg('white background')) print(terminal.gray_bg('gray background')) print(terminal.grey_bg('grey background')) print(terminal.red_bg(terminal.green_bg('green background')))
def make_teams(players, output, boards): # input file is JSON data with the following keys: rating, name, in_slack, account_status, date_created, prefers_alt, friends, avoid, has_20_games. with open(players, 'r') as infile: playerdata = json.load(infile) print("This data was read from file.") # put player data into Player objects players = [] for player in playerdata: if player['has_20_games'] and player['in_slack']: players.append(Player.player_from_json(player)) else: print("{0} skipped".format(player['name'])) players.sort(key=lambda player: player.rating, reverse=True) # Split into those that want to be alternates vs those that do not. alternates = [p for p in players if p.alt] players = [p for p in players if not p.alt] # splits list of Player objects into 6 near equal lists, sectioned by rating players_split = split_into_equal_groups_by_rating(players, boards) team_rating_bounds = get_rating_bounds_of_split(players_split) num_teams = int(math.ceil((len(players_split[0]) * 0.8) / 2.0) * 2) print(f"Targetting {num_teams} teams") # separate latest joining players into alternate lists as required for n, board in enumerate(players_split): board.sort(key=lambda player: (0 if player.previous_season_alt else 1, player.date)) alternates.extend(board[num_teams:]) del board[num_teams:] board.sort(key=lambda player: player.rating, reverse=True) alts_split = split_into_equal_groups_by_rating(alternates, boards) alt_rating_bounds = get_rating_bounds_of_split(alts_split) players = sum(players_split, []) #print len(players) #print num_teams #print alts_split # snake draft players into initial teams and update player and team attributes for board in players_split[1::2]: board.reverse() for n, board in enumerate(players_split): for player in board: player.board = n teams = [] for n in range(num_teams): teams.append(Team(boards)) for n, board in enumerate(players_split): for team, player in enumerate(board): teams[team].changeBoard(n, player) # convert players' friends and avoid from name to references of the friend's/avoid's player object for player in players: if player.friends or player.avoid: player.friends = re.split( "[^-_a-zA-Z0-9]+", player.friends ) # separate friends requests into individual usernames - split on any number of non-(alphanumeric, hyphen or underscore) player.avoid = re.split( "[^-_a-zA-Z0-9]+", player.avoid ) # separate avoid requests into individual usernames - split on any number of non-(alphanumeric, hyphen or underscore) else: player.friends = [] player.avoid = [] for player in players: temp_friends = [] for friend in player.friends: for potentialfriend in players: if friend.lower() == potentialfriend.name.lower( ) and potentialfriend not in temp_friends: # prevent duplicated friend error temp_friends.append(potentialfriend) player.friends = temp_friends temp_avoid = [] for avoid in player.avoid: for potentialavoid in players: if avoid.lower() == potentialavoid.name.lower( ) and potentialavoid not in temp_avoid: # prevent duplicated friend error temp_avoid.append(potentialavoid) player.avoid = temp_avoid #remove friend requests for same board for player in players: for friend in player.friends: if friend.board == player.board: player.friends.remove(friend) updatePref(players, teams) updateSort(players, teams) def swapPlayers(teama, playera, teamb, playerb, board): #swap players between teams - ensure players are same board for input teama.changeBoard(board, playerb) teamb.changeBoard(board, playera) def testSwap(teama, playera, teamb, playerb, board): #try a swap and return the preference change if this swap was made prior_pref = teama.team_pref_score + teamb.team_pref_score swapPlayers(teama, playera, teamb, playerb, board) #swap players forwards updatePref(players, teams) post_pref = teama.team_pref_score + teamb.team_pref_score swapPlayers(teama, playerb, teamb, playera, board) #swap players back updatePref(players, teams) return post_pref - prior_pref #more positive = better swap # take player from least happy team # calculate the overall preference score if player were to swap to each of the preferences' teams or preference swaps into their team. # swap player into the team that makes the best change to overall preference # check if the swap has increased the overall preference rating # if swap made, resort list by preference score and start at the least happy player again # if no improving swaps are available, go to the next player # if end of the list reached with no swaps made: stop p = 0 while p < len(players): player = players[p] #least happy player swaps = [] for friend in player.friends: #test both direction swaps for each friend and whichever is better, add the swap ID and score to temp friends list if friend.board != player.board and friend.team != player.team: #board check is redundant due to earlier removal of same board requests #test swap friend to player team (swap1) swap1_ID = (friend.team, friend, player.team, player.team.getPlayer(friend.board), friend.board) swap1_score = testSwap(*swap1_ID) #test swap player to friend team (swap2) swap2_ID = (player.team, player, friend.team, friend.team.getPlayer(player.board), player.board) swap2_score = testSwap(*swap2_ID) swaps.append( max((swap1_score, swap1_ID), (swap2_score, swap2_ID))) for avoid in player.avoid: #test moving player to be avoided to the best preferred team if player.team == avoid.team: #otherwise irrelevant for swap_team in teams: swap_ID = (avoid.team, avoid, swap_team, swap_team.getPlayer(avoid.board), avoid.board) swap_score = testSwap(*swap_ID) swaps.append((swap_score, swap_ID)) swaps.sort() if swaps and swaps[-1][ 0] > 0: # there is a swap to make and it improves the preference score swapPlayers(*(swaps[-1][1])) print(swaps[-1]) updatePref(players, teams) updateSort(players, teams) p = 0 else: # go to the next player in the list p += 1 for player in players: player.setReqMet() #WIP for upload format for heltour jsonoutput = [] #[{"action":"change-member","team_number":1,"board_number":1,"player":{"name":"lemonworld","is_captain":false,"is_vice_captain":false}}] for t, team in enumerate(teams): for b, board in enumerate(team.boards): pp = { "action": "change-member", "team_number": t + 1, "board_number": b + 1, "player": { "name": board.name, "is_captain": False, "is_vice_captain": False } } jsonoutput.append(pp) for b, board in enumerate(alts_split): print(board) for _, pp in enumerate(board): pp = { "action": "create-alternate", "board_number": b + 1, "player_name": pp.name } jsonoutput.append(pp) if output == "readable": terminal.separator() print( f"Using: {len(players)} players and {len(alternates)} alternates") print(terminal.green(f"Previous Season Alternates")) print(terminal.blue(f"Requested Alternate")) print("TEAMS") terminal.smallheader("Team #") for i in range(boards): n, x = team_rating_bounds[i] terminal.largeheader(f"Board #{i+1} [{n},{x})") print() for team_i in range(num_teams): terminal.smallcol(f"#{team_i+1}") for board_i in range(boards): team = teams[team_i] player = team.boards[board_i] short_name = player.name[:20] player_name = f"{short_name} ({player.rating})" terminal.largecol( player_name, terminal.green if player.previous_season_alt else None) print() print() print("ALTERNATES") terminal.smallheader(" ") for i in range(boards): n, x = alt_rating_bounds[i] terminal.largeheader(f"Board #{i+1} [{n},{x})") print() for player_i in range(max([len(a) for a in alts_split])): terminal.smallcol(" ") for board_i in range(boards): board = alts_split[board_i] player_name = "" if player_i < len(board): player = board[player_i] short_name = player.name short_name = player.name[:20] player_name = f"{short_name} ({player.rating})" terminal.largecol(player_name, terminal.blue if player.alt else None) print() elif output == "json": print(json.dumps(jsonoutput))
def print_title(self, title): if 'Option' in title: print(terminal.magenta(title)) elif 'Command' in title: print(terminal.green(title)) return self
def Result(): return terminal.bold(terminal.green(r"[Result] "))
def green(text): return terminal.green(text)