feat: estimate game duration from genres and tags
CI / test (push) Successful in 15s

This commit is contained in:
strsv committed 2026-11-06 17:21:21 +03:00
1 parent b65c8c39fe
commit b2ab347129
18 files changed
+166 -25

No files matched your search

+71 -1
View File
@@ -8,7 +8,7 @@ from datetime import datetime, timezone
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
SCHEMA_VERSION = 3
SCHEMA_VERSION = 4
MOOD_PRESETS = {
"adventure-rpg-party": {
@@ -329,6 +329,69 @@ def _migration_2(con):
refresh_search_index(con)
GENRE_HOURS = {
"Massively Multiplayer": 80,
"RPG": 30,
"Strategy": 25,
"Simulation": 25,
"Adventure": 12,
"Action": 12,
"Racing": 10,
"Sports": 10,
"Free To Play": 10,
"Indie": 8,
"Casual": 6,
}
TAG_HOURS = {
"MMORPG": 80,
"JRPG": 40,
"Open World": 35,
"Colony Sim": 30,
"City Builder": 28,
"Management": 25,
"Farming Sim": 25,
"Survival": 25,
"Roguelike": 20,
"Sandbox": 20,
"Dungeon Crawler": 15,
"Roguelite": 15,
"Visual Novel": 12,
"Point & Click": 8,
"Puzzle": 8,
"Relaxing": 8,
"Walking Simulator": 6,
"Casual": 6,
"Short": 5,
}
def estimate_hours(genres, tag_names):
"""Rough playtime estimate from genres and Steam tags (no HLTB)."""
hours = max([GENRE_HOURS.get(genre, 0) for genre in genres] or [12])
hours = max([hours, *[TAG_HOURS.get(tag, 0) for tag in tag_names]])
if "Short" in tag_names:
hours = min(hours, TAG_HOURS["Short"])
return round(max(2.0, min(100.0, hours)), 1)
def refresh_duration_estimates(con):
"""Recompute games.estimated_hours for all games."""
genres = {}
for r in con.execute("SELECT appid, genre FROM game_genres"):
genres.setdefault(r["appid"], set()).add(r["genre"])
tags = {}
for r in con.execute("""SELECT gt.appid, tn.name
FROM game_tags gt JOIN tag_names tn ON tn.tag_id = gt.tag_id"""):
tags.setdefault(r["appid"], set()).add(r["name"])
updates = [
(estimate_hours(genres.get(r["appid"], set()), tags.get(r["appid"], set())), r["appid"])
for r in con.execute("SELECT appid FROM games WHERE LOWER(type) = 'game'")
]
con.executemany("UPDATE games SET estimated_hours = ? WHERE appid = ?", updates)
con.commit()
def _migration_3(con):
"""Add per-mood recommendation feedback."""
con.execute("""
@@ -344,10 +407,17 @@ def _migration_3(con):
"ON reco_feedback(mood_key, verdict)")
def _migration_4(con):
"""Add estimated_hours and populate it for existing games."""
_ensure_column(con, "games", "estimated_hours", "REAL")
refresh_duration_estimates(con)
MIGRATIONS = {
1: _migration_1,
2: _migration_2,
3: _migration_3,
4: _migration_4,
}
+1
View File
@@ -190,6 +190,7 @@ def main():
con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('steamid', ?)", (steamid,))
db.refresh_search_index(con)
db.refresh_duration_estimates(con)
counts = {r["type"]: r["n"] for r in con.execute(
"SELECT COALESCE(LOWER(type), 'unknown') AS type, COUNT(*) AS n FROM games GROUP BY 1")}
+2
View File
@@ -153,6 +153,7 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
rows = con.execute("""
SELECT g.appid, g.name, g.metacritic, g.review_pct, g.release_date,
g.estimated_hours,
COALESCE(p.minutes, 0) AS minutes, p.last_played,
COALESCE(i.installed, 0) AS installed,
COALESCE(pe.status, 'backlog') AS status, pe.rating, pe.favorite, pe.notes
@@ -274,6 +275,7 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
"review": r["review_pct"],
"metacritic": r["metacritic"],
"hours": round(r["minutes"] / 60, 1),
"duration": r["estimated_hours"],
"installed": bool(r["installed"]),
"status": r["status"],
"rating": r["rating"],
+5 -4
View File
@@ -10,10 +10,11 @@ CREATE TABLE IF NOT EXISTS games (
appid INTEGER PRIMARY KEY,
name TEXT NOT NULL,
type TEXT,
release_date TEXT,
metacritic INTEGER,
review_pct INTEGER,
updated_at TEXT
release_date TEXT,
metacritic INTEGER,
review_pct INTEGER,
estimated_hours REAL,
updated_at TEXT
);
CREATE TABLE IF NOT EXISTS game_genres (