changeset 72:783dc57eca7c

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 8f1edcab5602
children 5c751b9a3d8b
files components/equip.py
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/components/equip.py	Wed Sep 21 17:39:07 2011 +0200
+++ b/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"""