DatabaseManager¶
-
class
database.DatabaseManager(filename, log=None)[source]¶ The Database Manager
This manager handles interactions with a TinyDB database corresponding to the current game world. After documents are pulled from a table and modified, they need to be upserted for the changes to save.
- Variables
database – The TinyDB database instance for the world.
rooms – The table of all rooms in the database.
users – The table of all users in the database.
items – The table of all items in the database.
defaults – The JSON database defaults configuration.
-
__init__(filename, log=None)[source]¶ Database Manager Initializer
- Parameters
filename – The relative or absolute filename of the TinyDB database file.
log – Alternative logging facility, if set.
-
delete_item(document)[source]¶ Delete an item.
- Parameters
document – The item document to delete.
- Returns
True
-
delete_room(document)[source]¶ Delete a room.
- Parameters
document – The room document to delete.
- Returns
True
-
delete_user(document)[source]¶ Delete a user.
This is not safe to call while the user is online.
- Parameters
document – The user document to delete.
- Returns
True
-
item_by_id(itemid)[source]¶ Get an item by its id.
- Parameters
itemid – The id of the item to retrieve from the database.
- Returns
Item document or None.
-
login_user(username, passhash)[source]¶ Check if a username and password match an existing user, and log them in.
The Database Manager keeps track of which users are online.
- Parameters
username – The name of the user to log in.
passhash – The hashed password of the user to log in.
- Returns
User document if succeeded, None if failed.
-
logout_user(username)[source]¶ Log out a user.
- Parameters
username – The name of the user to log out.
- Returns
True.
-
online(username)[source]¶ Check if a user is online.
- Parameters
username – The name of the user to check.
- Returns
True if online, False if offline.
-
room_by_id(roomid)[source]¶ Get a room by its id.
- Parameters
roomid – The id of the room to retrieve from the database.
- Returns
Room document or None.
-
upsert_item(document)[source]¶ Update or insert an item.
- Parameters
document – The item document to update or insert.
- Returns
True
-
upsert_room(document)[source]¶ Update or insert a room.
- Parameters
document – The room document to update or insert.
- Returns
True
-
upsert_user(document)[source]¶ Update or insert a user.
- Parameters
document – The user document to update or insert.
- Returns
True
-
user_by_name(username)[source]¶ Get a user by their name.
If there is any chance the user could be logged in, and the record needs to be altered, you should use the equivalent Console method. This method is faster but unsafe for logged in users.
- Parameters
username – The name of the user to retrieve from the database.
- Returns
User document or None.
-
user_by_nick(nickname)[source]¶ Get a user by their nickname.
If there is any chance the user could be logged in, and the record needs to be altered, you should use the equivalent Console method. This method is faster but unsafe for logged in users.
- Parameters
nickname – The nickname of the user to retrieve from the database.
- Returns
User document or None.