Changeset 439


Ignore:
Timestamp:
12/08/09 08:20:30 (10 years ago)
Author:
b0rland_parpg
Message:

Ticket #229: patch by b0rland
Added findItem function which currently allows searching by name and kind inside inventory and container

Location:
trunk/game
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/inventory.py

    r436 r439  
    100100        return self.items[self.item_lookup[ID]].findItemByID(ID) 
    101101 
     102    def findItem(self, **kwargs): 
     103        """Find an item in inventory by various attributes. All parameters are optional. 
     104           @type name: String 
     105           @param name: Unique or non-unique object name. If the name is non-unique, first matching object is returned 
     106           @type kind: String 
     107           @param kind: One of the possible object kinds like "openable" or "weapon" (see base.py) 
     108           @return: The item matching criteria or None if none was found 
     109        """ 
     110 
     111        for slot in self.items: 
     112            item_found = self.items[slot].findItem(**kwargs) 
     113            if item_found != None: 
     114                return item_found 
     115        return None 
    102116 
    103117    def __repr__(self): 
  • trunk/game/scripts/objects/base.py

    r436 r439  
    268268                return self.items[i] 
    269269        return None 
     270 
     271    def findItem(self, **kwargs): 
     272        """Find an item in container by various attributes. All parameters are optional. 
     273           @type name: String 
     274           @param name: Unique or non-unique object name. If the name is non-unique, first matching object is returned 
     275           @type kind: String 
     276           @param kind: One of the possible object kinds like "openable" or "weapon" 
     277           @return: The item matching criteria or None if none was found 
     278        """ 
     279        for i in self.items : 
     280            if "name" in kwargs and self.items[i].name != kwargs["name"]: 
     281                continue 
     282            if "kind" in kwargs and not self.items[i].trueAttr(kwargs["kind"]): 
     283                continue 
     284            return self.items[i] 
     285        return None 
    270286         
    271287class Living (object): 
  • trunk/game/tests/test_inventory.py

    r436 r439  
    2727 
    2828    def setUp(self): 
    29         self.item = self.CarryableObject(12) 
     29        self.item = self.CarryableObject(12, name="TestItem1") 
    3030        self.item.weight = 12 
    3131        self.item2 = self.CarryableObject(13) 
     
    7474        self.assertEqual(self.inventory.findItemByID(12), self.item) 
    7575        self.assertEqual(self.inventory.findItemByID(13), None) 
     76        self.assertEqual(self.inventory.findItem(name="TestItem1"), self.item) 
     77        self.assertEqual(self.inventory.findItem(name="RandomName1"), None) 
     78        self.assertEqual(self.inventory.findItem(kind="carryable"), self.item) 
     79        self.assertEqual(self.inventory.findItem(kind="weapon"), None) 
    7680 
    7781         
Note: See TracChangeset for help on using the changeset viewer.