Thursday, February 14, 2019

Building Your World - part nine: Where am I now

Storing and retrieving your location

In the last part two tables were created, one to store player information, the other to store player information. The player information is not important at the moment, there is only one player. However as both characters are associated with the single player, we will have to have some way of matching what you want to do with the particular character.

But first, let us start by transferring the character location from the code to the database.

Assigning initial locations

The two characters created in the last part have no location information:
mysql> select * from characters;+-------------+----------+---------------+------------------------------------------------+-----------------+| characterID | playerID | characterName | description | characterRoomID |+-------------+----------+---------------+------------------------------------------------+-----------------+| 1 | 1 | John | a seedy looking gentleman in a long trenchcoat | NULL || 2 | 1 | Philip | a man in a black suit and sunglasses | NULL |+-------------+----------+---------------+------------------------------------------------+-----------------+

For the initial part of this exercise, we will be using "John", character ID 1.

So we start by placing John in the first room, the kitchen.
mysql> update characters set characterRoomID=1 where characterID=1;


ysql> select characterRoomID,characterName,characterID from characters;+-----------------+---------------+-------------+| characterRoomID | characterName | characterID |+-----------------+---------------+-------------+| 1 | John | 1 || NULL | Philip | 2 |+-----------------+---------------+-------------+2 rows in set (0.00 sec)

Getting the character information

So the next stage is to decide what we want, and then build a class to handle it.

At the moment, we want to obtain the location of the the player's current character. Later we will may want to know what they are carrying, any skills they have and other information we have not thought of.

The class will have a creator, that takes the player ID and looks everything else up for us. We will default to taking the first character for that player.

class Character():
    def __init__(self,player_ID,acursor):
        sql="SELECT characterID,playerID,characterName,description,characterRoomID FROM characters WHERE playerID=%s ORDER BY playerID LIMIT 1 "
        playerID=(str(player_ID),)

        acursor.execute(sql,playerID)

        myresult = acursor.fetchall()
        for x in myresult:
            self.characterID=x[0]
            self.playerID=x[1]
            self.name=x[2]
            self.description=x[3]
            self.roomID=x[4]
        self.items={}

    # Getters and setters
    def get_currentRoom(self):
        return self.roomID

    def set_currentRoom(self,newRoom):
        self.roomID=newRoom
        return self.roomID

    def get_name(self):
        return self.name

    def get_description(self):
        return self.description()

    # Actions
    def update_character(self, acursor):
        sql="UPDATE characters SET characterRoomID=%s WHERE playerID=%s AND characterID=%s"
        params=(str(self.roomID),str(self.playerID),str(self.characterID))
        acursor.execute(sql,params)

The update_character method does not include a commit, that needs to be in the calling code.

Modifications to the main program

from room import Room
from character import Character
import adventureconnection
mydb=adventureconnection.make_connection()
mycursor = mydb.cursor()

player_character=Character(1,mycursor)

while player_character.get_currentRoom()!=3:

    current_room = Room(player_character.get_currentRoom(),mycursor)
    current_room.get_details()
    command=input("> ")
    if command in("north","south","east","west"):
        print("Go "+command)
        new_location=current_room.move(command)
        if player_character.get_currentRoom()==new_location:
            print("You cannot go " +command)
        else:
            player_character.set_currentRoom(new_location)
            player_character.update_character(mycursor)
            mydb.commit()


print("And you have left the building...")

Test Run

Kitchen======A bright shiny IKEA kitchenThere is a plain door to the south> southGo southDining Hall======A room containing pale Scandinavian furnitureThere is a discrete door to the northThere is a grand door to the west> west
Go westAnd you have left the building...>>>


mysql> select characterID,playerID,characterName,characterRoomID from characters where playerID='1' order by playerID limit 1;+-------------+----------+---------------+-----------------+| characterID | playerID | characterName | characterRoomID |+-------------+----------+---------------+-----------------+| 1 | 1 | John | 3 |+-------------+----------+---------------+-----------------+1 row in set (0.00 sec)