comparison components/equip.py @ 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 9e03f7816061
comparison
equal deleted inserted replaced
71:8f1edcab5602 72:783dc57eca7c
28 self.slot = slot 28 self.slot = slot
29 29
30 def __str__(self): 30 def __str__(self):
31 return "\"%s\" is not a valid slot." % self.slot 31 return "\"%s\" is not a valid slot." % self.slot
32 32
33 class AlreadyEquippedError(Exception):
34 """Error that gets raised when the equipable already has a wearer"""
35
36 def __str__(self):
37 return "The equipable is already weared."
38
39 class CannotBeEquippedInSlot(Exception):
40 """Error that gets raised when the equipable can't be equiped in that slot"""
41
42 def __init__(self, slot, equipable):
43 self.slot = slot
44 self.equipable = equipable
45
46 def __str__(self):
47 return "%s is not in the equipables slots. (%s)" % (self.slot, ', '.join(self.equipable.possible_slots))
48
49
33 def equip(wearer, equipable, slot): 50 def equip(wearer, equipable, slot):
34 """Equip the wearer with the given equipable. 51 """Equip the wearer with the given equipable.
35 @returns The equipable that was at the given slot, or None""" 52 @returns The equipable that was at the given slot, or None"""
53 if equipable.wearer:
54 raise AlreadyEquippedError
36 if slot in equipable.possible_slots: 55 if slot in equipable.possible_slots:
37 try: 56 try:
38 old_item = getattr(wearer, slot) 57 old_item = getattr(wearer, slot)
39 setattr(wearer, slot, equipable) 58 setattr(wearer, slot, equipable)
40 equipable.in_slot = slot 59 equipable.in_slot = slot
41 equipable.wearer = wearer 60 equipable.wearer = wearer
61 if old_item:
62 old_item.in_slot = None
63 old_item.wearer = None
42 return old_item 64 return old_item
43 except AttributeError: 65 except AttributeError:
44 raise SlotInvalidError(slot) 66 raise SlotInvalidError(slot)
45 return None 67 raise CannotBeEquippedInSlot(slot, equipable)
46 68
47 def get_equipable(wearer, slot): 69 def get_equipable(wearer, slot):
48 """Return the equipable in the given slot""" 70 """Return the equipable in the given slot"""
49 try: 71 try:
50 item = getattr(wearer, slot) 72 item = getattr(wearer, slot)