示例#1
0
    def test_condition_parameters(self):
        buffable = Buffable()
        buff = BuffSpec()
        buff.activation_triggers = ["DamageEvent"]
        buff.deactivation_triggers = ["DamageEvent"]
        buff.conditions = ["is_damage_higher_then 10"
                           ]  # condition parameters after condition name
        buff.buff_id = 5
        buff.modifiers = [Modifier("+", 30, Attributes.DEF)]
        buffspecs.register_buff(buff)

        @buffspecs.AddConditionFor([DamageEvent])
        def is_damage_higher_then(event, param):
            return event.damage > param

        # Add the buff
        add_buff(buffable, buff, CompleteBuildingEvent())

        # Damage lower then condition threshhold
        call_event(DamageEvent(buffable, 8))

        # Buff should not have been activated
        assert buff.buff_id not in buffable.active_buffs
        assert buffable.attributes[Attributes.DEF] == 0

        # Now a damage higher
        call_event(DamageEvent(buffable, 12))
        # Buff should have been activated
        assert buff.buff_id in buffable.active_buffs
        assert buffable.attributes[Attributes.DEF] == 30
示例#2
0
    def test_condition_switching_buff(self):
        buffable = Buffable()
        buff = BuffSpec()
        buff.activation_triggers = ["DamageEvent"]
        buff.deactivation_triggers = ["DamageEvent"]
        buff.conditions = ["is_burning"]
        buff.buff_id = 5
        buff.modifiers = [Modifier("+", 30, Attributes.DEF)]
        buffspecs.register_buff(buff)

        buffable.attributes[
            "Burning"] = 0  # example to set a state, not burning

        @buffspecs.AddConditionFor([DamageEvent])
        def is_burning(event):
            return event.buffable.attributes["Burning"] == 1

        # Add the buff
        add_buff(buffable, buff, CompleteBuildingEvent())

        # The buff should not be applied because the buffable is not burning
        assert buff.buff_id not in buffable.active_buffs
        assert buffable.attributes[Attributes.DEF] == 0

        # Now make it burn
        damage = 10
        buffable.attributes["Burning"] = 1
        call_event(DamageEvent(buffable, damage))

        # Now the buff should have applied
        assert buff.buff_id in buffable.active_buffs
        assert buffable.attributes[Attributes.DEF] == 30

        # And we should have added the remove trigger
        assert buff.buff_id in buffable.deactivation_triggers["DamageEvent"]
        # Buff history contains this modification
        assert len(buffable.attributes.get_data(Attributes.DEF).history) == 1

        # Now After calling the other event the buff should be removed
        buffable.attributes["Burning"] = 0
        call_event(DamageEvent(buffable, damage))

        # Now the buff should be removed
        assert buff.buff_id not in buffable.active_buffs
        assert buff.buff_id not in buffable.deactivation_triggers
        assert buffable.attributes[Attributes.DEF] == 0

        # And the trigger should be added again and the remove trigger should be removed
        assert buff.buff_id not in buffable.deactivation_triggers[
            "DamageEvent"]
        assert buff.buff_id in buffable.activation_triggers["DamageEvent"]
        # Also the modification history is removed because this buff is inactive and not modifyng anything
        assert len(buffable.attributes.get_data(Attributes.DEF).history) == 0

        # In case we burn again...
        buffable.attributes["Burning"] = 1
        call_event(DamageEvent(buffable, damage))

        # Buff is activated again
        assert buff.buff_id in buffable.active_buffs
        assert buffable.attributes[Attributes.DEF] == 30