示例#1
0
def list_matches(working_dir: WorkingDir, odd_week: bool, show_results: bool):
    """
    Prints all the matches that will be run this week.
    """

    ladder = Ladder.read(working_dir.ladder)
    playing_division_indices = get_playing_division_indices(ladder, odd_week)

    print(f'Matches to play:')

    # The divisions play in reverse order, but we don't print them that way.
    for div_index in playing_division_indices:
        print(f'--- {Ladder.DIVISION_NAMES[div_index]} division ---')

        rr_bots = ladder.round_robin_participants(div_index)
        rr_matches = generate_round_robin_matches(rr_bots)

        for match_participants in rr_matches:

            # Find result if show_results==True
            result_str = ''
            if show_results:
                result_path = working_dir.get_match_result(
                    div_index, match_participants[0], match_participants[1])
                if result_path.exists():
                    result = MatchResult.read(result_path)
                    result_str = f'  (result: {result.blue_goals}-{result.orange_goals})'

            print(
                f'{match_participants[0]} vs {match_participants[1]}{result_str}'
            )
def parse_results(filename):
    """
    If you have the output of the list_results function as a text document, you can use this function to parse it
    out into MatchResult objects. This is a slightly hacky utility function for manual use.
    """
    with open(filename) as f:
        line_list = f.readlines()

    vs_lines = [line for line in line_list if ' vs ' in line]

    results = []

    for line in vs_lines:
        m = re.search(r'(.+) vs (.+)  \(result: ([0-9]+)-([0-9]+)\)', line)
        if m is not None:
            result = MatchResult(blue=m.group(1),
                                 orange=m.group(2),
                                 blue_goals=int(m.group(3)),
                                 orange_goals=int(m.group(4)),
                                 blue_shots=0,
                                 orange_shots=0,
                                 blue_saves=0,
                                 orange_saves=0,
                                 blue_points=0,
                                 orange_points=0)

            results.append(result)

    return results
示例#3
0
 def get_past_result(self, bot_1, bot_2) -> MatchResult:
     path = self.get_result_path(bot_1, bot_2)
     if path.exists():
         try:
             print(f'Found existing result {path.name}')
             return MatchResult.read(path)
         except Exception as e:
             print(f'Error loading result {path.name}. Fix/delete the result and run script again.')
             raise e
     return None
示例#4
0
def fetch_match_score(packet: GameTickPacket):
    blue = packet.game_cars[0]
    orange = packet.game_cars[1]
    return MatchResult(blue=blue.name,
                       orange=orange.name,
                       blue_goals=packet.teams[0].score,
                       orange_goals=packet.teams[1].score,
                       blue_shots=blue.score_info.shots,
                       orange_shots=orange.score_info.shots,
                       blue_saves=blue.score_info.saves,
                       orange_saves=orange.score_info.saves,
                       blue_points=blue.score_info.score,
                       orange_points=orange.score_info.score)
def find_historical_result(bot1: VersionedBot, bot2: VersionedBot,
                           session_result_path: Path,
                           stale_rematch_threshold: int,
                           working_dir: WorkingDir):
    if session_result_path.exists():
        # Found existing result
        try:
            print(f'Found existing result {session_result_path.name}')
            return MatchResult.read(session_result_path)

        except Exception as e:
            print(
                f'Error loading result {session_result_path.name}. Fix/delete the result and run script again.'
            )
            raise e

    return get_stale_match_result(bot1, bot2, stale_rematch_threshold,
                                  working_dir, True)
