def test_inactivating_propagation_target(self):
        player = Player()
        player.attributes[Attributes.HP] = 10

        castle = Castle()
        castle.players.append(player)

        # A global castle buff of +50 ATK
        castle_buff = BuffBuilder().modify("+", 50, Attributes.ATK).propagates_to(Player)\
         .whenever(FartEvent).just_if("is_healthy").build()

        @buffspecs.AddConditionFor([BuffEvent])
        def is_healthy(event):
            return event.buffable.attributes[Attributes.HP] > 0

        add_buff(castle, castle_buff, CompleteBuildingEvent())

        # Call the event triggering the buff
        call_event(FartEvent(player))

        assert player.attributes[Attributes.ATK] == 50

        inactivate_buff(player, castle_buff, None)

        assert player.attributes[Attributes.ATK] == 0
        assert castle_buff.buff_id not in player.active_buffs

        # Activation trigger should be registered because buff had a condition that can change
        assert len(player.activation_triggers["FartEvent"]) == 1
    def test_inactivating_propagator(self):
        player = Player()

        castle = Castle()
        castle.players.append(player)

        # A global castle buff of +50 ATK
        castle_buff = BuffBuilder().modify(
            "+", 50, Attributes.ATK).propagates_to(Player).build()
        add_buff(castle, castle_buff, CompleteBuildingEvent())

        assert player.attributes[Attributes.ATK] == 50

        inactivate_buff(castle, castle_buff, None)

        assert player.attributes[Attributes.ATK] == 0
	def test_inactivating_propagated_buff_stack_from_source(self):
		player = Player()

		castle = Castle()
		castle.players.append(player)

		# A global castle buff of +50 ATK
		castle_buff = BuffBuilder().modify("+", 50, Attributes.ATK).propagates_to(Player).stacks(3).build()

		# Add thre stacks of the buff
		add_buff(castle, castle_buff, CompleteBuildingEvent())
		add_buff(castle, castle_buff, CompleteBuildingEvent())
		add_buff(castle, castle_buff, CompleteBuildingEvent())
		assert player.attributes[Attributes.ATK] == 150

		# Stacks are added both to source as well to propagation target
		assert player.active_buffs[castle_buff.buff_id].stack == 3
		assert castle.active_buffs[castle_buff.buff_id].stack == 3

		inactivate_buff(castle, castle_buff, None)

		assert player.attributes[Attributes.ATK] == 100
		assert castle.active_buffs[castle_buff.buff_id].stack == 2
		assert player.active_buffs[castle_buff.buff_id].stack == 2
Exemple #4
0
def call_event(event):
    """ Calls an event and try to trigger any remaining triggers on the event buffable.

    :param BuffEvent event:
    :rtype EventResult
    :returns An event result with all added, removed and propagated modifications.
    """
    buffable = event.buffable
    result = EventResult()

    # Activation Triggers
    for triggered_buff_spec in get_buff_specs_triggered_by_event(
            event, buffable.activation_triggers):
        result.added_modifications = activate_buff(buffable,
                                                   triggered_buff_spec, event)

    # Deactivation Triggers
    for triggered_buff_spec in get_buff_specs_triggered_by_event(
            event, buffable.deactivation_triggers, condition_inverse=True):
        result.removed_modifications = inactivate_buff(buffable,
                                                       triggered_buff_spec,
                                                       event)

    # Propagation Triggers
    for triggered_buff_spec in get_buff_specs_triggered_by_event(
            event, buffable.propagation_triggers, propagation=True):
        for propagation_event in get_buff_propagation_events(
                buffable, triggered_buff_spec, event):
            buff_spec = buffspecs.get_buff_spec(propagation_event.buff_id)
            result.propagated_modifications[
                propagation_event.buffable.id].append(
                    add_buff(propagation_event.buffable,
                             buff_spec,
                             propagation_event,
                             propagated=True))
            # In case this buff spec has no propagation triggers and was just propagated by "AddBuffEvent"
            # means this buff wont be able to re-propagate ever again.
            if not buff_spec.propagation_triggers:
                delete_triggers(propagation_event.buff_id, ["AddBuffEvent"],
                                buffable.propagation_triggers)

    return result