def make(entity_name): """Generates entities from the db.py database of entities.""" # Returns a new copy of the specified entity. if entity_name in db.actor_dict: # Create an Actor entity components = db.actor_dict.get(entity_name) components['name'] = entity_name return copy.deepcopy(Actor(**components)) # Add the components common to all actors # components.update(db.actor_components) # Add the name component # components['name'] = entity_name # return Entity(**components) # return copy.deepcopy(db.actor_dict[entity_name]) if entity_name in db.item_dict: # Create an Item entity components = db.item_dict.get(entity_name) components['name'] = entity_name return copy.deepcopy(Item(**components)) # return copy.deepcopy(db.item_dict[entity_name]) if entity_name in db.dungeon_features: components = db.dungeon_features.get(entity_name) components['name'] = entity_name return copy.deepcopy(Entity(**components)) if entity_name == "money": m = copy.deepcopy(Item(**db.money)) # This is a crude hack, but it should work to make the inventory character always $ m.add_comp(letter=LetterComponent("$")) return m raise ValueError(f"'{entity_name}' is not a valid Entity!")
def test_add_inv_item__new_slot__partial_stack__last_letter_unoccupied(vials5): pi = PlayerInventory(10) vials5.add_comp(letter=LetterComponent('z')) pi.add_inv_item(vials5, 2) result = pi.item_dict['z'] assert result.name == "healing vial" assert result.stackable.size == 2
def test_add_inv_item__new_slot__nonstackable__last_letter_occupied( dagger, plunger): pi = PlayerInventory(10) plunger.add_comp(letter=LetterComponent('a')) pi.add_inv_item(dagger) pi.add_inv_item(plunger) assert pi.item_dict['a'] == dagger assert pi.item_dict['b'] == plunger # rolls to next available letter
def test_add_inv_item__new_slot__full_stack__last_letter_occupied( dagger, vials5): pi = PlayerInventory(10) vials5.add_comp(letter=LetterComponent('a')) pi.add_inv_item(dagger) pi.add_inv_item(vials5) assert pi.item_dict['a'] == dagger assert pi.item_dict['b'] == vials5 # rolls to next available letter
def test_add_inv_item__new_slot__partial_stack__last_letter_occupied( dagger, vials5): pi = PlayerInventory(10) vials5.add_comp(letter=LetterComponent('a')) pi.add_inv_item(dagger) pi.add_inv_item(vials5, 2) result = pi.item_dict['b'] # rolls to next available letter assert result.name == "healing vial" assert result.stackable.size == 2
def add_inv_item(self, item, qty=0): """Attempts to add an item to the inventory. Returns True if successful, False otherwise. Money always goes in $ """ previous_size = len(self) result = self.add_item(item, qty) size_changed = len(self) != previous_size current_items = self.item_dict.values() if result and not size_changed: # it added to a stackable. Letter is already set twin = self.get_similar(item) return twin.letter.letter elif result and size_changed: # a new slot was occupied # Scan the entities to see what was added. new_item = [e for e in self.entities if e not in current_items][0] # See if this item has a previous letter letter = None if new_item.has_comp("letter"): letter = new_item.letter.letter # We'll need to find a new letter if there the last_letter is being used or is None. # Otherwise, we can just use it by default! if not letter or letter in self.item_dict: letter = self.find_next_letter() # Also set the letter in the ItemComponent new_item.add_comp(letter=LetterComponent(letter)) self.item_dict[letter] = new_item return letter # Something went wrong... raise exceptions.Impossible
def test_init__is_Component(): lc = LetterComponent('a') assert isinstance(lc, Component)
def test_init__invalid_letter(): with pytest.raises(ValueError): lc = LetterComponent('?')
def test_init__letter_arg(): lc = LetterComponent('a') assert lc.letter == "a"
def test_add_inv_item__new_slot__nonstackable__last_letter_unoccupied(plunger): pi = PlayerInventory(10) plunger.add_comp(letter=LetterComponent('z')) pi.add_inv_item(plunger) assert pi.item_dict['z'] == plunger
def test_add_inv_item__new_slot__full_stack__last_letter_unoccupied(vials5): pi = PlayerInventory(10) vials5.add_comp(letter=LetterComponent('z')) assert pi.add_inv_item(vials5) == 'z' assert pi.item_dict['z'] == vials5