Skip to content

Commit

Permalink
bugfix for the save_item case
Browse files Browse the repository at this point in the history
  • Loading branch information
sgongora27 committed Sep 25, 2024
1 parent 61246c6 commit 211f55c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ def move(self, new_location: Location):
else:
raise Exception(f"Error: {new_location.name} is not reachable")

def save_item(self,item: Item):
def save_item(self,item: Item, item_location_or_owner):
"""Add an item to the character inventory."""
if item.gettable:
if item not in self.inventory:
self.inventory += [item]
self.location.items = [i for i in self.location.items if i is not item]
if item_location_or_owner.__class__.__name__ == 'Character':
item_location_or_owner.inventory = [i for i in item_location_or_owner.inventory if i is not item]
elif item_location_or_owner.__class__.__name__ == 'Location':
item_location_or_owner.items = [i for i in item_location_or_owner.items if i is not item]
else:
raise Exception(f"Error: {item.name} is already in your inventory")
else:
Expand All @@ -114,8 +117,7 @@ def drop_item (self, item: Item):
def give_item (self, character: 'Character', item: Item):
"""Give an item to another character."""
try:
self.inventory = [i for i in self.inventory if i is not item]
character.save_item(item)
character.save_item(item, self)
except Exception as e:
print(e)

Expand Down Expand Up @@ -261,10 +263,15 @@ def parse_moved_objects (self, updates: str) -> None:
pair = re.findall(r"<([^<>]*?)>.*?<([^<>]*?)>",parsed_object)
try:
world_item = self.items[pair[0][0]]

if pair[0][1] == 'Inventory': #(save_item case)
self.player.save_item(world_item)
item_location = [character for character in list(self.characters.values()) if world_item in character.inventory]
item_location += [location for location in list(self.locations.values()) if world_item in location.items]
self.player.save_item(world_item, item_location[0])

elif pair[0][1] in self.characters: #(give_item case)
self.player.give_item(self.characters[pair[0][1]], world_item)

else: #(drop_item case)
self.player.drop_item(world_item)
except Exception as e:
Expand Down

0 comments on commit 211f55c

Please sign in to comment.