v1.3 看得见:三色错题本(红/黄/绿+待做视图) + 收尾页(表扬行为) + 小狮落库(pet_stage/pet_fed_at,247 测试)

This commit is contained in:
2026-11-23 21:31:11 +08:00
parent 2f81e07b7d
commit 53699b4e42
12 changed files with 435 additions and 24 deletions
+44
View File
@@ -653,6 +653,7 @@ def list_children(conn, gid: str) -> list[dict]:
for r in rows:
d = dict(r)
d["subjects"] = _subjects(conn, r["id"])
d["pet"] = _pet_sync(conn, r["id"])
out.append(d)
return out
@@ -663,6 +664,7 @@ def get_child(conn, gid: str, cid: str) -> dict:
_need(row, "NO_CHILD", "没有这个孩子")
d = dict(row)
d["subjects"] = _subjects(conn, cid)
d["pet"] = _pet_sync(conn, cid)
return d
@@ -908,6 +910,46 @@ def today_queue(conn, gid: str, cid: str, subject: str | None = None) -> dict:
}
PET_STAGES = (12, 40, 100) # 已毕业 12 / 40 / 100 道,形态升一级
def pet_stage_of(graduated: int) -> int:
return 3 if graduated >= PET_STAGES[2] else 2 if graduated >= PET_STAGES[1] else 1 if graduated >= PET_STAGES[0] else 0
def _pet_sync(conn, cid: str) -> dict:
"""把「已吃掉几道」和「今天喂过没有」写进 children,换设备也是同一只。
只在当天该做的题全部做完时算「喂过」——不是打卡,是把今天这轮跑完。
"""
graduated = conn.execute(
"SELECT COUNT(*) FROM mistakes WHERE child_id=? AND status='graduated'", (cid,)).fetchone()[0]
left = conn.execute(
"SELECT COUNT(*) FROM mistakes WHERE child_id=? AND status IN"
" ('observing','queued','passed_once','unstable') AND next_due<=?", (cid, today())).fetchone()[0]
stage = pet_stage_of(graduated)
# 「喂过」= 今天该做的题都做完了,而且今天确实动过手。
# 光看 left==0 会把「今天没事干」也算成喂过,刚建号的孩子就白送一次。
day = today()
touched = conn.execute(
"SELECT 1 FROM attempts WHERE child_id=? AND substr(created_at,1,10)=? LIMIT 1",
(cid, day)).fetchone() is not None
fed = left == 0 and touched
row = conn.execute("SELECT pet_stage, pet_fed_at FROM children WHERE id=?", (cid,)).fetchone()
cur_stage = (row["pet_stage"] if row else None) or 0
cur_fed = row["pet_fed_at"] if row else None
# 只在真的变了才写:get_child 会在读接口里被调用,别让 GET 变成天天 UPDATE
if cur_stage != stage or (fed and cur_fed != day):
if fed:
conn.execute("UPDATE children SET pet_stage=?, pet_fed_at=? WHERE id=?", (stage, day, cid))
cur_fed = day
else:
conn.execute("UPDATE children SET pet_stage=? WHERE id=?", (stage, cid))
cur_stage = stage
return {"stage": cur_stage, "fed_at": cur_fed, "fed_today": cur_fed == day,
"graduated": graduated, "left_today": left}
def attempt(conn, gid: str, cid: str, mid: str, answer_text: str, cost_ms: int | None = None) -> dict:
get_child(conn, gid, cid)
m = conn.execute("SELECT * FROM mistakes WHERE id=? AND child_id=?", (mid, cid)).fetchone()
@@ -930,8 +972,10 @@ def attempt(conn, gid: str, cid: str, mid: str, answer_text: str, cost_ms: int |
(new_id("a"), mid, cid, answer_text, int(correct), cost_ms, now()))
_apply(conn, mid, state)
log_event(conn, "attempt", cid, correct=correct, status=state["status"])
pet = _pet_sync(conn, cid)
return {
"mistake_id": mid,
"pet": pet,
"correct": correct,
"detail": detail,
# 答完才能给答案:讲解要照着它生成步骤,之前不给是为了不让孩子提前看