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
+11
View File
@@ -618,3 +618,14 @@ python tools/backfill_kp.py --apply # 真写
调用成本:预览与正式入库调的是同一个 `recognize_block`,裁剪图按 sha1 命中
`data/ocr_cache/`,所以预览这一遍**不额外花钱**(除非把 `OCR_CACHE=0` 关了)。
### 四点二十七、小狮落库:跨设备一致
`children` 表加了两列:`pet_stage`(0/1/2/3,按已毕业数 12 / 40 / 100 分档)和 `pet_fed_at`(最后喂食日期)。
- `services._pet_sync(conn, cid)` 现算:`graduated` = 已毕业数,`left_today` = 今天到期且未毕业的题数。
- `fed_today` 的条件是 **`left_today == 0` 且今天真有一道题做过**——不是打卡,也不是打开页面就算。
新号没有做题记录时不给「喂过」,否则白送一次。
- 只有 `stage` 变了、或今天第一次喂,才 `UPDATE`;`GET /api/children` 不会天天写库。
- `get_child()` / `list_children()` / `attempt()` 的返回体都带 `pet: {stage, fed_at, fed_today, graduated, left_today}`,
前端优先用服务端的 `stage`(演示模式才本地算)。
+5 -1
View File
@@ -46,7 +46,9 @@ CREATE TABLE IF NOT EXISTS children (
grade INTEGER NOT NULL CHECK (grade BETWEEN 1 AND 6),
remind_at TEXT NOT NULL DEFAULT '19:30',
created_at TEXT NOT NULL,
deleted_at TEXT
deleted_at TEXT,
pet_stage INTEGER NOT NULL DEFAULT 0,
pet_fed_at TEXT
);
CREATE TABLE IF NOT EXISTS child_subjects (
@@ -277,6 +279,8 @@ def _migrate(conn: sqlite3.Connection) -> None:
"unstable_count": "INTEGER NOT NULL DEFAULT 0"},
"guardians": {"phone": "TEXT"},
"papers": {"clean_path": "TEXT", "image_path": "TEXT"},
# 小狮的形态与「今天喂过没有」要跨设备一致,所以落库(原来只在浏览器算出来)
"children": {"pet_stage": "INTEGER NOT NULL DEFAULT 0", "pet_fed_at": "TEXT"},
}
for table, cols in want.items():
ensure_columns(conn, table, cols)
+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,
# 答完才能给答案:讲解要照着它生成步骤,之前不给是为了不让孩子提前看