def test_changing_source_attribute_affects_derivated_attribute(self): buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 # Simple buff that 50% of your attack goes to your def buff = BuffBuilder().modify("%", 0.5, Attributes.ATK).to_attribute( Attributes.DEF).build() add_buff(buffable, buff, CompleteBuildingEvent()) # Player should have got 50% of his atk (50) to def assert buffable.attributes[Attributes.DEF] == 50 assert buffable.attributes[Attributes.ATK] == 100 # Now we add +100 attack to the player, his 50% buff instead of giving 50 def should give 100 def buff_2 = BuffBuilder().modify("+", 100, Attributes.ATK).build() add_buff(buffable, buff_2, CompleteBuildingEvent()) # Player defense should be updated as his attack increased assert buffable.attributes[Attributes.DEF] == 100 assert buffable.attributes[Attributes.ATK] == 200 # Now removing the buff should re-calculate the derivation as well remove_buff(buffable, buff_2.buff_id) # Player should have got 50% of his atk (50) to def as it should be derivating from 100 atk again assert buffable.attributes[Attributes.DEF] == 50 assert buffable.attributes[Attributes.ATK] == 100
def test_derivating_the_propagated_value(self): player = Player() player.attributes[Attributes.ATK] = 200 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player # 50% of equipment attack goes to player DEF equipment_buff_2 = BuffBuilder().modify("%", 0.5, Attributes.ATK)\ .propagates_to_attribute(Attributes.DEF).propagates_to(Player).build() add_buff(equipment, equipment_buff_2, CompleteBuildingEvent()) # Atk should not be modified assert player.attributes[Attributes.ATK] == 200 # 50% of the EQUIPMENT ATK (100) should have gone to player DEF assert player.attributes[ Attributes.DEF] == equipment.attributes[Attributes.ATK] * 0.5 # Equipment should not be affected assert equipment.attributes[Attributes.ATK] == 100 assert equipment.attributes[Attributes.DEF] == 0 # Derivation should be stored in the target, not the propagator assert len(equipment.attributes.get_data( Attributes.ATK).derivations) == 0 assert len(player.attributes.get_data(Attributes.ATK).derivations) == 1
def test_propagation_with_triggers(self): player = Player() player.attributes[Attributes.ATK] = 100 # A global castle buff of 50% ATK castle_buff = BuffBuilder().modify("%", 0.5, Attributes.ATK)\ .propagates_when(RecruitPlayerEvent).propagates_to(Player).build() castle = Castle() castle.players.append(player) add_buff(castle, castle_buff, CompleteBuildingEvent()) # Castle should have a propagation trigger not a activation trigger assert castle_buff.buff_id not in castle.activation_triggers[ "RecruitPlayerEvent"] assert castle_buff.buff_id in castle.propagation_triggers[ "RecruitPlayerEvent"] # Event was not triggered, so player should not get modified assert player.attributes[Attributes.ATK] == 100 # Now we trigger the event and expect the propagation to happen call_event(RecruitPlayerEvent(castle)) # Now the buff should have been applied assert player.attributes[Attributes.ATK] == 150 assert castle_buff.buff_id in player.active_buffs assert castle_buff.buff_id in castle.active_buffs # The propagation trigger should not consumed assert castle_buff.buff_id in castle.propagation_triggers[ "RecruitPlayerEvent"]
def test_basic_buff_dependency(self): buffable = Buffable() buff_id_1 = 1 buff_id_2 = 2 buff_1 = BuffBuilder().modify( "+", 50, Attributes.ATK).just_if("has_buff 2").build() buff_2 = BuffBuilder().modify("+", 50, Attributes.ATK).build() # A condition that can be triggered by a MockEvent @buffspecs.AddConditionFor([BuffEvent]) def has_buff(event, buff_id): return buff_id in event.buffable.active_buffs add_buff(buffable, buff_1, CompleteBuildingEvent()) # Player should not be modified because he did not have buff 2 assert buffable.attributes[Attributes.ATK] == 0 # Adding the second buff should trigger the first one because his condidion matched add_buff(buffable, buff_2, CompleteBuildingEvent()) # Both buffs should be applied now assert buffable.attributes[Attributes.ATK] == 100 assert buff_id_1 in buffable.active_buffs assert buff_id_2 in buffable.active_buffs
def test_increasing_propagation_source_attribute_repropagates_derivation( self): player = Player() player.attributes[Attributes.ATK] = 200 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player # 50% of equipment attack goes to player DEF add_buff(equipment, BuffBuilder().modify("%", 0.5, Attributes.ATK)\ .propagates_to_attribute(Attributes.DEF).propagates_to(Player).build(), CompleteBuildingEvent() ) # 50% of the EQUIPMENT ATK (100) should have gone to player DEF assert equipment.attributes[Attributes.ATK] == 100 assert player.attributes[ Attributes.DEF] == equipment.attributes[Attributes.ATK] * 0.5 # +100 to Equipment ATTACK, it should propagate 50% of it to player DEF add_buff(equipment, BuffBuilder().modify("+", 100, Attributes.ATK).build(), CompleteBuildingEvent()) assert equipment.attributes[Attributes.ATK] == 200 assert player.attributes[ Attributes.DEF] == equipment.attributes[Attributes.ATK] * 0.5
def test_buff_cancelling_when_other_buff_triggering(self): buffable = Buffable() buff_id_1 = 1 buff_id_2 = 2 buff_1 = BuffBuilder(buff_id_1).modify("+", 50, Attributes.ATK).whenever(DamageEvent)\ .just_if("not has_buff 2").build() buff_2 = BuffBuilder(buff_id_2).modify( "+", 75, Attributes.ATK).whenever(DamageEvent).build() # A condition that can be triggered by a MockEvent @buffspecs.AddConditionFor([BuffEvent]) def has_buff(event, buff_id): return buff_id in event.buffable.active_buffs add_buff(buffable, buff_1, CompleteBuildingEvent()) call_event(DamageEvent(buffable)) # Player does not have buff 2 so he should be getting buff 1 assert buffable.attributes[Attributes.ATK] == 50 assert buff_id_1 in buffable.active_buffs add_buff(buffable, buff_2, CompleteBuildingEvent()) call_event(DamageEvent(buffable)) # Now that he got buff 2 applied, buff 1 should be removed and only buff 2 is applied assert buffable.attributes[Attributes.ATK] == 75 assert buff_id_1 not in buffable.active_buffs assert buff_id_2 in buffable.active_buffs
def test_derivation_debug_history(self): buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 buff = BuffBuilder().modify("%", 0.5, Attributes.ATK).to_attribute( Attributes.DEF).build() add_buff(buffable, buff, CompleteBuildingEvent()) atk_modification_history = list( buffable.attributes.get_data(Attributes.DEF).history.values()) last_modification = atk_modification_history[0] # The original modifier should be in history as well assert last_modification.modifier.value == 0.5 assert last_modification.modifier.attribute_id == Attributes.ATK assert last_modification.modifier.operator == "%" # It should have created an modifier of 50% of user atk, since atk was 100, thats +50 DEF assert last_modification.derivated_modifier.value == 50 assert last_modification.derivated_modifier.attribute_id == Attributes.DEF assert last_modification.derivated_modifier.operator == "+" assert last_modification.buff_id == buff.buff_id event_chain = last_modification.source_event.get_event_chain() assert isinstance(event_chain[0], AddBuffEvent) assert isinstance(event_chain[1], CompleteBuildingEvent)
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_propagating_a_derivation_buff(self): # Simple derivation buff buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 buff = BuffBuilder().modify("%", 0.5, Attributes.ATK).to_attribute( Attributes.DEF).build() # We want to track the performance of this with TrackStack() as track: add_buff(buffable, buff, CompleteBuildingEvent())
def test_propagation_with_stacks(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 the first stack add_buff(castle, castle_buff, CompleteBuildingEvent()) assert player.attributes[Attributes.ATK] == 50 assert player.active_buffs[castle_buff.buff_id].stack == 1 assert castle.active_buffs[castle_buff.buff_id].stack == 1 # Add a second stack add_buff(castle, castle_buff, CompleteBuildingEvent()) assert player.active_buffs[castle_buff.buff_id].stack == 2 assert castle.active_buffs[castle_buff.buff_id].stack == 2 assert player.attributes[Attributes.ATK] == 100 # Add a the third stack add_buff(castle, castle_buff, CompleteBuildingEvent()) assert player.attributes[Attributes.ATK] == 150 # An extra stack should not propagate the buff anymore add_buff(castle, castle_buff, CompleteBuildingEvent()) assert player.attributes[Attributes.ATK] == 150
def test_removing_propagation(self): player = Player() player.attributes[Attributes.ATK] = 100 castle_buff = BuffBuilder().modify( "%", 0.5, Attributes.ATK).propagates_to(Player).build() castle = Castle() castle.players.append(player) add_buff(castle, castle_buff, CompleteBuildingEvent()) assert player.attributes[Attributes.ATK] == 150 remove_buff(castle, castle_buff.buff_id) assert player.attributes[Attributes.ATK] == 100
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_simple_derivation(self): buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 # Simple buff that 50% of your attack goes to your def buff = BuffBuilder().modify("%", 0.5, Attributes.ATK).to_attribute( Attributes.DEF).build() add_buff(buffable, buff, CompleteBuildingEvent()) # Player should have got 50% of his atk (50) to def assert buffable.attributes[Attributes.DEF] == 50 assert buffable.attributes[Attributes.ATK] == 100 assert buff.buff_id in buffable.active_buffs # Checking the derivation has been registered assert list( buffable.attributes.get_data( Attributes.ATK).derivations.keys())[0] == Attributes.DEF
def test_propating_two_targets(self): player = Player() player.attributes[Attributes.ATK] = 100 castle = Castle() player.castle = castle castle.players.append(player) # A global castle buff of bonus coins collected that can affect both players and castles castle_buff = BuffBuilder().modify( "+", 10, Attributes.BONUS_COINS_COLLECTED).propagates_to(Player, Castle).build() add_buff(castle, castle_buff, CompleteBuildingEvent()) # Both players and castles shall have bonus coins collected assert player.attributes[Attributes.BONUS_COINS_COLLECTED] == 10 assert castle.attributes[Attributes.BONUS_COINS_COLLECTED] == 10
def test_propagation_wont_duplicate_buffs(self): player = Player() player.attributes[Attributes.ATK] = 100 castle_buff = BuffBuilder().modify("%", 0.5, Attributes.ATK) \ .propagates_when(RecruitPlayerEvent).propagates_to(Player).build() castle = Castle() castle.players.append(player) add_buff(castle, castle_buff, CompleteBuildingEvent()) # Trigger the event twice call_event(RecruitPlayerEvent(castle)) call_event(RecruitPlayerEvent(castle)) # The propagation should have happened only once assert len(player.active_buffs) == 1 assert player.attributes[Attributes.ATK] == 150
def test_removing_propagation_source(self): player = Player() player.attributes[Attributes.ATK] = 200 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player # 50% of equipment attack goes to player DEF equipment_buff_2 = BuffBuilder().modify("%", 0.5, Attributes.ATK) \ .propagates_to_attribute(Attributes.DEF).propagates_to(Player).build() add_buff(equipment, equipment_buff_2, CompleteBuildingEvent()) # 50% of the EQUIPMENT ATK (100) should have gone to player DEF assert player.attributes[ Attributes.DEF] == equipment.attributes[Attributes.ATK] * 0.5 # If we remove the propagation source, the propagation targets attributes needs to be updated remove_buff(equipment, equipment_buff_2.buff_id) assert player.attributes[Attributes.DEF] == 0
def test_propagation_also_modifyng_source(self): player = Player() player.attributes[Attributes.ATK] = 100 castle = Castle() castle.players.append(player) # A global castle buff of +50 ATK but also affects the castle castle_buff = BuffBuilder().modify("+", 50, Attributes.ATK)\ .propagates_to(Player, Castle).build() add_buff(castle, castle_buff, CompleteBuildingEvent()) # The buff should be active in the owner and target assert castle_buff.buff_id in castle.active_buffs assert castle_buff.buff_id in player.active_buffs # Since castle buffs are propagated to the players in that castle, the player should have got +50% ATK assert player.attributes[Attributes.ATK] == 150 # However even tho the castle has this buff as active, since its not a target it did not change ATK assert castle.attributes[Attributes.ATK] == 50
def test_buff_stack_with_expiry_all_at_once(self): buffable = Buffable() buff = BuffSpec() buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.max_stack = 3 # 3 stacks max# buff.duration_seconds = 10 buff.modifiers = [Modifier("+", 10, Attributes.DEF)] with FixedTime(get_timestamp()): expiry_time = get_timestamp() + buff.duration_seconds # Add the buff for full 3 stacks add_buff(buffable, buff, CompleteBuildingEvent()) add_buff(buffable, buff, CompleteBuildingEvent()) add_buff(buffable, buff, CompleteBuildingEvent()) assert buffable.attributes[Attributes.DEF] == 30 assert buffable.active_buffs[buff.buff_id].stack == 3 assert len(buffable.expiry_times) == 3 with FixedTime(expiry_time): assert buffable.attributes[Attributes.DEF] == 0 assert len(buffable.expiry_times) == 0 assert buff.buff_id not in buffable.active_buffs
def test_next_to_expire(self): buffable = Buffable() buff = BuffSpec() buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.duration_seconds = 10 # Only lasts for 10 seconds buff.max_stack = 3 buff.modifiers = [Modifier("+", 50, Attributes.DEF)] with FixedTime(get_timestamp()): expiry_time = get_timestamp() + buff.duration_seconds # Add the buff 3 stacks add_buff(buffable, buff, CompleteBuildingEvent()) add_buff(buffable, buff, CompleteBuildingEvent()) add_buff(buffable, buff, CompleteBuildingEvent()) with FixedTime(expiry_time): expired_buffs = list(get_expired_buffs(buffable)) assert len(expired_buffs) == 3 # Calling expired buffs should have removed them from expiry list assert len(buffable.expiry_times) == 0
def test_propagating_a_derivation_buff(self): player = Player() player.attributes[Attributes.ATK] = 50 player.attributes[Attributes.DEF] = 75 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player castle = Castle() castle.players = [player] # 50% of player def becomes player HP add_buff(equipment, BuffBuilder(1).modify("%", 0.5, Attributes.DEF).to_attribute(Attributes.HP) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert player.attributes[Attributes.DEF] == 75 assert player.attributes[Attributes.HP] == 75 / 2 # 50% of equipment attack goes to player DEF add_buff(equipment, BuffBuilder(2).modify("%", 0.5, Attributes.ATK).propagates_to_attribute(Attributes.DEF) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert player.attributes[Attributes.DEF] == 125 assert player.attributes[Attributes.HP] == 125 / 2 # Remove the player DEF -> HP derivation remove_buff(equipment, 1) foka = (125 + 40 + 40 + 50) / 2 asd = player.attributes[Attributes.HP] assert player.attributes[Attributes.DEF] == 125 assert player.attributes[Attributes.HP] == 0
def test_double_propagation(self): player = Player() player.attributes[Attributes.ATK] = 100 castle = Castle() castle.players.append(player) equipment = Equipment() equipment.owner = player # A global castle buff of 50% ATK castle_buff = BuffBuilder().modify( "%", 0.5, Attributes.ATK).propagates_to(Player).build() # Equipment buff of 100% bonus atk equipment_buff = BuffBuilder().modify( "%", 1, Attributes.ATK).propagates_to(Player).build() add_buff(castle, castle_buff, CompleteBuildingEvent()) add_buff(equipment, equipment_buff, CompleteBuildingEvent()) assert len(player.active_buffs) == 2 # flat bonus = 100, 250% total bonus from propagations, so 250 final value assert player.attributes[Attributes.ATK] == 250
def test_removing_derivation_buff_updates_derivated_attributes(self): buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 buffable.attributes[Attributes.DEF] = 100 # Simple buff that 50% of your attack goes to your def buff = BuffBuilder().modify("%", 0.5, Attributes.ATK).to_attribute( Attributes.DEF).build() add_buff(buffable, buff, CompleteBuildingEvent()) # 50% of your def, goes to your HP buff_2 = BuffBuilder().modify("%", 0.5, Attributes.DEF).to_attribute( Attributes.HP).build() add_buff(buffable, buff_2, CompleteBuildingEvent()) # Player defense should be updated as his attack increased assert buffable.attributes[Attributes.DEF] == 150 assert buffable.attributes[Attributes.HP] == 75 remove_buff(buffable, buff.buff_id) assert buffable.attributes[Attributes.DEF] == 100 assert buffable.attributes[Attributes.HP] == 50
def test_chaining_propagation(self): player = Player() player.attributes[Attributes.ATK] = 100 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player # 50% of player DEF becomes player HP add_buff(equipment, BuffBuilder().modify("%", 0.5, Attributes.DEF).to_attribute(Attributes.HP) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) # 50% of equipment attack goes to player DEF add_buff(equipment, BuffBuilder().modify("%", 0.5, Attributes.ATK).propagates_to_attribute(Attributes.DEF) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert player.attributes[Attributes.DEF] == 50 assert player.attributes[Attributes.HP] == 25
def test_pulling_propagated_buffs(self): player = Player() player.attributes[Attributes.ATK] = 100 castle_buff = BuffBuilder().modify( "%", 0.5, Attributes.ATK).propagates_to(Player).build() castle = Castle() castle.players.append(player) add_buff(castle, castle_buff, CompleteBuildingEvent()) # First player should be updated assert player.attributes[Attributes.ATK] == 150 # Adding another player player2 = Player() player2.attributes[Attributes.ATK] = 100 # We can enforce a propagation to happen in case we just created a new buffable and we have "global buffs" pull_propagated_buffs(castle, player2, CompleteBuildingEvent()) # Player should have pulled the propagation from his castle assert player2.attributes[Attributes.ATK] == 150
def test_propagation_debug_tracking(self): player = Player() player.attributes[Attributes.ATK] = 100 castle = Castle() castle.players.append(player) # A global castle buff of 50% ATK castle_buff = BuffBuilder().modify( "%", 0.5, Attributes.ATK).propagates_to(Player).build() event_result = add_buff(castle, castle_buff, CompleteBuildingEvent()) # The event result should let us know about the propagation propagated_modifications = event_result.propagated_modifications added_modifications_on_propagation = propagated_modifications[ player.id][0].added_modifications[0] # We also have the history in the attribute modification history attribute_history_modification = list( player.attributes.get_data(Attributes.ATK).history.values())[0] for modification in [ added_modifications_on_propagation, attribute_history_modification ]: # Asserting this stored the modifier correctly assert modification.modifier.value == 0.5 assert modification.modifier.operator == "%" assert modification.modifier.attribute_id == Attributes.ATK # Asserting the buff chain, we added a buff, caused a propagation to add a buff in another buffable event_chain = modification.source_event.get_event_chain() assert isinstance(event_chain[0], AddBuffEvent) assert isinstance(event_chain[1], BuffPropagatedEvent) assert event_chain[1].source_buffable == castle assert isinstance(event_chain[2], AddBuffEvent) assert isinstance(event_chain[3], CompleteBuildingEvent)
def test_adding_buff(self): buffable = Buffable() buff = BuffSpec(1) buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.max_stack = 3 # 3 stacks max buff.modifiers = [Modifier("+", 10, Attributes.DEF)] # Add the buff add_buff(buffable, buff, CompleteBuildingEvent()) # Since it has no triggers or conditions, it was added automatically assert buffable.attributes[Attributes.DEF] == 10 assert buff.buff_id in buffable.active_buffs assert buffable.active_buffs[buff.buff_id].stack == 1 # Add the buff again, should be in 2 stacks now add_buff(buffable, buff, CompleteBuildingEvent()) assert buffable.attributes[Attributes.DEF] == 20 assert buffable.active_buffs[buff.buff_id].stack == 2 # Add the buff again, should be in 2 stacks now add_buff(buffable, buff, CompleteBuildingEvent()) assert buffable.attributes[Attributes.DEF] == 30 assert buffable.active_buffs[buff.buff_id].stack == 3 # Now we reached our max stack, it should always be 3 stacks. add_buff(buffable, buff, CompleteBuildingEvent()) assert buffable.attributes[Attributes.DEF] == 30 assert buffable.active_buffs[buff.buff_id].stack == 3 # If we remove the buff, all stacks should be removed remove_buff(buffable, 1) assert buffable.attributes[Attributes.DEF] == 0 assert buff.buff_id not in buffable.active_buffs
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
def test_propagating_a_derivation_buff(self): player = Player() player.attributes[Attributes.ATK] = 50 player.attributes[Attributes.DEF] = 75 equipment = Equipment() equipment.attributes[Attributes.ATK] = 100 equipment.owner = player castle = Castle() castle.players = [player] # 50% of player def becomes player HP add_buff(equipment, BuffBuilder(1).modify("%", 0.5, Attributes.DEF).to_attribute(Attributes.HP) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert player.attributes[Attributes.HP] == 75 / 2 # 50% of equipment attack goes to player DEF add_buff(equipment, BuffBuilder(2).modify("%", 0.5, Attributes.ATK).propagates_to_attribute(Attributes.DEF)\ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert player.attributes[Attributes.DEF] == 125 assert player.attributes[Attributes.HP] == 125 / 2 # 25% of castle DEF becomes player DEF add_buff(castle, BuffBuilder(3).modify("%", 0.5, Attributes.DEF).propagates_to_attribute(Attributes.DEF) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert castle.attributes[Attributes.DEF] == 0 assert player.attributes[Attributes.DEF] == 125 assert player.attributes[Attributes.HP] == 125 / 2 # Castle buff of +80 DEF, 50% should derivate to player add_buff(castle, BuffBuilder(4).modify("+", 80, Attributes.DEF).build(), CompleteBuildingEvent()) assert castle.attributes[Attributes.DEF] == 80 assert player.attributes[Attributes.DEF] == 125 + 40 assert player.attributes[Attributes.HP] == (125 + 40) / 2 # Another Castle buff of +80 DEF, 50% should derivate to player add_buff(castle, BuffBuilder(5).modify("+", 80, Attributes.DEF).build(), CompleteBuildingEvent()) assert castle.attributes[Attributes.DEF] == 80 + 80 assert player.attributes[Attributes.DEF] == 125 + 40 + 40 assert player.attributes[Attributes.HP] == (125 + 40 + 40) / 2 # Just bumping player + 100 ATK. To remember: add_buff(equipment, BuffBuilder(6).modify("+", 100, Attributes.ATK).build(), CompleteBuildingEvent()) assert castle.attributes[Attributes.DEF] == 80 + 80 assert player.attributes[Attributes.DEF] == 125 + 40 + 40 + 50 assert player.attributes[Attributes.HP] == (125 + 40 + 40 + 50) / 2 # 100% of castle DEF goes to player HP add_buff( castle, BuffBuilder(7).modify( "%", 1, Attributes.DEF).propagates_to_attribute( Attributes.HP).propagates_to(Player).build(), CompleteBuildingEvent()) assert castle.attributes[Attributes.DEF] == 80 + 80 assert player.attributes[Attributes.DEF] == 125 + 40 + 40 + 50 assert player.attributes[Attributes.HP] == ( (125 + 40 + 40 + 50) / 2) + castle.attributes[Attributes.DEF] # 50% of HP to CRIT_DAMAGE on player add_buff( player, BuffBuilder(8).modify("%", 0.5, Attributes.HP).to_attribute( Attributes.CRIT_DAMAGE).build(), CompleteBuildingEvent()) assert player.attributes[Attributes.HP] == ( (125 + 40 + 40 + 50) / 2) + castle.attributes[Attributes.DEF] assert player.attributes[ Attributes.CRIT_DAMAGE] == player.attributes[Attributes.HP] / 2 assert castle.attributes[Attributes.DEF] == 80 + 80 assert player.attributes[Attributes.DEF] == 125 + 40 + 40 + 50 with TrackStack() as track: # 50% of castle def to player def add_buff(castle, BuffBuilder(9).modify("%", 0.5, Attributes.DEF).propagates_to_attribute(Attributes.DEF) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) # Printing the stack, this buff should trigger a 4 step derivation chain track.print_stack() assert player.attributes[Attributes.DEF] == 125 + 40 + 40 + 50 + 80 assert player.attributes[Attributes.HP] == ( (125 + 40 + 40 + 50 + 80) / 2) + castle.attributes[Attributes.DEF] assert player.attributes[ Attributes.CRIT_DAMAGE] == player.attributes[Attributes.HP] / 2 assert castle.attributes[Attributes.DEF] == 80 + 80