diff --git a/routers/sentences.py b/routers/sentences.py index 5a5f248..8af1b4f 100644 --- a/routers/sentences.py +++ b/routers/sentences.py @@ -151,6 +151,59 @@ async def get_next_sentence(request: Request): } +@router.post("/show-answer") +async def show_answer(request: Request): + """Подсказка «показать предложение». Easy — ∞ бесплатно, Medium — 2 раза × −20, Hard — 403.""" + telegram_id, _ = await require_auth(request) + body = await request.json() + sentence_id = body.get("sentence_id") + if not sentence_id: + raise HTTPException(400, "sentence_id required") + + pool = await get_pool() + async with pool.acquire() as conn: + user = await conn.fetchrow( + "SELECT difficulty FROM users WHERE telegram_id = $1", telegram_id + ) + if not user: + raise HTTPException(401, "User not registered") + difficulty = user["difficulty"] or "medium" + + row = await conn.fetchrow( + "SELECT sentence_pl FROM sentences WHERE id = $1", sentence_id + ) + if not row: + raise HTTPException(404, "Sentence not found") + + if difficulty == "hard": + raise HTTPException(403, "Hints are disabled on Hard difficulty") + + progress = await conn.fetchrow( + "SELECT list_opened FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2", + telegram_id, sentence_id + ) + list_opened = (progress["list_opened"] if progress else 0) or 0 + + if difficulty == "medium": + if list_opened >= 2: + raise HTTPException(403, "Show answer limit reached on Medium difficulty") + await conn.execute(""" + INSERT INTO sentence_progress (user_id, sentence_id, list_opened) + VALUES ($1, $2, 1) + ON CONFLICT (user_id, sentence_id) DO UPDATE + SET list_opened = sentence_progress.list_opened + 1 + """, telegram_id, sentence_id) + new_list_opened = list_opened + 1 + else: + new_list_opened = list_opened # Easy — не инкрементим + + return { + "sentence_pl": row["sentence_pl"], + "list_opened": new_list_opened, + "max_list_opened": None if difficulty == "easy" else 2, + } + + @router.post("/check") async def check_sentence(request: Request): """Проверка предложения с защитой от повторного прохождения и выдачей достижений"""