diff --git a/routers/sentences.py b/routers/sentences.py index 83f3ce4..43a4241 100644 --- a/routers/sentences.py +++ b/routers/sentences.py @@ -268,36 +268,58 @@ async def check_sentence(request: Request): is_correct = user_words_normalized == correct_words_normalized if is_correct: - word_count = len(correct_words_original) - - # 🆕 Расчёт звёзд для "Составь предложение" - # Старт 5. Штрафы: - # -1 за каждую неверную проверку (клиент шлёт wrong_checks) - # -1 если time > лимита (по длине: 3-4 → 30с, 5-6 → 45с, 7-8 → 60с) + # 🔧 НОВОЕ (16.10.2026): очки по формуле сложностей. + # Easy — фикс 20. Medium — 60 − 20×list_opened (мин 20). + # Hard — 100, +50 бонус если completion_time <= 15 сек. wrong_checks = int(body.get("wrong_checks", 0) or 0) - if word_count <= 4: - time_limit = 30 - elif word_count <= 6: - time_limit = 45 - else: - time_limit = 60 + user_row = await conn.fetchrow( + "SELECT difficulty FROM users WHERE telegram_id = $1", telegram_id + ) + difficulty = (user_row["difficulty"] if user_row else None) or "medium" - time_penalty = 1 if (completion_time and completion_time > time_limit) else 0 - wrong_penalty = min(max(0, wrong_checks), 3) - stars = max(1, 5 - wrong_penalty - time_penalty) + progress_row = await conn.fetchrow( + "SELECT list_opened FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2", + telegram_id, sentence_id + ) + list_opened = (progress_row["list_opened"] if progress_row else 0) or 0 + + if difficulty == "easy": + points = 20 + elif difficulty == "medium": + points = max(20, 60 - 20 * list_opened) + else: # hard + points = 100 + if completion_time is not None and completion_time <= 15: + points = 150 # Начисляем XP from services.xp import award_xp - xp_result = await award_xp(conn, telegram_id, "sentence", stars) + xp_result = await award_xp(conn, telegram_id, "sentence", points) score = xp_result["points"] await conn.execute( - """INSERT INTO sentence_progress (user_id, sentence_id, completed_at, score, level) - VALUES ($1, $2, NOW(), $3, $4)""", - telegram_id, sentence_id, score, row["level"], + """INSERT INTO sentence_progress (user_id, sentence_id, completed_at, score, level, completion_time) + VALUES ($1, $2, NOW(), $3, $4, $5) + ON CONFLICT (user_id, sentence_id) DO UPDATE + SET completed_at = NOW(), score = $3, level = $4, completion_time = $5""", + telegram_id, sentence_id, score, row["level"], completion_time or 0, ) + # 🔧 НОВОЕ (16.10.2026): серии в sentence_stats (PK user_id). + await conn.execute(""" + INSERT INTO sentence_stats (user_id, total_score, no_mistakes_streak, perfect_streak, updated_at) + VALUES ($1, $2, + CASE WHEN $3 = 0 THEN 1 ELSE 0 END, + CASE WHEN $3 = 0 AND $4 = 0 THEN 1 ELSE 0 END, + NOW()) + ON CONFLICT (user_id) DO UPDATE + SET total_score = sentence_stats.total_score + $2, + no_mistakes_streak = CASE WHEN $3 = 0 THEN sentence_stats.no_mistakes_streak + 1 ELSE 0 END, + perfect_streak = CASE WHEN $3 = 0 AND $4 = 0 THEN sentence_stats.perfect_streak + 1 ELSE 0 END, + updated_at = NOW() + """, telegram_id, score, wrong_checks, list_opened) + # 🔧 ФИКС (23.09.2026): achievements_nested_pool_acquire_sentences — # раньше check_and_award_achievements вызывался ВНУТРИ async with pool.acquire(), # что могло привести к дедлоку пула. Теперь собираем данные внутри блока,