示例#6
0
def run_league_play(working_dir: WorkingDir, odd_week: bool,
                    replay_preference: ReplayPreference):
    """
    Run a league play event by running round robins for half the divisions. When done, a new ladder file is created.
    """

    bots = load_all_bots(working_dir)
    ladder = Ladder.read(working_dir.ladder)

    # We need the result of every match to create the next ladder. For each match in each round robin, if a result
    # exist already, it will be parsed, if it doesn't exist, it will be played.
    # When all results have been found, the new ladder can be completed and saved.
    new_ladder = Ladder(ladder.bots)
    event_results = []

    # playing_division_indices contains either even or odd indices.
    # If there is only one division always play that division (division 0, quantum).
    playing_division_indices = range(
        ladder.division_count())[int(odd_week) %
                                 2::2] if ladder.division_count() > 1 else [0]

    # The divisions play in reverse order, so quantum/overclocked division plays last
    for div_index in playing_division_indices[::-1]:
        print(
            f'Starting round robin for the {Ladder.DIVISION_NAMES[div_index]} division'
        )

        rr_bots = ladder.round_robin_participants(div_index)
        rr_matches = generate_round_robin_matches(rr_bots)
        rr_results = []

        for match_participants in rr_matches:

            # Check if match has already been play, i.e. the result file already exist
            result_path = working_dir.get_match_result(div_index,
                                                       match_participants[0],
                                                       match_participants[1])
            if result_path.exists():
                # Found existing result
                try:
                    print(f'Found existing result {result_path.name}')
                    result = MatchResult.read(result_path)

                    rr_results.append(result)

                except Exception as e:
                    print(
                        f'Error loading result {result_path.name}. Fix/delete the result and run script again.'
                    )
                    raise e

            else:
                assert match_participants[
                    0] in bots, f'{match_participants[0]} was not found in \'{working_dir.bots}\''
                assert match_participants[
                    1] in bots, f'{match_participants[1]} was not found in \'{working_dir.bots}\''

                # Play the match
                print(
                    f'Starting match: {match_participants[0]} vs {match_participants[1]}. Waiting for match to finish...'
                )
                match_config = make_match_config(working_dir,
                                                 bots[match_participants[0]],
                                                 bots[match_participants[1]])
                match = MatchExercise(
                    name=f'{match_participants[0]} vs {match_participants[1]}',
                    match_config=match_config,
                    grader=MatchGrader(replay_monitor=ReplayMonitor(
                        replay_preference=replay_preference), ))

                # Let overlay know which match we are about to start
                overlay_data = OverlayData(
                    div_index, bots[match_participants[0]].config_path,
                    bots[match_participants[1]].config_path)
                overlay_data.write(working_dir.overlay_interface)

                with setup_manager_context() as setup_manager:
                    # Disable rendering by replacing renderer with a renderer that does nothing
                    setup_manager.game_interface.renderer = FakeRenderer()

                    # For loop, but should only run exactly once
                    for exercise_result in run_playlist(
                        [match], setup_manager=setup_manager):

                        # Warn users if no replay was found
                        if isinstance(
                                exercise_result.grade, Fail
                        ) and exercise_result.exercise.grader.replay_monitor.replay_id == None:
                            print(
                                f'WARNING: No replay was found for the match \'{match_participants[0]} vs {match_participants[1]}\'. Is Bakkesmod injected and \'Automatically save all replays\' enabled?'
                            )

                        # Save result in file
                        result = exercise_result.exercise.grader.match_result
                        result.write(result_path)
                        print(
                            f'Match finished {result.blue_goals}-{result.orange_goals}. Saved result as {result_path}'
                        )

                        rr_results.append(result)

                        # Let the winner celebrate and the scoreboard show for a few seconds.
                        # This sleep not required.
                        time.sleep(8)

        print(f'{Ladder.DIVISION_NAMES[div_index]} division done')
        event_results.append(rr_results)

        # Find bots' overall score for the round robin
        overall_scores = [
            CombinedScore.calc_score(bot, rr_results) for bot in rr_bots
        ]
        sorted_overall_scores = sorted(overall_scores)[::-1]
        print(
            f'Bots\' overall performance in {Ladder.DIVISION_NAMES[div_index]} division:'
        )
        for score in sorted_overall_scores:
            print(
                f'> {score.bot}: goal_diff={score.goal_diff}, goals={score.goals}, shots={score.shots}, saves={score.saves}, points={score.points}'
            )

        # Rearrange bots in division on the new ladder
        first_bot_index = new_ladder.division_size * div_index
        bots_to_rearrange = len(rr_bots)
        for i in range(bots_to_rearrange):
            new_ladder.bots[first_bot_index + i] = sorted_overall_scores[i].bot

    # Save new ladder
    Ladder.write(new_ladder, working_dir.new_ladder)
    print(f'Done. Saved new ladder as {working_dir.new_ladder.name}')

    # Remove overlay interface file now that we are done
    if working_dir.overlay_interface.exists():
        working_dir.overlay_interface.unlink()

    return new_ladder
