Added RPG

This commit is contained in:
2026-05-28 14:29:43 +03:00
parent e5c0df308f
commit 87699172de
20 changed files with 1268 additions and 22 deletions
+111
View File
@@ -36,6 +36,17 @@ async def get_all_sessions() -> list:
return [dict(r) for r in rows]
async def get_session(session_id: str) -> dict | None:
async with aiosqlite.connect(DB_PATH) as db:
db.row_factory = aiosqlite.Row
async with db.execute(
"SELECT * FROM sessions WHERE session_id = ?",
(session_id,),
) as cursor:
row = await cursor.fetchone()
return dict(row) if row else None
async def update_session_title(session_id: str, title: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
@@ -47,13 +58,113 @@ async def update_session_title(session_id: str, title: str):
async def update_session_persona(session_id: str, persona_id: str):
async with aiosqlite.connect(DB_PATH) as db:
db.row_factory = aiosqlite.Row
async with db.execute(
"SELECT persona_id FROM sessions WHERE session_id = ?",
(session_id,),
) as cur:
row = await cur.fetchone()
prev = row["persona_id"] if row else None
await db.execute(
"UPDATE sessions SET persona_id = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(persona_id, session_id),
)
# If persona changed, reset RPG state bound to the persona/arc.
if prev is not None and prev != persona_id:
await db.execute(
"""UPDATE sessions
SET facts_json = '[]',
global_plot = '',
status_quo = '',
plot_arc_json = '{}'
WHERE session_id = ?""",
(session_id,),
)
await db.execute(
"DELETE FROM action_resolutions WHERE session_id = ?",
(session_id,),
)
await db.commit()
async def update_session_rpg(session_id: str, rpg_enabled: bool):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"UPDATE sessions SET rpg_enabled = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(1 if rpg_enabled else 0, session_id),
)
await db.commit()
async def update_session_facts(session_id: str, facts_json: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"UPDATE sessions SET facts_json = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(facts_json, session_id),
)
await db.commit()
async def update_session_global_plot(session_id: str, global_plot: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"UPDATE sessions SET global_plot = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(global_plot, session_id),
)
await db.commit()
async def update_session_status_quo(session_id: str, status_quo: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"UPDATE sessions SET status_quo = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(status_quo, session_id),
)
await db.commit()
async def update_session_plot_arc(session_id: str, plot_arc_json: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"UPDATE sessions SET plot_arc_json = ?, updated_at = CURRENT_TIMESTAMP WHERE session_id = ?",
(plot_arc_json, session_id),
)
await db.commit()
async def add_action_resolution(
session_id: str,
intent_text: str,
roll: int,
outcome: str,
resolution_text: str,
message_id: int | None = None,
):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"""INSERT INTO action_resolutions
(session_id, message_id, intent_text, roll, outcome, resolution_text)
VALUES (?, ?, ?, ?, ?, ?)""",
(session_id, message_id, intent_text, roll, outcome, resolution_text),
)
await db.commit()
async def get_last_action_resolution(session_id: str) -> dict | None:
async with aiosqlite.connect(DB_PATH) as db:
db.row_factory = aiosqlite.Row
async with db.execute(
"""SELECT * FROM action_resolutions
WHERE session_id = ?
ORDER BY id DESC LIMIT 1""",
(session_id,),
) as cur:
row = await cur.fetchone()
return dict(row) if row else None
async def delete_session(session_id: str):
async with aiosqlite.connect(DB_PATH) as db:
await db.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))