def test_propagation_condition_and_trigger(self): player1 = Player() player1.attributes[Attributes.ATK] = 100 castle = Castle() castle.players.append(player1) # A global castle buff that propagates that 50% ATK to players if a castle has 3 or more players castle_buff = BuffBuilder().modify("%", 0.5, Attributes.ATK)\ .propagates_when(RecruitPlayerEvent).only_propagates_if("cond_has_qtd_players 2").propagates_to(Player).build() @buffspecs.AddConditionFor([BuffEvent]) def cond_has_qtd_players(event, amt): return len(event.buffable.players) >= amt add_buff(castle, castle_buff, CompleteBuildingEvent()) # The buff is not active on the player assert castle_buff.buff_id in castle.active_buffs assert castle_buff.buff_id not in player1.active_buffs # Castle should have registered the trigger assert "RecruitPlayerEvent" not in castle.activation_triggers assert "RecruitPlayerEvent" in castle.propagation_triggers # add another player to the castle player2 = Player() player2.attributes[Attributes.ATK] = 200 castle.players.append(player2) call_event(RecruitPlayerEvent(castle)) # Now that we matched the condition all 3 players should have been propagated and have 50% bonus assert player1.attributes[Attributes.ATK] == 100 * 1.5 assert player2.attributes[Attributes.ATK] == 200 * 1.5 # Adding a new player should propagate the buff to the new player as well player3 = Player() player3.attributes[Attributes.ATK] = 300 castle.players.append(player3) call_event(RecruitPlayerEvent(castle)) # Should not affect the other players assert player1.attributes[Attributes.ATK] == 100 * 1.5 assert player2.attributes[Attributes.ATK] == 200 * 1.5 # Should propagate to the new player assert player3.attributes[Attributes.ATK] == 300 * 1.5
def test_propagation_registering_triggers_properly(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 len(equipment.activation_triggers) == 0 assert len(player.activation_triggers) == 0 assert len(equipment.propagation_triggers) == 0 add_buff(equipment, BuffBuilder(2).modify("%", 0.5, Attributes.ATK).propagates_to_attribute(Attributes.HP) \ .propagates_to(Player).build(), CompleteBuildingEvent() ) assert len(equipment.propagation_triggers) == 0 assert len(player.activation_triggers) == 0
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_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_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_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_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_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_performance(self): player1 = Player() player1_equips = [] for i in range(10): equip = Equipment() equip.owner = player1 player1_equips.append(equip) player2 = Player() player2_equips = [] for i in range(10): equip = Equipment() equip.owner = player2 player2_equips.append(equip) castle = Castle() castle.players = [player1, player2] seed = 1234 random.seed(seed) buff_targets = {} for i in range(10): builder = BuffBuilder() modifier = random_modifier() builder.modify(*modifier) propagates = False apply_to, propagate_to = random_targets() if chance_pct(25): builder.to_attribute(random_attribute(exlude=modifier[2])) if chance_pct(25): propagates = True builder.propagates_to_attribute(random_attribute())
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_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_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_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_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_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_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_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