def __call__(self, caster): """ Performs the casting :param caster: character doing the casting :type caster: Character """ add_history_value(caster, 'hit_points') spell_factory = SpellGeneratorBuilder().build() effects_factory = get_effect_creator({ 'heal medium wounds': { 'type': Heal, 'duration': None, 'frequency': None, 'tick': 0, 'healing': 10, 'icon': None, 'title': 'Heal medium wounds', 'description': 'Heals medium amount of damage' }, # noqa 'cause wound': { 'type': DamageEffect, 'duration': None, 'frequency': None, 'tick': 0, 'damage': 5, 'damage_type': 'magic', 'icon': None, 'title': 'Cause minor wound', 'description': 'Causes minor amount of damage' }, # noqa 'fire': { 'type': DamageEffect, 'duration': 30, 'frequency': 5, 'tick': 0, 'damage': 3, 'damage_type': 'fire', 'icon': None, 'title': 'Fire', 'description': 'You are on fire!' } }) spell_casting_factory = ( SpellCastingFactoryBuilder().with_spell_factory( spell_factory).with_effects_factory(effects_factory).build()) set_action_factory(ActionFactoryBuilder().with_spellcasting_factory( spell_casting_factory).build() ) # TODO: mutating global state is bad if self.target: direction = find_direction(caster.location, self.target.location) else: direction = 1 cast(caster, direction=direction, spell_name=self.spell_name)
def setup(self): """ Setup the test case """ self.character = (CharacterBuilder() .with_model(Model()) .build()) self.level1 = (LevelBuilder() .with_floor_tile("floor") .with_wall_at((1, 0)) .build()) self.level2 = (LevelBuilder() .with_floor_tile("floor") .build()) self.portal1 = Portal((None, None), None) self.portal1.icon = 1 self.portal2 = Portal(("stairs", "stairs"), None) self.portal2 = Portal(("stairs", "stairs"), None) add_portal(self.level1, (5, 5), self.portal1) add_portal(self.level2, (10, 10), self.portal2, self.portal1) add_character(self.level1, (5, 5), self.character) set_action_factory(ActionFactoryBuilder() .build())
def setup(self): """ Setup the test case """ self.rng = Random() self.model = mock() self.effect_factory = get_effect_creator({ 'heal': { 'type': Heal, 'duration': 0, 'frequency': 0, 'tick': 0, 'healing': 10, 'icon': 101, 'title': 'title', 'description': 'major heal' } }) drink_factory = DrinkFactory(self.effect_factory) set_action_factory(ActionFactory(self.model, drink_factory)) self.character = ( CharacterBuilder().with_hit_points(1).with_max_hp(5).build()) effect = (HealBuilder().with_duration(0).with_frequency(0).with_tick( 0).with_healing(5).with_target(self.character).build()) self.potion = ( ItemBuilder().with_name('healing potion').with_effect_handle( EffectHandleBuilder().with_trigger('on drink').with_effect( 'heal')).build()) self.character.inventory.append(self.potion)
def setup(self): """ Setup test case """ set_action_factory(ActionFactoryBuilder() .with_inventory_factory() .build())
def __call__(self, caster): """ Performs the casting :param caster: character doing the casting :type caster: Character """ add_history_value(caster, 'hit_points') spell_factory = SpellGeneratorBuilder().build() effects_factory = get_effect_creator({'heal medium wounds': {'type': Heal, 'duration': None, 'frequency': None, 'tick': 0, 'healing': 10, 'icon': None, 'title': 'Heal medium wounds', 'description': 'Heals medium amount of damage'}, # noqa 'cause wound': {'type': DamageEffect, 'duration': None, 'frequency': None, 'tick': 0, 'damage': 5, 'damage_type': 'magic', 'icon': None, 'title': 'Cause minor wound', 'description': 'Causes minor amount of damage'}, # noqa 'fire': {'type': DamageEffect, 'duration': 30, 'frequency': 5, 'tick': 0, 'damage': 3, 'damage_type': 'fire', 'icon': None, 'title': 'Fire', 'description': 'You are on fire!'}}) spell_casting_factory = (SpellCastingFactoryBuilder() .with_spell_factory(spell_factory) .with_effects_factory(effects_factory) .build()) set_action_factory(ActionFactoryBuilder() .with_spellcasting_factory(spell_casting_factory) .build()) # TODO: mutating global state is bad if self.target: direction = find_direction(caster.location, self.target.location) else: direction = 1 cast(caster, direction=direction, spell_name=self.spell_name)
def __call__(self, character): """ Execute the action """ set_action_factory(ActionFactoryBuilder().with_gain_domain_factory(). build()) # TODO: mutating global state is bad gain_domain(character=character, item=self.item, domain=self.domain)
def __call__(self, character): """ Execute the action """ set_action_factory(ActionFactoryBuilder() .with_gain_domain_factory() .build()) # TODO: mutating global state is bad gain_domain(character=character, item=self.item, domain=self.domain)
def initialise_factories(self, context): """ Initialises action factory, sub factories and various generators """ effect_config = {} configurators = self.get_configurators(context.config_package, 'init_effects') for configurator in configurators: effects = configurator(context) for effect in effects: effect_config[effect[0]] = effect[1] effect_factory = get_effect_creator(effect_config) pyherc.vtable["\ufdd0:create-effect"] = effect_factory drink_factory = DrinkFactory(effect_factory) inventory_factory = InventoryFactory([PickUpFactory(), DropFactory(), EquipFactory(), UnEquipFactory()]) spell_factory = SpellGenerator() spell_casting_factory = SpellCastingFactory(spell_factory, effect_factory) mitosis_factory = MitosisFactory(self.creature_generator, self.rng, 60) metamorphosis_factory = MetamorphosisFactory(self.creature_generator, self.rng) dig_factory = DigFactory(self.rng) trapping_factory = TrappingFactory(self.trap_generator) self.action_factory = ActionFactory(self.model, [drink_factory, inventory_factory, spell_casting_factory, mitosis_factory, metamorphosis_factory, dig_factory, trapping_factory]) set_action_factory(self.action_factory) self.rules_engine = RulesEngine(self.action_factory)
def action_factorize(*args, **kwargs): """ Inject action factory """ context = args[0] if not hasattr(context, 'action_factory'): context.action_factory = (ActionFactoryBuilder( ).with_drink_factory().with_inventory_factory().build()) set_action_factory(context.action_factory) return fn(*args, **kwargs)
def initialise_factories(self, context): """ Initialises action factory, sub factories and various generators """ effect_config = {} configurators = self.get_configurators(context.config_package, 'init_effects') for configurator in configurators: effects = configurator(context) for effect in effects: effect_config[effect[0]] = effect[1] effect_factory = get_effect_creator(effect_config) pyherc.vtable["\ufdd0:create-effect"] = effect_factory drink_factory = DrinkFactory(effect_factory) inventory_factory = InventoryFactory( [PickUpFactory(), DropFactory(), EquipFactory(), UnEquipFactory()]) spell_factory = SpellGenerator() spell_casting_factory = SpellCastingFactory(spell_factory, effect_factory) mitosis_factory = MitosisFactory(self.creature_generator, self.rng, 60) metamorphosis_factory = MetamorphosisFactory(self.creature_generator, self.rng) dig_factory = DigFactory(self.rng) trapping_factory = TrappingFactory(self.trap_generator) self.action_factory = ActionFactory(self.model, [ drink_factory, inventory_factory, spell_casting_factory, mitosis_factory, metamorphosis_factory, dig_factory, trapping_factory ]) set_action_factory(self.action_factory) self.rules_engine = RulesEngine(self.action_factory)
def action_factorize(*args, **kwargs): """ Inject action factory """ context = args[0] if not hasattr(context, 'action_factory'): context.action_factory = (ActionFactoryBuilder() .with_drink_factory() .with_inventory_factory() .build()) set_action_factory(context.action_factory) return fn(*args, **kwargs)
def __call__(self, actor): """ Performs the drop action :param actor: character dropping the item :type actor: Character """ add_history_value(actor, 'location') add_history_value(actor, 'level') add_history_value(actor, 'inventory') add_history_value(actor, 'tick') set_action_factory( ActionFactoryBuilder().with_drink_factory().with_inventory_factory( ).build()) # TODO: mutating global state is bad drop_item(actor, self.item)
def setup(self): """ Test setup """ self.level = LevelBuilder().build() self.monster_1 = (CharacterBuilder().with_name('Pete').build()) self.monster_2 = (CharacterBuilder().with_name('Uglak').build()) self.monster_1.artificial_intelligence = lambda: None self.monster_2.artificial_intelligence = lambda: None add_character(self.level, (5, 5), self.monster_1) add_character(self.level, (6, 5), self.monster_2) set_action_factory(ActionFactoryBuilder().build())
def setup(self): """ Setup test case """ self.model = Model() self.character = (CharacterBuilder().with_model( self.model).with_location((1, 1)).build()) self.model.dungeon = (LevelBuilder().with_character( self.character).build()) self.listener = mock() self.model.register_event_listener(self.listener) set_action_factory(ActionFactoryBuilder().build())
def setup(self): """ Setup for testcases """ self.model = Model() set_action_factory(ActionFactoryBuilder() .build()) self.character1 = (CharacterBuilder() .with_model(self.model) .with_hit_points(10) .with_attack(3) .with_body(5) .build()) self.effect = DamageModifier(modifier=1, damage_type='crushing', duration=None, frequency=None, tick=None, icon=101, title='Weakness against crushing', description='This character is weak') self.effect.multiple_allowed = True self.character2 = (CharacterBuilder() .with_model(self.model) .with_hit_points(10) .with_attack(3) .with_body(5) .with_effect(self.effect) .build()) self.model.dungeon = Dungeon() self.level = (LevelBuilder() .with_character(self.character1, at_(5, 5)) .with_character(self.character2, at_(6, 5)) .build()) self.model.dungeon.levels = self.level self.rng = mock() when(self.rng).randint(1, 6).thenReturn(1)
def test_spell_casting_executes_action(self): """ Casting a spell should activate the action """ magic_factory = SpellCastingFactoryBuilder().build() action = mock() when(action).is_legal().thenReturn(True) when(magic_factory).get_action(any()).thenReturn( action) #pylint: disable-msg=E1103 set_action_factory(ActionFactoryBuilder().with_spellcasting_factory( magic_factory).build()) caster = (CharacterBuilder().build()) cast(caster, direction=1, spell_name='healing wind') verify(action).execute()
def __call__(self, actor): """ Performs the drop action :param actor: character dropping the item :type actor: Character """ add_history_value(actor, 'location') add_history_value(actor, 'level') add_history_value(actor, 'inventory') add_history_value(actor, 'tick') set_action_factory(ActionFactoryBuilder() .with_drink_factory() .with_inventory_factory() .build()) # TODO: mutating global state is bad drop_item(actor, self.item)
def setup(self): """ Setup the test case """ self.rng = Random() self.model = mock() self.effect_factory = get_effect_creator({'heal': {'type': Heal, 'duration': 0, 'frequency': 0, 'tick': 0, 'healing': 10, 'icon': 101, 'title': 'title', 'description': 'major heal'}}) drink_factory = DrinkFactory(self.effect_factory) set_action_factory(ActionFactory(self.model, drink_factory)) self.character = (CharacterBuilder() .with_hit_points(1) .with_max_hp(5) .build()) effect = (HealBuilder() .with_duration(0) .with_frequency(0) .with_tick(0) .with_healing(5) .with_target(self.character) .build()) self.potion = (ItemBuilder() .with_name('healing potion') .with_effect_handle( EffectHandleBuilder() .with_trigger('on drink') .with_effect('heal')) .build()) self.character.inventory.append(self.potion)
def __call__(self, attacker): """ Performs the hit :param attacker: character attacking :type attacker: Character """ add_history_value(self.target, 'hit_points') rng = mock() when(rng).randint(1, 6).thenReturn(1) action_factory = (ActionFactoryBuilder().with_drink_factory(). with_inventory_factory().build()) set_action_factory( action_factory) # TODO: mutating global state is bad pyherc.vtable['\ufdd0:attack'](attacker, find_direction(attacker.location, self.target.location))
def setup(self): """ Setup test case """ self.model = Model() self.character = (CharacterBuilder() .with_model(self.model) .with_location((1, 1)) .build()) self.model.dungeon = (LevelBuilder() .with_character(self.character) .build()) self.listener = mock() self.model.register_event_listener(self.listener) set_action_factory(ActionFactoryBuilder() .build())
def test_spell_casting_executes_action(self): """ Casting a spell should activate the action """ magic_factory = SpellCastingFactoryBuilder().build() action = mock() when(action).is_legal().thenReturn(True) when(magic_factory).get_action(any()).thenReturn(action) #pylint: disable-msg=E1103 set_action_factory(ActionFactoryBuilder() .with_spellcasting_factory(magic_factory) .build()) caster = (CharacterBuilder() .build()) cast(caster, direction = 1, spell_name = 'healing wind') verify(action).execute()
def setup(self): """ Setup test cases """ self.item = (ItemBuilder() .build()) self.model = Model() self.level = LevelBuilder().build() self.character = (CharacterBuilder() .with_location((5, 5)) .with_level(self.level) .with_model(self.model) .build()) add_item(self.level, (5, 5), self.item) set_action_factory(ActionFactoryBuilder() .with_inventory_factory() .build())
def __call__(self, attacker): """ Performs the hit :param attacker: character attacking :type attacker: Character """ add_history_value(self.target, 'hit_points') rng = mock() when(rng).randint(1, 6).thenReturn(1) action_factory = (ActionFactoryBuilder() .with_drink_factory() .with_inventory_factory() .build()) set_action_factory(action_factory) # TODO: mutating global state is bad pyherc.vtable['\ufdd0:attack'](attacker, find_direction(attacker.location, self.target.location))
def setup(self): """ Test setup """ self.level = LevelBuilder().build() self.monster_1 = (CharacterBuilder() .with_name('Pete') .build()) self.monster_2 = (CharacterBuilder() .with_name('Uglak') .build()) self.monster_1.artificial_intelligence = lambda : None self.monster_2.artificial_intelligence = lambda : None add_character(self.level, (5, 5), self.monster_1) add_character(self.level, (6, 5), self.monster_2) set_action_factory(ActionFactoryBuilder() .build())
def setup(self): """ Setup the test case """ self.character = (CharacterBuilder().with_model(Model()).build()) self.level1 = (LevelBuilder().with_floor_tile("floor").with_wall_at( (1, 0)).build()) self.level2 = (LevelBuilder().with_floor_tile("floor").build()) self.portal1 = Portal((None, None), None) self.portal1.icon = 1 self.portal2 = Portal(("stairs", "stairs"), None) self.portal2 = Portal(("stairs", "stairs"), None) add_portal(self.level1, (5, 5), self.portal1) add_portal(self.level2, (10, 10), self.portal2, self.portal1) add_character(self.level1, (5, 5), self.character) set_action_factory(ActionFactoryBuilder().build())
def setup(self): """ Setup test case """ self.model = mock() effects = get_effect_creator({'poison': {'type': Poison, 'duration': 12, 'frequency': 3, 'tick': 3, 'damage': 5, 'icon': 101, 'title': 'poison', 'description': 'Causes damage'}}) pyherc.vtable['\ufdd0:create-effect'] = effects set_action_factory(ActionFactoryBuilder() .with_effect_factory(effects) .build()) self.attacker = (CharacterBuilder() .with_location((5, 5)) .with_effect_handle(EffectHandleBuilder() .with_trigger('on attack hit') .with_effect('poison')) .with_model(self.model) .build()) self.defender = (CharacterBuilder() .with_location((5, 4)) .with_hit_points(50) .with_model(self.model) .build()) (LevelBuilder().with_character(self.attacker) .with_character(self.defender) .build())
def test_creating_effect(self): """ Test that effect can be created and triggered immediately """ effect_factory = get_effect_creator({'major heal': {'type': Heal, 'duration': 0, 'frequency': 0, 'tick': 0, 'healing': 10, 'icon': 100, 'title': 'healig', 'description': 'healing'}}) pyherc.vtable['\ufdd0:create-effect'] = effect_factory potion = (ItemBuilder() .with_effect_handle(EffectHandleBuilder() .with_trigger('on drink') .with_effect('major heal') .with_charges(2)) .build()) set_action_factory(ActionFactoryBuilder() .with_drink_factory(DrinkFactoryBuilder() .with_effect_factory(effect_factory)) # noqa .build()) character = (CharacterBuilder() .with_hit_points(1) .with_max_hp(10) .build()) drink(character, potion) assert_that(character.hit_points, is_(equal_to(10))) assert_that(character, has_no_effects())
def test_drinking_triggers_effect(self): """ Test that timed effect is triggered only after enough time has passed """ effect_factory = get_effect_creator({'major heal': {'type': Heal, 'duration': 12, 'frequency': 3, 'tick': 3, 'healing': 10, 'icon': 100, 'title': 'healig', 'description': 'healing'}}) pyherc.vtable['\ufdd0:create-effect'] = effect_factory potion = (ItemBuilder() .with_effect_handle(EffectHandleBuilder() .with_trigger('on drink') .with_effect('major heal') .with_charges(2)) .build()) set_action_factory(ActionFactoryBuilder() .with_drink_factory(DrinkFactoryBuilder() .with_effect_factory(effect_factory)) # noqa .build()) character = (CharacterBuilder() .with_hit_points(1) .with_max_hp(10) .build()) drink(character, potion) assert_that(character, has_effects(1))
def setup(self): """ Setup test cases """ self.character = (CharacterBuilder() .with_hit_points(10) .build()) self.target = (CharacterBuilder() .with_hit_points(10) .build()) self.level = (LevelBuilder() .build()) add_character(self.level, (2, 2), self.character) add_character(self.level, (5, 2), self.target) bow = (ItemBuilder() .with_name('bow') .with_required_ammunition_type('arrow') .with_damage(1, 'crushing') .build()) self.arrows = (ItemBuilder() .with_name('arrows') .with_ammunition_type('arrow') .with_range_damage(3, 'piercing') .with_count(10) .build()) self.character.inventory.append(bow) self.character.inventory.append(self.arrows) self.character.inventory.weapon = bow self.character.inventory.projectiles = self.arrows set_action_factory(ActionFactoryBuilder() .build())
def setup(self): """ Setup for testcases """ self.model = Model() set_action_factory(ActionFactoryBuilder().build()) self.character1 = (CharacterBuilder().with_model( self.model).with_hit_points(10).with_attack(3).with_body( 5).build()) self.effect = DamageModifier(modifier=1, damage_type='crushing', duration=None, frequency=None, tick=None, icon=101, title='Weakness against crushing', description='This character is weak') self.effect.multiple_allowed = True self.character2 = (CharacterBuilder().with_model( self.model).with_hit_points(10).with_attack(3).with_body( 5).with_effect(self.effect).build()) self.model.dungeon = Dungeon() self.level = (LevelBuilder().with_character(self.character1, at_(5, 5)).with_character( self.character2, at_(6, 5)).build()) self.model.dungeon.levels = self.level self.rng = mock() when(self.rng).randint(1, 6).thenReturn(1)
def setup(self): """ Setup test cases """ self.character = (CharacterBuilder().with_hit_points(10).build()) self.target = (CharacterBuilder().with_hit_points(10).build()) self.level = (LevelBuilder().build()) add_character(self.level, (2, 2), self.character) add_character(self.level, (5, 2), self.target) bow = (ItemBuilder().with_name('bow').with_required_ammunition_type( 'arrow').with_damage(1, 'crushing').build()) self.arrows = (ItemBuilder().with_name('arrows').with_ammunition_type( 'arrow').with_range_damage(3, 'piercing').with_count(10).build()) self.character.inventory.append(bow) self.character.inventory.append(self.arrows) self.character.inventory.weapon = bow self.character.inventory.projectiles = self.arrows set_action_factory(ActionFactoryBuilder().build())
def setup(self): """ Setup test case """ set_action_factory( ActionFactoryBuilder().with_inventory_factory().build())