# HG changeset patch # User KarstenBock@gmx.net # Date 1316627497 -7200 # Node ID 3d9b6405bd861c5ae6db96f5d596a99f1cebe461 # Parent 853ec1b277c7cbc8abb774239475727e693d20f7 Fixed old equipable values not reset when equippin another equipable in its slot. Added Exceptions for the Equip components. diff -r 853ec1b277c7 -r 3d9b6405bd86 src/parpg/components/equip.py --- 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"""