agent_m/agentm/logic/db_functions.py
mscrnt 5179d425fc Add initial project structure and core functionality for Agent M
- Created .gitignore to exclude virtual environment, logs, and database files.
- Updated README.md with project description, features, folder structure, requirements, and usage instructions.
- Implemented versioning and developer ID in agentm/__init__.py.
- Developed main application logic in agentm/app.py, including credential handling and screen navigation.
- Added database initialization and ROM management logic in agentm/logic/db.py and agentm/logic/db_functions.py.
- Integrated DIAMBRA login functionality in agentm/logic/diambra_login.py.
- Created ROM verification and caching system in agentm/logic/roms.py.
- Designed user interface components for home and login screens in agentm/views/home.py and agentm/views/login.py.
- Added logging utility in agentm/utils/logger.py for better debugging and tracking.
- Included assets such as game images, styles, and logos.
- Updated requirements.txt with necessary dependencies for the project.
2025-05-20 23:20:29 -07:00

121 lines
3.4 KiB
Python

import json
from datetime import datetime
from agentm.logic.db import get_db_conn
def get_cached_rom(rom_file: str) -> dict | None:
"""
Retrieve verified ROM metadata from the database by ROM filename.
Args:
rom_file: The filename of the ROM (e.g., 'sfiii3n.zip').
Returns:
A dictionary of ROM metadata if verified, otherwise None.
"""
with get_db_conn() as conn:
cur = conn.execute("""
SELECT sha256, verified_at, title, game_id,
difficulty_min, difficulty_max, characters, keywords
FROM roms WHERE rom_file = ? AND verified = 1
""", (rom_file,))
row = cur.fetchone()
if row:
return {
"sha256": row[0],
"verified": True,
"verified_at": row[1],
"title": row[2],
"rom_file": rom_file,
"game_id": row[3],
"difficulty_min": row[4],
"difficulty_max": row[5],
"characters": json.loads(row[6]) if row[6] else [],
"keywords": json.loads(row[7]) if row[7] else [],
}
return None
def get_all_verified_roms() -> list[dict]:
"""
Return a list of all verified ROMs as dictionaries.
Returns:
A list of dictionaries containing ROM metadata.
"""
with get_db_conn() as conn:
cur = conn.execute("""
SELECT sha256, verified_at, title, game_id, rom_file,
difficulty_min, difficulty_max, characters, keywords
FROM roms WHERE verified = 1
ORDER BY title ASC
""")
rows = cur.fetchall()
return [
{
"sha256": row[0],
"verified": True,
"verified_at": row[1],
"title": row[2],
"game_id": row[3],
"rom_file": row[4],
"difficulty_min": row[5],
"difficulty_max": row[6],
"characters": json.loads(row[7]) if row[7] else [],
"keywords": json.loads(row[8]) if row[8] else [],
}
for row in rows
]
def upsert_rom_record(
title: str,
rom_file: str,
game_id: str,
sha256: str,
difficulty_min: int = None,
difficulty_max: int = None,
characters: list[str] = None,
keywords: list[str] = None
):
"""
Insert or replace a verified ROM entry in the database.
Args:
title: Game title.
rom_file: ROM file name.
game_id: Game ID used by DIAMBRA.
sha256: SHA256 checksum of the ROM.
difficulty_min: Minimum difficulty.
difficulty_max: Maximum difficulty.
characters: List of characters.
keywords: List of keywords.
"""
with get_db_conn() as conn:
conn.execute("""
INSERT OR REPLACE INTO roms (
title,
rom_file,
game_id,
sha256,
difficulty_min,
difficulty_max,
characters,
keywords,
verified,
verified_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)
""", (
title,
rom_file,
game_id,
sha256,
difficulty_min,
difficulty_max,
json.dumps(characters or []),
json.dumps(keywords or []),
datetime.utcnow().isoformat()
))