def run_league_play(working_dir: WorkingDir,
                    run_strategy: RunStrategy,
                    replay_preference: ReplayPreference,
                    team_size: int,
                    shutdowntime: int,
                    stale_rematch_threshold: int = 0,
                    half_robin: bool = False):
    """
    Run a league play event by running round robins for half the divisions. When done, a new ladder file is created.

    :param stale_rematch_threshold: If a bot has won this number of matches in a row against a particular opponent
    and neither have had their code updated, we will consider it to be a stale rematch and skip future matches.
    If 0 is passed, we will not skip anything.
    :param half_robin: If true, we will split the division into an upper and lower round-robin, which reduces the
    number of matches required.
    """

    bots = load_all_bots_versioned(working_dir)
    ladder = Ladder.read(working_dir.ladder)

    latest_bots = [
        bot for bot in bots.values() if bot.bot_config.name in ladder.bots
    ]
    latest_bots.sort(key=lambda b: b.updated_date, reverse=True)
    print('Most recently updated bots:')
    for bot in latest_bots:
        print(f'{bot.updated_date.isoformat()} {bot.bot_config.name}')

    # We need the result of every match to create the next ladder. For each match in each round robin, if a result
    # exist already, it will be parsed, if it doesn't exist, it will be played.
    # When all results have been found, the new ladder can be completed and saved.
    new_ladder = Ladder(ladder.bots)
    event_results = []

    # playing_division_indices contains either even or odd indices.
    # If there is only one division always play that division (division 0, quantum).
    playing_division_indices = ladder.playing_division_indices(run_strategy)

    # The divisions play in reverse order, so quantum/overclocked division plays last
    for div_index in playing_division_indices[::-1]:
        print(
            f'Starting round robin for the {Ladder.DIVISION_NAMES[div_index]} division'
        )

        round_robin_ranges = get_round_robin_ranges(ladder, div_index,
                                                    half_robin)

        for start_index, end_index in round_robin_ranges:
            rr_bots = ladder.bots[start_index:end_index + 1]
            rr_matches = generate_round_robin_matches(rr_bots)
            rr_results = []

            for match_participants in rr_matches:

                # Check if match has already been played during THIS session. Maybe something crashed and we had to
                # restart autoleague, but we want to pick up where we left off.
                session_result_path = working_dir.get_match_result(
                    div_index, match_participants[0], match_participants[1])
                participant_1 = bots[match_participants[0]]
                participant_2 = bots[match_participants[1]]

                if session_result_path.exists():
                    print(f'Found existing result {session_result_path.name}')
                    rr_results.append(MatchResult.read(session_result_path))
                else:
                    historical_result = get_stale_match_result(
                        participant_1, participant_2, stale_rematch_threshold,
                        working_dir, True)
                    if historical_result is not None:
                        rr_results.append(historical_result)
                        # Don't write to result files at all, since this match didn't actually occur.
                        overlay_data = OverlayData(div_index, participant_1,
                                                   participant_2, new_ladder,
                                                   bots, historical_result,
                                                   rr_bots, rr_results)
                        overlay_data.write(working_dir.overlay_interface)
                        time.sleep(
                            8
                        )  # Show the overlay for a while. Not needed for any other reason.

                    else:
                        # Let overlay know which match we are about to start
                        overlay_data = OverlayData(div_index, participant_1,
                                                   participant_2, new_ladder,
                                                   bots, None, rr_bots,
                                                   rr_results)

                        overlay_data.write(working_dir.overlay_interface)

                        match_config = make_match_config(
                            participant_1.bot_config, participant_2.bot_config,
                            team_size)
                        result = run_match(participant_1.bot_config.name,
                                           participant_2.bot_config.name,
                                           match_config, replay_preference)
                        result.write(session_result_path)
                        versioned_result_path = working_dir.get_version_specific_match_result(
                            participant_1, participant_2)
                        result.write(versioned_result_path)
                        print(
                            f'Match finished {result.blue_goals}-{result.orange_goals}. Saved result as '
                            f'{session_result_path} and also {versioned_result_path}'
                        )

                        rr_results.append(result)

                        # Let the winner celebrate and the scoreboard show for a few seconds.
                        # This sleep not required.
                        time.sleep(8)

            # Find bots' overall score for the round robin
            overall_scores = [
                CombinedScore.calc_score(bot, rr_results) for bot in rr_bots
            ]
            sorted_overall_scores = sorted(overall_scores)[::-1]
            division_result_message = f'Bots\' overall round-robin performance ({Ladder.DIVISION_NAMES[div_index]} division):\n'
            for score in sorted_overall_scores:
                division_result_message += f'> {score.bot:<32}: wins={score.wins:>2}, goal_diff={score.goal_diff:>3}\n'

            print(division_result_message)
            overlay_data = OverlayData(div_index, None, None, new_ladder, bots,
                                       None, rr_bots, rr_results,
                                       division_result_message)
            overlay_data.write(working_dir.overlay_interface)

            # Rearrange bots in division on the new ladder
            first_bot_index = start_index
            bots_to_rearrange = len(rr_bots)
            for i in range(bots_to_rearrange):
                new_ladder.bots[first_bot_index +
                                i] = sorted_overall_scores[i].bot

            event_results.append(rr_results)

            time.sleep(8)  # Show the division overlay for a while.

        print(f'{Ladder.DIVISION_NAMES[div_index]} division done')

    # Save new ladder
    Ladder.write(new_ladder, working_dir.new_ladder)
    print(f'Done. Saved new ladder as {working_dir.new_ladder.name}')
    if shutdowntime != 0:
        import subprocess
        subprocess.call("shutdown.exe -s -t " + str(shutdowntime))

    # Remove overlay interface file now that we are done
    if working_dir.overlay_interface.exists():
        working_dir.overlay_interface.unlink()

    return new_ladder
