Tuesday, February 12, 2019

Building your world - part eight: Now where was I?

Remembering where you were

As the program currently exists, the starting location is defined in the code before entering the main loop.

player_location=1
while player_location!=3:

...

Now this is not a problem if you only want to always want to start from the beginning each time (and you only have a few room). It also is not a problem if the player's location is maintained within the program. As this is going to eventually be a web game, this will be a problem, the player's location needs to be maintained between calls to the web page.

In the same way that the Room class is populated from the rooms table, the player's location will be stored in a players table.

For a single player game, you could just have the character location as the only field (and a single row). However the idea is to extend this into a multiple character (and player) adventure, so it is worth creating the table to accommodate multiple character/players. 

Note, there is a difference between a player and a character. The former will have a log in so will need to have some way of securely logging in. The latter will "belong" to a player, a player may have multiple characters.

Later this will be extended further, but for now we are concerned with the following:

A table to contain player information. Initially this will just be:

  • Player ID - an auto increment integer primary key used to identify players
  • Username - a 50 character string
Additional fields will be required when logging in etc is implemented.

A table to contain character information
  • Character ID - an auto increment integer to identify the character
  • Player ID - the owning player (who will be the only player able to operate the character
  • Character name - 20 character field containing the character's name
  • Character description - 100 character description of the character
  • Character Room ID - the roomID of the room that contains the character (it can be NULL as the character may not be in a room yet)

Creating the Players table

In a MySQL console, use the following to create the players table:

create table players( 
     playerID INT NOT NULL AUTO_INCREMENT,
     username VARCHAR(50) NOT NULL,
     PRIMARY KEY(playerID),
     UNIQUE(username)
);

Creating the Characters table

create table characters(
     characterID  INT NOT NULL AUTO_INCREMENT,
     playerID INT NOT NULL,
     characterName VARCHAR(20) NOT NULL,
     description VARCHAR(50) NOT NULL,
     characterRoomID INT NULL,
     PRIMARY KEY(characterID ),
     FOREIGN KEY (playerID) REFERENCES players(playerID),
     FOREIGN KEY (characterRoomID) REFERENCES rooms(roomID),
     UNIQUE(characterName)
);

Check the new tables

mysql> show tables;+--------------------------------------+| Tables_in_technologyisnotd$adventure |+--------------------------------------+| characters || links || players || rooms |+--------------------------------------+4 rows in set (0.00 sec)

Populating the tables

At the moment there is only one player, and the mechanisms for dealing with multiple players is for a later date, so a holding "player" called "Me" will do for now.

insert into players(username)values('Me');

mysql> select * from players;+----------+----------+| playerID | username |+----------+----------+| 1 | Me |+----------+----------+1 row in set (0.00 sec)

For testing purposes there will be two characters "Philip" and "John".

insert into characters(playerID,characterName,description)
values(1,'John','a seedy looking gentleman in a long trenchcoat');

insert into characters(playerID,characterName,description)
values(1,'Philip','a man in a black suit and sunglasses');

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 |+-------------+----------+---------------+------------------------------------------------+-----------------+2 rows in set (0.00 sec)