Completed
Push — master ( e60eb4...733664 )
by Matt
01:03
created

clotheshorse.ClothesHorse.wear()   B

Complexity

Conditions 6

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nop 2
dl 0
loc 19
rs 8.6666
c 0
b 0
f 0
1
from container import Container
2
class ClothesHorse(Container):
3
    def __init__(self, inventory, wearing):
4
        super().__init__(inventory)
5
        self.wearing = wearing
6
    
7
    def has(self, item_id):
8
        return super().has(item_id) or item_id in self.wearing
9
10
    def is_carrying_anything(self):
11
        return super().is_carrying_anything or len(self.wearing > 0)
12
13
    def is_wearing(self, item_id):
14
        return item_id in self.wearing
15
    
16
    # Basically "take" only we pass our client a 
17
    # reference to the item that they should only
18
    # use temporariliy. We still hold the item.
19
    def get_item_reference(self, item_id):
20
        return super().get_item_reference(item_id) or self.wearing.get(item_id)
21
22
    def take(self, item_id):
23
        if item_id in self.wearing:
24
            if self.wearing[item_id].has_trait("moveable"):
25
                return (self.wearing.pop(item_id), None)
26
            else:
27
                return (None, "You don't want to take that off.")
28
        return super().take(item_id)
29
    
30
    def wear(self, item_id):
31
        if self.is_wearing(item_id):
32
            return (False, "You're already wearing that.")
33
        item_to_wear = self.inventory.get(item_id)
34
        if item_to_wear:
35
            # Note that a trait can be an empty dictionary, so has_trait is a better check for existence
36
            # than getting the trait and checking it (as an empty dictionary evaluates to False)
37
            if item_to_wear.has_trait("wearable"):
38
                wearable_trait = item_to_wear.get_trait("wearable")
39
                slot_name = wearable_trait.get("slot")
40
                if slot_name and self.wearing_in_slot(slot_name):
41
                   return (False, "You'll need to take something off first.")
42
                (item, message) = self.take(item_id)
43
                self.wearing[item_id] = item
44
                return (True, wearable_trait.get("wear_description") or f"You are now wearing {item.name}.")
45
            else:
46
                return (False, "You can't wear that.")
47
        else:
48
            return (False, "You're not carrying that.")
49
50
    def wearing_in_slot(self, slot_name):
51
        # Simple code for now as we never allow the player to wear 
52
        # more than one item per slot.
53
        for item in self.wearing.values():
54
            if item.get_trait("wearable").get("slot") == slot_name:
55
                return item
56
        return None
57
58
    @property
59
    def is_fully_clothed(self):
60
        return self.wearing_in_slot("top") is not None and self.wearing_in_slot("bottom") is not None
61
        
62
    def unwear(self, item_id):
63
        if not item_id in self.wearing:
64
            return (False, "You're not wearing that.")
65
        else:
66
            if self.wearing[item_id].has_trait("wearable"):
67
                trait = self.wearing[item_id].get_trait("wearable")
68
                if not trait.get("unremoveable") or False:
69
                    item = self.wearing.pop(item_id)
70
                    # It goes back into our general inventory
71
                    self.give(item)
72
                    return (True, "You take off " + item.name + ".")
73
                else:
74
                    message = trait.get("unwear_description") or "You can't take that off."
75
                    return (False, message)
76
            else:
77
                return(False, "That doesn't seem appropriate.")
78
79
    def __repr__(self):
80
        debug = super().__repr__() + "\n"
81
        if self.wearing:
82
            debug += "Clothes horse wearing: " + ", ".join(self.wearing)
83
        else:
84
            debug += "(Wearing nothing)"
85
        return debug
86