<commit_before>"""
Implements the turn structure mechanics (phases and steps).

.. seealso::
    The :ref:`turn-structure` section of the :ref:`comprehensive-rules`.

"""

from collections import namedtuple

from cardboard.events import events


phase_events = events["game"]["turn"]["phase"]


def untap(game):
    """
    Perform the :ref:`untap-step`.

    """

    game.events.trigger(event=phase_events["beginning"]["untap"]["started"])

    for permanent in game.turn.active_player.battlefield:
        if permanent.is_tapped:
            permanent.untap()

    game.events.trigger(event=phase_events["beginning"]["untap"]["ended"])


def upkeep(game):
    """
    Perform the :ref:`upkeep-step`.

    """

    game.events.trigger(event=phase_events["beginning"]["upkeep"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["beginning"]["upkeep"]["ended"])


def draw(game):
    """
    Perform the :ref:`draw-step`.

    """

    game.turn.active_player.draw()
    game.events.trigger(event=phase_events["beginning"]["draw"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["beginning"]["draw"]["ended"])


def _main(game):
    """
    Perform the :ref:`main-phase`.

    """

    game.grant_priority()


def first_main(game):
    game.events.trigger(event=phase_events["first_main"]["started"])
    _main(game)
    game.events.trigger(event=phase_events["first_main"]["ended"])


def beginning_of_combat(game):
    """
    Perform the :ref:`beginning-combat-step`.

    """

    game.events.trigger(event=phase_events["combat"]["beginning"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["combat"]["beginning"]["ended"])


def declare_attackers(game):
    """
    Perform the :ref:`declare-attackers-step`.

    """

    game.events.trigger(
        event=phase_events["combat"]["declare_attackers"]["started"]
    )

    possible_attackers = {c for c in game.battlefield if c.can_attack}

    targets = game.turn.active_player.opponents

    for opponent in targets:
        targets |= {card for card in opponent.battlefield
                    if card.type == "Planeswalker"}

    attackers = game.turn.active_player.frontend.select_cards(game.battlefield,
                                                              how_many=None)
    attacks = {k : game.turn.active_player.frontend.select_combined()
               for k in attackers}

    for card in attacks:
        card.tap()

    game.grant_priority()
    game.events.trigger(
        event=phase_events["combat"]["declare_attackers"]["ended"]
    )


def declare_blockers(game):
    """
    Perform the :ref:`declare-blockers-step`.

    """

    game.events.trigger(
        event=phase_events["combat"]["declare_blockers"]["started"]
    )

    game.events.trigger(
        event=phase_events["combat"]["declare_blockers"]["ended"]
    )


def combat_damage(game):
    """<commit_msg>Temporarily commenting out some lines that aren't tested / well written yet.<commit_after>"""
Implements the turn structure mechanics (phases and steps).

.. seealso::
    The :ref:`turn-structure` section of the :ref:`comprehensive-rules`.

"""

from collections import namedtuple

from cardboard.events import events


phase_events = events["game"]["turn"]["phase"]


def untap(game):
    """
    Perform the :ref:`untap-step`.

    """

    game.events.trigger(event=phase_events["beginning"]["untap"]["started"])

    for permanent in game.turn.active_player.battlefield:
        if permanent.is_tapped:
            permanent.untap()

    game.events.trigger(event=phase_events["beginning"]["untap"]["ended"])


def upkeep(game):
    """
    Perform the :ref:`upkeep-step`.

    """

    game.events.trigger(event=phase_events["beginning"]["upkeep"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["beginning"]["upkeep"]["ended"])


def draw(game):
    """
    Perform the :ref:`draw-step`.

    """

    game.turn.active_player.draw()
    game.events.trigger(event=phase_events["beginning"]["draw"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["beginning"]["draw"]["ended"])


def _main(game):
    """
    Perform the :ref:`main-phase`.

    """

    game.grant_priority()


def first_main(game):
    game.events.trigger(event=phase_events["first_main"]["started"])
    _main(game)
    game.events.trigger(event=phase_events["first_main"]["ended"])


def beginning_of_combat(game):
    """
    Perform the :ref:`beginning-combat-step`.

    """

    game.events.trigger(event=phase_events["combat"]["beginning"]["started"])
    game.grant_priority()
    game.events.trigger(event=phase_events["combat"]["beginning"]["ended"])


def declare_attackers(game):
    """
    Perform the :ref:`declare-attackers-step`.

    """

    game.events.trigger(
        event=phase_events["combat"]["declare_attackers"]["started"]
    )

    possible_attackers = {c for c in game.battlefield if c.can_attack}

    targets = game.turn.active_player.opponents

    for opponent in targets:
        targets |= {card for card in opponent.battlefield
                    if card.type == "Planeswalker"}

    # attackers = game.turn.active_player.frontend.select.cards(
    #     game.battlefield, how_many=None
    # )
    # attacks = {
    #    k : game.turn.active_player.frontend.select.combined(attackers)
    #    for k in attackers
    # }

    # for card in attacks:
    #     card.tap()

    game.grant_priority()
    game.events.trigger(
        event=phase_events["combat"]["declare_attackers"]["ended"]
    )


def declare_blockers(game):
    """
    Perform the :ref:`declare-blockers-step`.

    """

    game.events.trigger(
        event=phase_events["combat"]["declare_blockers"]["started"]
    )

    game.events.trigger(
        event=phase_events["combat"]["declare_blockers"]["ended"]
    )


def combat_damage(game):
    """
{'fraction_non_alphanumeric': 0.15795664594185851, 'fraction_numerical': 0.0, 'mean_word_length': 3.4550898203592815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6569327', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1903, 'n_words': 281}