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_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_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_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_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_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_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_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_expiring_stacks(self): buffable = Buffable() buff = BuffSpec(1) buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.duration_seconds = 10 # Only lasts for 10 seconds buff.max_stack = 2 buff.modifiers = [Modifier("+", 50, Attributes.DEF)] # Add the buff 2 times, one in the future add_buff(buffable, buff, CompleteBuildingEvent()) with FixedTime(get_timestamp() + 5): add_buff(buffable, buff, CompleteBuildingEvent()) expiry_time_1 = get_timestamp() + buff.duration_seconds expiry_time_2 = expiry_time_1 + 5 assert buffable.attributes[Attributes.DEF] == 100 assert buffable.active_buffs[buff.buff_id].stack == 2 assert len(buffable.expiry_times) == 2 with FixedTime(expiry_time_1): assert buffable.attributes[Attributes.DEF] == 50 assert buffable.active_buffs[buff.buff_id].stack == 1 assert len(buffable.expiry_times) == 1 with FixedTime(expiry_time_2): assert buffable.attributes[Attributes.DEF] == 0 assert buff.buff_id not in buffable.active_buffs assert len(buffable.expiry_times) == 0
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_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_modification_history(self): buffable = Buffable() attributes = buffable.attributes # Create an event with fake event data source_event = CompleteBuildingEvent() apply_attributes_modification(attributes, BuffModification(Modifier("+", 25, Attributes.ATK), source_event)) apply_attributes_modification(attributes, BuffModification(Modifier("%", 1.00, Attributes.ATK), source_event)) apply_attributes_modification(attributes, BuffModification(Modifier("%", 1.00, Attributes.ATK), source_event)) assert buffable.attributes[Attributes.ATK] == 75 # Checking if we has history of those 3 modifications attribute_history = list(buffable.attributes.get_data(Attributes.ATK).history.values()) assert attribute_history[0].modifier.operator == "+" assert attribute_history[0].modifier.value == 25 assert attribute_history[0].modifier.attribute_id == Attributes.ATK assert attribute_history[0].source_event == source_event assert attribute_history[1].modifier.operator == "%" assert attribute_history[1].modifier.value == 1.00 assert attribute_history[1].modifier.attribute_id == Attributes.ATK assert attribute_history[1].source_event == source_event assert attribute_history[2].modifier.operator == "%" assert attribute_history[2].modifier.value == 1.00 assert attribute_history[2].modifier.attribute_id == Attributes.ATK assert attribute_history[2].source_event == source_event
def test_registering_1_expiry_per_stack(self): buffable = Buffable() buff = BuffSpec(1) buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.duration_seconds = 10 # Only lasts for 10 seconds buff.max_stack = 2 buff.modifiers = [Modifier("+", 50, Attributes.DEF)] # Add the buff 2 times add_buff(buffable, buff, CompleteBuildingEvent()) add_buff(buffable, buff, CompleteBuildingEvent()) # Should have registered one expiry time per stack assert buffable.attributes[Attributes.DEF] == 100 assert buffable.active_buffs[buff.buff_id].stack == 2 assert len(buffable.expiry_times) == 2
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_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_adding_buff(self): buffable = Buffable() buff = BuffSpec() buff.activation_triggers = [] # no triggers buff.conditions = [] # no conditions buff.modifiers = [Modifier("+", 30, 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] == 30 assert buff.buff_id in buffable.active_buffs
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_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_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_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_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_removing_buff(self): buffable = Buffable() buffable.attributes[Attributes.ATK] = 100 buff = BuffSpec() buff.modifiers = [Modifier("%", 0.5, Attributes.ATK)] add_buff(buffable, buff, CompleteBuildingEvent()) assert buffable.attributes[Attributes.ATK] == 150 remove_buff(buffable, buff.buff_id) assert buffable.attributes[Attributes.ATK] == 100 assert buff.buff_id not in buffable.active_buffs
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_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_buff_modification_history(self): buffable = Buffable() buff = BuffSpec() buff.modifiers = [Modifier("+", 30, Attributes.DEF)] # Faking an event that would result in adding a buff event_to_get_the_buff = CompleteBuildingEvent() # Add the buff event_result = add_buff(buffable, buff, event_to_get_the_buff) added_modifications = event_result.added_modifications # Check modification history to debug/backtrack assert added_modifications[0].modifier.operator == "+" assert added_modifications[0].modifier.attribute_id == Attributes.DEF assert added_modifications[ 0].source_event.trigger_event == event_to_get_the_buff
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