[Eva] B2.3: формула очков по сложности + серии в sentence_stats
This commit is contained in:
+41
-19
@@ -268,36 +268,58 @@ async def check_sentence(request: Request):
|
|||||||
is_correct = user_words_normalized == correct_words_normalized
|
is_correct = user_words_normalized == correct_words_normalized
|
||||||
|
|
||||||
if is_correct:
|
if is_correct:
|
||||||
word_count = len(correct_words_original)
|
# 🔧 НОВОЕ (16.10.2026): очки по формуле сложностей.
|
||||||
|
# Easy — фикс 20. Medium — 60 − 20×list_opened (мин 20).
|
||||||
# 🆕 Расчёт звёзд для "Составь предложение"
|
# Hard — 100, +50 бонус если completion_time <= 15 сек.
|
||||||
# Старт 5. Штрафы:
|
|
||||||
# -1 за каждую неверную проверку (клиент шлёт wrong_checks)
|
|
||||||
# -1 если time > лимита (по длине: 3-4 → 30с, 5-6 → 45с, 7-8 → 60с)
|
|
||||||
wrong_checks = int(body.get("wrong_checks", 0) or 0)
|
wrong_checks = int(body.get("wrong_checks", 0) or 0)
|
||||||
|
|
||||||
if word_count <= 4:
|
user_row = await conn.fetchrow(
|
||||||
time_limit = 30
|
"SELECT difficulty FROM users WHERE telegram_id = $1", telegram_id
|
||||||
elif word_count <= 6:
|
)
|
||||||
time_limit = 45
|
difficulty = (user_row["difficulty"] if user_row else None) or "medium"
|
||||||
else:
|
|
||||||
time_limit = 60
|
|
||||||
|
|
||||||
time_penalty = 1 if (completion_time and completion_time > time_limit) else 0
|
progress_row = await conn.fetchrow(
|
||||||
wrong_penalty = min(max(0, wrong_checks), 3)
|
"SELECT list_opened FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2",
|
||||||
stars = max(1, 5 - wrong_penalty - time_penalty)
|
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
|
# Начисляем XP
|
||||||
from services.xp import award_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"]
|
score = xp_result["points"]
|
||||||
|
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
"""INSERT INTO sentence_progress (user_id, sentence_id, completed_at, score, level)
|
"""INSERT INTO sentence_progress (user_id, sentence_id, completed_at, score, level, completion_time)
|
||||||
VALUES ($1, $2, NOW(), $3, $4)""",
|
VALUES ($1, $2, NOW(), $3, $4, $5)
|
||||||
telegram_id, sentence_id, score, row["level"],
|
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 —
|
# 🔧 ФИКС (23.09.2026): achievements_nested_pool_acquire_sentences —
|
||||||
# раньше check_and_award_achievements вызывался ВНУТРИ async with pool.acquire(),
|
# раньше check_and_award_achievements вызывался ВНУТРИ async with pool.acquire(),
|
||||||
# что могло привести к дедлоку пула. Теперь собираем данные внутри блока,
|
# что могло привести к дедлоку пула. Теперь собираем данные внутри блока,
|
||||||
|
|||||||
Reference in New Issue
Block a user