feat: compare mood presets in the UI
Add GET /api/compare with shared/unique games and Jaccard, plus a preset comparison panel on the recommendations tab.
This commit is contained in:
1 parent
9943081baa
commit
02af4bde09
10 files changed
+198
-3
No files matched your search
@@ -1003,6 +1003,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self._hltb_status(qs)
|
||||
elif path == "/api/moods":
|
||||
self._moods()
|
||||
elif path == "/api/compare":
|
||||
self._compare(qs)
|
||||
elif path == "/api/moods/export":
|
||||
self._export_moods(qs)
|
||||
elif path == "/api/descriptions":
|
||||
@@ -1326,6 +1328,45 @@ class Handler(BaseHTTPRequestHandler):
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def _compare(self, qs):
|
||||
a = (qs.get("a", [""])[0] or "").strip()
|
||||
b = (qs.get("b", [""])[0] or "").strip()
|
||||
if not a or not b:
|
||||
self._error(400, "two presets required")
|
||||
return
|
||||
try:
|
||||
limit = max(1, min(100, int(qs.get("limit", ["20"])[0])))
|
||||
except (TypeError, ValueError):
|
||||
limit = 20
|
||||
con = connect()
|
||||
try:
|
||||
db.seed_moods(con)
|
||||
try:
|
||||
mood_a = reco.load_mood(con, preset=a)
|
||||
mood_b = reco.load_mood(con, preset=b)
|
||||
except KeyError:
|
||||
self._error(404, "unknown preset")
|
||||
return
|
||||
_, results_a = reco.recommend(con, mood_a, limit=limit)
|
||||
_, results_b = reco.recommend(con, mood_b, limit=limit)
|
||||
set_a = {g["appid"] for g in results_a}
|
||||
set_b = {g["appid"] for g in results_b}
|
||||
common_ids = set_a & set_b
|
||||
union = set_a | set_b
|
||||
|
||||
def brief(game):
|
||||
return {"appid": game["appid"], "name": game["name"], "score": game["score"]}
|
||||
|
||||
self._json({
|
||||
"a": a, "b": b, "limit": limit,
|
||||
"jaccard": round(len(common_ids) / len(union), 4) if union else 0.0,
|
||||
"common": [brief(g) for g in results_a if g["appid"] in common_ids],
|
||||
"only_a": [brief(g) for g in results_a if g["appid"] not in set_b],
|
||||
"only_b": [brief(g) for g in results_b if g["appid"] not in set_a],
|
||||
})
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def _save_mood(self, body):
|
||||
try:
|
||||
mood = normalize_mood_payload(body)
|
||||
|
||||
Reference in new issue
Block a user