Tuesday, February 26, 2019

Building Your World part eleven - seeing what you have

What have you got, and what can you see?

So far there are three items in this new world:
mysql> select * from items;+--------+--------------+-----------------------------+-----------+------------+-----------------+| itemID | itemName | itemDescription | itemValue | itemRoomID | itemCharacterID |+--------+--------------+-----------------------------+-----------+------------+-----------------+| 1 | Handkerchief | a handkerchief | 1 | NULL | 1 || 2 | Rolling Pin | a flour covered rolling pin | 1 | 1 | NULL || 3 | Bag of Rice | a paper bag full of rice | 1 | 1 | NULL |+--------+--------------+-----------------------------+-----------+------------+-----------------+

But you cannot see the items in the room, nor can you check what you have in your pockets.

For the former, we return to the Room class.
 In the initialisation code, a new list is added which contains the items present in the room.

        self.item = []

This is not filled because the room may be loaded to determine the available exits during the movement action, and as the character is leaving the room, the contents are of no interest.

The items are added using the load_contents method.
    def load_contents(self,acursor):
        sql="select itemID, itemName, itemDescription, itemValue from items where itemRoomID=%s"
        roomID=(str(self.roomID),)

        acursor.execute(sql,roomID)

        myresult = acursor.fetchall()

        for x in myresult:
            newitem=Item(x[0],x[1],x[2],x[3])
            self.items.append(newitem)

An additional function is required to display the contents of the room:

    def contents(self):
        result="Items:" +str(len(self.items)) +"\n"
        if len(self.items)>0:
            result="The room contains the following: \n"
            for anItem in self.items:
                result+=anItem.get_name()
                result+="\n"
        else:
            result="There are no items in this room"
        return result

A call is made to this function in the  get_details(self) function.

Kitchen======A bright shiny IKEA kitchenThere is a plain door to the southThe room contains the following: Rolling PinBag of Rice