changeset 101:3d9b6405bd86

Fixed old equipable values not reset when equippin another equipable in its slot. Added Exceptions for the Equip components.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 19:51:37 +0200
parents 853ec1b277c7
children 334f7aea6386
files src/parpg/components/equip.py
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/parpg/components/equip.py	Wed Sep 21 17:39:07 2011 +0200
+++ b/src/parpg/components/equip.py	Wed Sep 21 19:51:37 2011 +0200
@@ -30,19 +30,41 @@
     def __str__(self):
         return "\"%s\" is not a valid slot." % self.slot
     
+class AlreadyEquippedError(Exception):
+    """Error that gets raised when the equipable already has a wearer"""
+    
+    def __str__(self):
+        return "The equipable is already weared."
+
+class CannotBeEquippedInSlot(Exception):
+    """Error that gets raised when the equipable can't be equiped in that slot"""
+    
+    def __init__(self, slot, equipable):
+        self.slot = slot
+        self.equipable = equipable
+        
+    def __str__(self):
+        return "%s is not in the equipables slots. (%s)" % (self.slot, ', '.join(self.equipable.possible_slots))
+    
+    
 def equip(wearer, equipable, slot):
     """Equip the wearer with the given equipable.
     @returns The equipable that was at the given slot, or None"""
+    if equipable.wearer:
+        raise AlreadyEquippedError
     if slot in equipable.possible_slots:
         try:
             old_item = getattr(wearer, slot)
             setattr(wearer, slot, equipable)
             equipable.in_slot = slot
             equipable.wearer = wearer
+            if old_item:
+                old_item.in_slot = None
+                old_item.wearer = None
             return old_item
         except AttributeError:
             raise SlotInvalidError(slot)
-    return None
+    raise CannotBeEquippedInSlot(slot, equipable)
 
 def get_equipable(wearer, slot):
     """Return the equipable in the given slot"""