feat: localize tags and genres; improve search, duration and sorting
CI / test (push) Successful in 27s

This commit is contained in:
strsv committed 2026-11-09 08:13:00 +03:00
1 parent ea3074e0a9
commit be4fbf6864
31 files changed
+1126 -145

No files matched your search

+32 -10
View File
@@ -254,14 +254,29 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
raise ValueError(f"unknown taste profile: {taste_profile}")
context_appids = taste_profile_appids(con, taste_profile) if taste_profile else None
tag_names = {r["tag_id"]: r["name"] for r in con.execute("SELECT tag_id, name FROM tag_names")}
tag_names = {}
tag_labels = {}
for r in con.execute("SELECT tag_id, name, name_ru FROM tag_names"):
tag_names[r["tag_id"]] = r["name"]
tag_labels[r["name"]] = r["name_ru"] or r["name"]
genre_labels = {r["name"]: r["name_ru"] or r["name"]
for r in con.execute("SELECT name, name_ru FROM genre_names")}
name_to_ids = {}
for tid, name in tag_names.items():
name_to_ids.setdefault(name, []).append(tid)
def display_mood(source):
result = dict(source)
result["tags"] = {tag_labels.get(name, name): weight
for name, weight in (source.get("tags") or {}).items()}
result["genres"] = [genre_labels.get(name, name)
for name in (source.get("genres") or [])]
return result
rows = con.execute("""
SELECT g.appid, g.name, g.metacritic, g.review_pct, g.release_date,
g.estimated_hours,
g.hltb_main, g.hltb_extra, g.hltb_complete,
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
@@ -294,7 +309,7 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
ids = [r["appid"] for r in rows]
if not ids:
return mood, []
return display_mood(mood), []
placeholders = ",".join("?" * len(ids))
game_tags = {i: set() for i in ids}
@@ -398,25 +413,29 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
if recency_reason:
reasons.append(recency_reason)
if matched:
reasons.append("Теги: " + ", ".join(matched[:3]))
reasons.append("Теги: " + ", ".join(tag_labels.get(n, n) for n in matched[:3]))
if r["minutes"] == 0:
reasons.append("Ещё не запускали")
elif r["installed"]:
reasons.append("Установлена")
if hit_genres:
reasons.append("Жанры: " + ", ".join(sorted(hit_genres)))
reasons.append("Жанры: " + ", ".join(
sorted(genre_labels.get(g, g) for g in hit_genres)))
if r["review_pct"] is not None and r["review_pct"] >= 85:
reasons.append(f"Отзывы {r['review_pct']}%")
results.append({
"appid": r["appid"],
"name": r["name"],
"score": round(score, 1),
"matched": matched[:6],
"genres": sorted(hit_genres) or sorted(game_genres.get(r["appid"], []))[:3],
"matched": [tag_labels.get(n, n) for n in matched[:6]],
"genres": [genre_labels.get(g, g) for g in
(sorted(hit_genres) or sorted(game_genres.get(r["appid"], []))[:3])],
"review": r["review_pct"],
"metacritic": r["metacritic"],
"hours": round(r["minutes"] / 60, 1),
"duration": r["estimated_hours"],
"duration": r["hltb_main"] or r["estimated_hours"],
"hltb": {"main": r["hltb_main"], "extra": r["hltb_extra"],
"complete": r["hltb_complete"]} if r["hltb_main"] else None,
"installed": bool(r["installed"]),
"status": r["status"],
"rating": r["rating"],
@@ -426,7 +445,7 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
"feedback": feedback.get(r["appid"]),
"reasons": reasons[:3],
})
return mood, diversify(results, result_tags, limit)
return display_mood(mood), diversify(results, result_tags, limit)
def similar_games(con, appid, limit=6):
@@ -461,6 +480,8 @@ def similar_games(con, appid, limit=6):
genre_map = {i: set() for i in ids}
for r in con.execute(f"SELECT appid, genre FROM game_genres WHERE appid IN ({ph})", ids):
genre_map[r["appid"]].add(r["genre"])
genre_labels = {r["name"]: r["name_ru"] or r["name"]
for r in con.execute("SELECT name, name_ru FROM genre_names")}
target_tags = tag_map.get(appid, set())
if not target_tags:
@@ -513,7 +534,7 @@ def similar_games(con, appid, limit=6):
"metacritic": r["metacritic"],
"release": r["release_date"],
"year": year,
"genres": sorted(genres)[:3],
"genres": [genre_labels.get(g, g) for g in sorted(genres)[:3]],
"matched": [],
"hours": round((r["minutes"] or 0) / 60, 1),
"installed": bool(r["installed"]),
@@ -530,7 +551,8 @@ def similar_games(con, appid, limit=6):
if wanted:
ph2 = ",".join("?" * len(wanted))
names = {r["tag_id"]: r["name"] for r in con.execute(
f"SELECT tag_id, name FROM tag_names WHERE tag_id IN ({ph2})", list(wanted))}
f"SELECT tag_id, COALESCE(name_ru, name) AS name FROM tag_names "
f"WHERE tag_id IN ({ph2})", list(wanted))}
for g in top:
common = target_tags & tag_map.get(g["appid"], set())
g["matched"] = [names[t] for t in common if t in names][:5]