def list_results(working_dir: WorkingDir, run_strategy: RunStrategy,
                 half_robin: bool):
    ladder = Ladder.read(working_dir.ladder)
    playing_division_indices = ladder.playing_division_indices(run_strategy)

    if len(ladder.bots) < 2:
        print(f'Not enough bots on the ladder to play any matches')
        return

    # Write overview to file first, then print content of the file
    with open(working_dir.results_overview, 'w') as f:
        f.write(f'Matches:\n')

        if run_strategy == RunStrategy.ROLLING or half_robin:
            # The ladder was dynamic, so we can't print divisions neatly.
            # Just print everything in one blob.
            match_results = [
                MatchResult.read(path)
                for path in working_dir.match_results.glob('*')
            ]
            for result in match_results:
                result_str = f'  (result: {result.blue_goals}-{result.orange_goals})'
                f.write(f'{result.blue} vs {result.orange}{result_str}\n')

            write_overall_scores(f, ladder.bots, match_results)

        else:
            # The divisions play in reverse order, but we don't print them that way.
            for div_index in playing_division_indices:
                f.write(
                    f'--- {Ladder.DIVISION_NAMES[div_index]} division ---\n')

                rr_bots = ladder.round_robin_participants(div_index)
                rr_matches = generate_round_robin_matches(rr_bots)

                for match_participants in rr_matches:
                    result_str = ''
                    result_path = working_dir.get_match_result(
                        div_index, match_participants[0],
                        match_participants[1])
                    if result_path.exists():
                        result = MatchResult.read(result_path)
                        result_str = f'  (result: {result.blue_goals}-{result.orange_goals})'
                    f.write(
                        f'{match_participants[0]} vs {match_participants[1]}{result_str}\n'
                    )

            f.write('\n')

            # Print a table with all the combined scores
            for div_index in playing_division_indices:
                rr_bots = ladder.round_robin_participants(div_index)
                rr_matches = generate_round_robin_matches(rr_bots)
                rr_results = []
                for match_participants in rr_matches:
                    result_path = working_dir.get_match_result(
                        div_index, match_participants[0],
                        match_participants[1])
                    if result_path.exists():
                        rr_results.append(MatchResult.read(result_path))

                write_overall_scores(f, rr_bots, rr_results, div_index)

            f.write(
                f'--------------------------------+------+----------+-------+-------+-------+-------+\n'
            )

    # Results have been writen to the file, now display the content
    with open(working_dir.results_overview, 'r') as f:
        print(f.read())

    print(f'Result overview was written to \'{working_dir.results_overview}\'')
 def __init__(self, match_files: List[Path]):
     self.match_files = match_files
     self.match_files.sort(reverse=True)
     self.results = [MatchResult.read(path) for path in self.match_files]