mud_server.db.database
Database initialization and management for the MUD server.
This module provides all database operations for the MUD server using SQLite. It handles: - Database schema initialization - User account management (create, authentication, roles) - Character management (creation, locations, inventory) - Session tracking (login/logout, active users) - Chat message storage and retrieval
- Database Design:
Tables: - users: Account identities (login, role, status) - characters: World-facing personas owned by users - character_locations: Per-character room state - sessions: Active login sessions with activity tracking - chat_messages: All chat messages with room and recipient info
- Security Considerations:
Passwords hashed with bcrypt (never plain text)
Email stored as hashed value only (privacy-first)
SQL injection prevented using parameterized queries
Session IDs are UUIDs (hard to guess)
- Performance Notes:
SQLite handles basic concurrency (~50-100 players)
No connection pooling (single file database)
Suitable for small-medium deployments
Attributes
Functions
|
Initialize the SQLite database with required tables. |
|
Public wrapper for resolving character names from usernames or character names. |
Create a new SQLite connection to the database file. |
|
|
Create a new user account and (optionally) a default character. |
|
Create a character for an existing user. |
|
Return True if a user account exists. |
|
Return user id for the given username, or None if not found. |
|
Return username for a user id, or None if not found. |
|
Return the role for a username, or None if not found. |
|
Return account_origin for the given username. |
|
Update a user's role. |
|
Verify a password against stored bcrypt hash. |
|
Return True if the user is active (not banned). |
|
Deactivate (ban) a user account. |
|
Activate (unban) a user account. |
|
Change a user's password (hashes with bcrypt). |
|
Tombstone a user account without deleting rows. |
|
Delete a user account while preserving character data. |
|
Return True if a character with this name exists. |
|
Return character row by name. |
|
Return character row by id. |
|
Return character name for the given id, or None if not found. |
|
Return all characters owned by the given user for a world. |
|
Return the set of world ids in which the user has characters. |
|
Detach characters from a user (used when tombstoning guest accounts). |
|
Return the current room for a character by name within a world. |
|
Set the current room for a character by name within a world. |
|
Return character names in a room with active sessions for a world. |
|
Return the character inventory as a list of item ids. |
|
Set the character inventory. |
|
Add a chat message for a character. |
|
Get recent messages from a room. Filters whispers based on character. |
|
Create a new session record for a user. |
|
Attach a character (and optionally world) to an existing session. |
|
Remove a specific session by its session_id. |
|
Remove all sessions for a user (used for forced logout/ban). |
|
Update last_activity for a session and extend expiry when sliding is enabled. |
|
Return session record by session_id (or None if not found). |
Count active sessions within the configured activity window. |
|
Remove expired sessions based on expires_at timestamp. |
|
Remove all sessions from the database. |
|
|
Return character names with active sessions for a world. |
Delete expired guest accounts and unlink their characters. |
|
|
Return a world catalog entry by id. |
|
Return all worlds in the catalog. |
|
Return worlds accessible to the given user. |
Return a sorted list of user-defined table names (excludes sqlite_*). |
|
Return table metadata for admin database browsing. |
|
|
Return column names and rows for a given table. |
Return detailed user list for admin database viewer. |
|
Return basic user list for admin summaries. |
|
|
Return character location rows with names for admin display. |
|
Return all active (non-expired) sessions. |
|
Return active sessions with activity age in seconds. |
|
Return recent chat messages across all rooms. |
|
Backward-compatible alias for user_exists(). |
|
Backward-compatible alias for get_user_role(). |
|
Backward-compatible alias for get_user_account_origin(). |
|
Backward-compatible alias for set_user_role(). |
|
Backward-compatible alias for is_user_active(). |
|
Backward-compatible alias for deactivate_user(). |
|
Backward-compatible alias for activate_user(). |
|
Backward-compatible alias for create_user_with_password(). |
|
Backward-compatible alias for get_character_room(). |
|
Backward-compatible alias for set_character_room(). |
|
Backward-compatible alias for get_character_inventory(). |
|
Backward-compatible alias for set_character_inventory(). |
Backward-compatible alias for get_active_characters(). |
|
|
Backward-compatible alias for get_characters_in_room(). |
Backward-compatible alias for get_all_users_detailed(). |
|
Backward-compatible alias for get_all_users(). |
|
Backward-compatible alias for get_character_locations(). |
|
|
Backward-compatible alias for delete_user(). |
|
Backward-compatible alias for cleanup_expired_guest_accounts(). |
|
Backward-compatible alias for removing sessions by username. |
Module Contents
- mud_server.db.database.DEFAULT_WORLD_ID = 'pipeworks_web'
- mud_server.db.database.init_database(*, skip_superuser=False)[source]
Initialize the SQLite database with required tables.
Creates all necessary tables if they don’t exist. If MUD_ADMIN_USER and MUD_ADMIN_PASSWORD environment variables are set and no users exist, creates a superuser with those credentials (unless skip_superuser=True).
- Parameters:
skip_superuser (bool) – If True, skip superuser creation from env vars.
- Side Effects:
Creates data/mud.db file if it doesn’t exist
Creates tables if they don’t exist
Creates superuser if env vars set and no users exist
- mud_server.db.database.resolve_character_name(name, *, world_id=None)[source]
Public wrapper for resolving character names from usernames or character names.
This preserves legacy call sites that still supply usernames while the character model is being adopted across the codebase.
- mud_server.db.database.get_connection()[source]
Create a new SQLite connection to the database file.
- Returns:
sqlite3.Connection object
- Return type:
- mud_server.db.database.create_user_with_password(username, password, *, role='player', account_origin='legacy', email_hash=None, is_guest=False, guest_expires_at=None, create_default_character=True, world_id=DEFAULT_WORLD_ID)[source]
Create a new user account and (optionally) a default character.
- Parameters:
username (str) – Unique account username.
password (str) – Plain text password (hashed with bcrypt).
role (str) – Role string.
account_origin (str) – Provenance marker for cleanup/auditing.
email_hash (str | None) – Hashed email value (nullable during development).
is_guest (bool) – Whether this is a guest account.
guest_expires_at (str | None) – Expiration timestamp for guest accounts.
create_default_character (bool) – If True, create a character with the same name.
world_id (str) – World to bind the default character to.
- Returns:
True if created successfully, False if username already exists.
- Return type:
- mud_server.db.database.create_character_for_user(user_id, name, *, is_guest_created=False, room_id='spawn', world_id=DEFAULT_WORLD_ID)[source]
Create a character for an existing user.
- Parameters:
- Returns:
True if character created, False on constraint violation.
- Return type:
- mud_server.db.database.get_user_id(username)[source]
Return user id for the given username, or None if not found.
- mud_server.db.database.get_username_by_id(user_id)[source]
Return username for a user id, or None if not found.
- mud_server.db.database.get_user_role(username)[source]
Return the role for a username, or None if not found.
- mud_server.db.database.get_user_account_origin(username)[source]
Return account_origin for the given username.
- mud_server.db.database.verify_password_for_user(username, password)[source]
Verify a password against stored bcrypt hash.
Uses a dummy hash for timing safety when user doesn’t exist.
- mud_server.db.database.is_user_active(username)[source]
Return True if the user is active (not banned).
- mud_server.db.database.change_password_for_user(username, new_password)[source]
Change a user’s password (hashes with bcrypt).
- mud_server.db.database.tombstone_user(user_id)[source]
Tombstone a user account without deleting rows.
- mud_server.db.database.delete_user(username)[source]
Delete a user account while preserving character data.
- This performs:
Unlink characters from the user (user_id -> NULL)
Remove all sessions
Tombstone the user row (soft delete)
- mud_server.db.database.character_exists(name)[source]
Return True if a character with this name exists.
- mud_server.db.database.get_character_name_by_id(character_id)[source]
Return character name for the given id, or None if not found.
- mud_server.db.database.get_user_characters(user_id, *, world_id=None)[source]
Return all characters owned by the given user for a world.
When world_id is omitted, the default world is used to keep legacy code paths functional during the migration.
- mud_server.db.database.get_user_character_world_ids(user_id)[source]
Return the set of world ids in which the user has characters.
This is used to enforce allow_multi_world_characters when creating new characters.
- mud_server.db.database.unlink_characters_for_user(user_id)[source]
Detach characters from a user (used when tombstoning guest accounts).
- mud_server.db.database.get_character_room(name, *, world_id=None)[source]
Return the current room for a character by name within a world.
- mud_server.db.database.set_character_room(name, room, *, world_id=None)[source]
Set the current room for a character by name within a world.
- mud_server.db.database.get_characters_in_room(room, *, world_id=None)[source]
Return character names in a room with active sessions for a world.
- mud_server.db.database.get_character_inventory(name)[source]
Return the character inventory as a list of item ids.
- mud_server.db.database.set_character_inventory(name, inventory)[source]
Set the character inventory.
- mud_server.db.database.add_chat_message(character_name, message, room, recipient_character_name=None, recipient=None, *, world_id=None)[source]
Add a chat message for a character.
Supports optional whisper recipient and uses world scoping. If world_id is omitted, the default world is used during migration.
- mud_server.db.database.get_room_messages(room, *, limit=50, character_name=None, username=None, world_id=None)[source]
Get recent messages from a room. Filters whispers based on character.
Messages are scoped to the provided world_id; default world is used when omitted to preserve legacy code paths during migration.
- mud_server.db.database.create_session(user_id, session_id, *, client_type='unknown', character_id=None, world_id=None)[source]
Create a new session record for a user.
- Behavior depends on configuration:
allow_multiple_sessions = False: remove existing sessions for the user
allow_multiple_sessions = True: keep existing sessions
If world_id is omitted, the default world is used. This is temporary compatibility behavior until the API layer is updated to provide it.
- mud_server.db.database.set_session_character(session_id, character_id, *, world_id=None)[source]
Attach a character (and optionally world) to an existing session.
- mud_server.db.database.remove_session_by_id(session_id)[source]
Remove a specific session by its session_id.
- mud_server.db.database.remove_sessions_for_user(user_id)[source]
Remove all sessions for a user (used for forced logout/ban).
- mud_server.db.database.update_session_activity(session_id)[source]
Update last_activity for a session and extend expiry when sliding is enabled.
- mud_server.db.database.get_session_by_id(session_id)[source]
Return session record by session_id (or None if not found).
- mud_server.db.database.get_active_session_count()[source]
Count active sessions within the configured activity window.
- mud_server.db.database.cleanup_expired_sessions()[source]
Remove expired sessions based on expires_at timestamp.
- mud_server.db.database.get_active_characters(*, world_id=None)[source]
Return character names with active sessions for a world.
- mud_server.db.database.cleanup_expired_guest_accounts()[source]
Delete expired guest accounts and unlink their characters.
- Returns:
Number of guest users deleted.
- Return type:
- mud_server.db.database.list_worlds(*, include_inactive=False)[source]
Return all worlds in the catalog.
- Parameters:
include_inactive (bool) – When False, only active worlds are returned.
- mud_server.db.database.list_worlds_for_user(user_id, *, role=None, include_inactive=False)[source]
Return worlds accessible to the given user.
Admins and superusers have implicit access to all worlds. Other roles must have a world_permissions row with can_access=1.
- mud_server.db.database.get_table_names()[source]
Return a sorted list of user-defined table names (excludes sqlite_*).
- mud_server.db.database.get_table_rows(table_name, limit=100)[source]
Return column names and rows for a given table.
- mud_server.db.database.get_all_users_detailed()[source]
Return detailed user list for admin database viewer.
- mud_server.db.database.get_character_locations(*, world_id=None)[source]
Return character location rows with names for admin display.
- mud_server.db.database.get_all_sessions(*, world_id=None)[source]
Return all active (non-expired) sessions.
- mud_server.db.database.get_active_connections(*, world_id=None)[source]
Return active sessions with activity age in seconds.
- mud_server.db.database.get_all_chat_messages(limit=100, *, world_id=None)[source]
Return recent chat messages across all rooms.
- mud_server.db.database.player_exists(username)[source]
Backward-compatible alias for user_exists().
- mud_server.db.database.get_player_role(username)[source]
Backward-compatible alias for get_user_role().
- mud_server.db.database.get_player_account_origin(username)[source]
Backward-compatible alias for get_user_account_origin().
- mud_server.db.database.set_player_role(username, role)[source]
Backward-compatible alias for set_user_role().
- mud_server.db.database.is_player_active(username)[source]
Backward-compatible alias for is_user_active().
- mud_server.db.database.deactivate_player(username)[source]
Backward-compatible alias for deactivate_user().
- mud_server.db.database.activate_player(username)[source]
Backward-compatible alias for activate_user().
- mud_server.db.database.create_player_with_password(username, password, role='player', account_origin='legacy')[source]
Backward-compatible alias for create_user_with_password().
- mud_server.db.database.get_player_room(username)[source]
Backward-compatible alias for get_character_room().
- mud_server.db.database.set_player_room(username, room)[source]
Backward-compatible alias for set_character_room().
- mud_server.db.database.get_player_inventory(username)[source]
Backward-compatible alias for get_character_inventory().
- mud_server.db.database.set_player_inventory(username, inventory)[source]
Backward-compatible alias for set_character_inventory().
- mud_server.db.database.get_active_players()[source]
Backward-compatible alias for get_active_characters().
- mud_server.db.database.get_players_in_room(room)[source]
Backward-compatible alias for get_characters_in_room().
- mud_server.db.database.get_all_players_detailed()[source]
Backward-compatible alias for get_all_users_detailed().
- mud_server.db.database.get_player_locations()[source]
Backward-compatible alias for get_character_locations().
- mud_server.db.database.delete_player(username)[source]
Backward-compatible alias for delete_user().