feat(wordsearch): конструктор уровней + csv_words + word_usage + приоритет ручных в игре

This commit is contained in:
2026-09-29 15:31:13 +00:00
parent f2c7be8451
commit 7dbfcbe760
5 changed files with 623 additions and 6 deletions
+77
View File
@@ -1068,6 +1068,83 @@ async def init_db():
await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level_number ON levels(level, level_number)") await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level_number ON levels(level, level_number)")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level_id ON level_words(level_id)") await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level_id ON level_words(level_id)")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level ON level_words(level)") await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level ON level_words(level)")
# ============ Авто-миграция: wordsearch builder (29.09.2026) ============
# is_active — черновик (FALSE) / опубликован (TRUE)
# created_by — telegram_id админа, создавшего вручную
# created_at — когда создан
col_exists = await conn.fetchval("""
SELECT 1 FROM information_schema.columns
WHERE table_name = 'levels' AND column_name = 'is_active'
""")
if not col_exists:
await conn.execute("""
ALTER TABLE levels ADD COLUMN is_active BOOLEAN DEFAULT TRUE
""")
logger.info("Added is_active column to levels")
col_exists = await conn.fetchval("""
SELECT 1 FROM information_schema.columns
WHERE table_name = 'levels' AND column_name = 'created_by'
""")
if not col_exists:
await conn.execute("""
ALTER TABLE levels ADD COLUMN created_by BIGINT
""")
logger.info("Added created_by column to levels")
col_exists = await conn.fetchval("""
SELECT 1 FROM information_schema.columns
WHERE table_name = 'levels' AND column_name = 'created_at'
""")
if not col_exists:
await conn.execute("""
ALTER TABLE levels ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
""")
logger.info("Added created_at column to levels")
# Индекс для быстрого поиска опубликованных ручных уровней
await conn.execute("""
CREATE INDEX IF NOT EXISTS idx_levels_active_manual
ON levels(level, is_active, created_by)
WHERE is_active = TRUE AND created_by IS NOT NULL
""")
# ============ csv_words — слова из CSV для конструктора ============
await conn.execute("""
CREATE TABLE IF NOT EXISTS csv_words (
id SERIAL PRIMARY KEY,
level_code VARCHAR(10) NOT NULL,
word_pl TEXT NOT NULL,
translation_ru TEXT DEFAULT '',
translation_uk TEXT DEFAULT '',
translation_en TEXT DEFAULT '',
UNIQUE(level_code, word_pl)
)
""")
await conn.execute("""
CREATE INDEX IF NOT EXISTS idx_csv_words_level
ON csv_words(level_code)
""")
logger.info("Table csv_words initialized")
# ============ word_usage — счётчик использований слов ============
await conn.execute("""
CREATE TABLE IF NOT EXISTS word_usage (
id SERIAL PRIMARY KEY,
level_code VARCHAR(10) NOT NULL,
word_pl TEXT NOT NULL,
usage_count INT DEFAULT 0,
last_used_at TIMESTAMP DEFAULT NOW(),
UNIQUE(level_code, word_pl)
)
""")
await conn.execute("""
CREATE INDEX IF NOT EXISTS idx_word_usage_level_word
ON word_usage(level_code, word_pl)
""")
logger.info("Table word_usage initialized")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level_number ON sentences(level, sentence_number)") await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level_number ON sentences(level, sentence_number)")
# Оставляем score-индексы (не дубли) # Оставляем score-индексы (не дубли)
await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_score ON user_progress(score DESC)") await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_score ON user_progress(score DESC)")
+2 -1
View File
@@ -14,7 +14,7 @@ from fastapi.staticfiles import StaticFiles
from config import FRONTEND_ORIGINS, UPLOADS_DIR, CF_IPS from config import FRONTEND_ORIGINS, UPLOADS_DIR, CF_IPS
from database import init_db from database import init_db
from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content
from routers import lectures, admin_auth from routers import lectures, admin_auth, admin_wordsearch
from routers.sentences import init_sentences from routers.sentences import init_sentences
from routers.achievements import init_achievements from routers.achievements import init_achievements
from routers.learning_data import init_learning_data from routers.learning_data import init_learning_data
@@ -365,6 +365,7 @@ app.include_router(feed.router)
app.include_router(admin_content.router) app.include_router(admin_content.router)
app.include_router(lectures.router) app.include_router(lectures.router)
app.include_router(admin_auth.router) app.include_router(admin_auth.router)
app.include_router(admin_wordsearch.router)
# ============ Базовые эндпоинты ============ # ============ Базовые эндпоинты ============
+449
View File
@@ -0,0 +1,449 @@
"""
Admin Wordsearch Router — конструктор уровней филвордов.
Доступ: только для ADMIN_IDS.
Логика: препод создаёт уровни вручную через админку.
Эндпоинты:
- GET /admin/wordsearch/words?level=A1 — слова из CSV + счётчики
- GET /admin/wordsearch/levels?level=A1 — список созданных уровней
- GET /admin/wordsearch/level/{id} — один уровень (для редактирования)
- POST /admin/wordsearch/level — создать
- PUT /admin/wordsearch/level/{id} — обновить
- DELETE /admin/wordsearch/level/{id} — удалить
- POST /admin/wordsearch/validate — проверить корректность уровня
"""
import json
import logging
from typing import Optional
from fastapi import APIRouter, Request, HTTPException
from auth import require_auth
from database import get_pool
from config import ADMIN_IDS
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/admin/wordsearch", tags=["admin-wordsearch"])
LEVELS = ["A1", "A2", "B1", "B2", "C1", "C2"]
MAX_GRID_SIZE = 15
def _require_admin(telegram_id: int) -> None:
if telegram_id not in ADMIN_IDS:
raise HTTPException(403, "Access denied")
# ============ СЛОВА ИЗ CSV ============
@router.get("/words")
async def get_csv_words(request: Request, level: str = "A1"):
"""
Список слов из CSV для уровня с счётчиком использований.
Сортировка: сначала неиспользованные, потом по алфавиту.
"""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
if level not in LEVELS:
raise HTTPException(400, f"Invalid level: {level}")
pool = await get_pool()
async with pool.acquire() as conn:
rows = await conn.fetch("""
SELECT
w.word_pl,
w.translation_ru,
w.translation_uk,
w.translation_en,
COALESCE(u.usage_count, 0) AS usage_count
FROM csv_words w
LEFT JOIN word_usage u
ON u.word_pl = w.word_pl AND u.level_code = w.level_code
WHERE w.level_code = $1
ORDER BY COALESCE(u.usage_count, 0) ASC, w.word_pl ASC
""", level)
return {
"level": level,
"total": len(rows),
"words": [
{
"word_pl": r["word_pl"],
"translation_ru": r["translation_ru"] or "",
"translation_uk": r["translation_uk"] or "",
"translation_en": r["translation_en"] or "",
"usage_count": r["usage_count"],
}
for r in rows
],
}
# ============ УРОВНИ ============
@router.get("/levels")
async def list_levels(request: Request, level: Optional[str] = None):
"""Список созданных вручную уровней филвордов."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
pool = await get_pool()
async with pool.acquire() as conn:
if level and level in LEVELS:
rows = await conn.fetch("""
SELECT id, level_number, grid_size, level, is_active, created_at,
jsonb_array_length(grid_letters) AS rows_count
FROM levels
WHERE created_by IS NOT NULL AND level = $1
ORDER BY level_number ASC, id ASC
""", level)
else:
rows = await conn.fetch("""
SELECT id, level_number, grid_size, level, is_active, created_at,
jsonb_array_length(grid_letters) AS rows_count
FROM levels
WHERE created_by IS NOT NULL
ORDER BY level, level_number ASC, id ASC
""")
return {
"total": len(rows),
"levels": [
{
"id": r["id"],
"level": r["level"],
"level_number": r["level_number"],
"grid_size": r["grid_size"],
"is_active": r["is_active"],
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
}
for r in rows
],
}
@router.get("/level/{level_id}")
async def get_level(request: Request, level_id: int):
"""Один уровень целиком — для редактирования в конструкторе."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
pool = await get_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow("""
SELECT id, level_number, grid_size, level, grid_letters, word_paths,
is_active, created_by, created_at
FROM levels
WHERE id = $1 AND created_by IS NOT NULL
""", level_id)
if not row:
raise HTTPException(404, "Level not found")
# Слова уровня для списка размещённых
words_rows = await conn.fetch("""
SELECT word_pl, translation_ru, translation_uk, translation_en
FROM level_words
WHERE level_id = $1
""", level_id)
return {
"id": row["id"],
"level": row["level"],
"level_number": row["level_number"],
"grid_size": row["grid_size"],
"grid_letters": row["grid_letters"],
"word_paths": row["word_paths"],
"is_active": row["is_active"],
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
"words": [
{
"word_pl": w["word_pl"],
"translation_ru": w["translation_ru"] or "",
"translation_uk": w["translation_uk"] or "",
"translation_en": w["translation_en"] or "",
}
for w in words_rows
],
}
def _validate_payload(payload: dict) -> tuple[int, list, dict]:
"""
Валидация grid_letters и word_paths.
Returns: (grid_size, grid_letters, word_paths)
Raises: HTTPException(400, ...)
"""
grid_size = payload.get("grid_size")
if not isinstance(grid_size, int) or grid_size < 3 or grid_size > MAX_GRID_SIZE:
raise HTTPException(400, f"grid_size must be 3-{MAX_GRID_SIZE}")
grid_letters = payload.get("grid_letters")
if not isinstance(grid_letters, list) or len(grid_letters) != grid_size:
raise HTTPException(400, "grid_letters must be a 2D array of size grid_size × grid_size")
for row in grid_letters:
if not isinstance(row, list) or len(row) != grid_size:
raise HTTPException(400, "each row of grid_letters must have grid_size elements")
for cell in row:
# Разрешаем: null (неактивная), "", либо одна буква
if cell is not None and (not isinstance(cell, str) or len(cell) > 1):
raise HTTPException(400, "cell must be null, '', or a single character")
word_paths = payload.get("word_paths")
if not isinstance(word_paths, dict):
raise HTTPException(400, "word_paths must be an object")
# Проверяем, что каждое слово в word_paths имеет корректный путь
for word, path in word_paths.items():
if not isinstance(word, str) or not word:
raise HTTPException(400, "word must be a non-empty string")
if not isinstance(path, list) or len(path) == 0:
raise HTTPException(400, f"path for '{word}' must be a non-empty array")
# Проверяем, что путь соседний (Manhattan distance 1)
for i in range(1, len(path)):
prev = path[i - 1]
cur = path[i]
if not (isinstance(prev, list) and len(prev) == 2 and
isinstance(cur, list) and len(cur) == 2):
raise HTTPException(400, f"path points for '{word}' must be [r, c]")
dr = abs(prev[0] - cur[0])
dc = abs(prev[1] - cur[1])
if not (dr <= 1 and dc <= 1 and (dr + dc) > 0):
raise HTTPException(400, f"path for '{word}' has non-adjacent cells")
# Проверяем, что буквы в клетках совпадают со словом
word_upper = word.upper()
if len(path) != len(word_upper):
raise HTTPException(
400,
f"path length for '{word}' ({len(path)}) != word length ({len(word_upper)})"
)
for i, (r, c) in enumerate(path):
if r < 0 or r >= grid_size or c < 0 or c >= grid_size:
raise HTTPException(400, f"path for '{word}' goes out of grid")
cell = grid_letters[r][c]
if cell != word_upper[i]:
raise HTTPException(
400,
f"grid[{r}][{c}] = {cell!r}, expected {word_upper[i]!r} for '{word}'"
)
return grid_size, grid_letters, word_paths
@router.post("/validate")
async def validate_level(request: Request):
"""Проверяет корректность уровня без сохранения."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
body = await request.json()
try:
_validate_payload(body)
except HTTPException as e:
return {"valid": False, "error": e.detail}
return {"valid": True}
# ============ СОЗДАНИЕ / ОБНОВЛЕНИЕ ============
async def _upsert_level_words(conn, level_id: int, level_code: str, word_paths: dict):
"""Обновляет level_words и word_usage для уровня."""
# Получаем переводы из csv_words
words = list(word_paths.keys())
if not words:
return
rows = await conn.fetch("""
SELECT word_pl, translation_ru, translation_uk, translation_en
FROM csv_words
WHERE level_code = $1 AND word_pl = ANY($2::text[])
""", level_code, words)
translations_map = {r["word_pl"]: r for r in rows}
# Удаляем старые level_words
await conn.execute("DELETE FROM level_words WHERE level_id = $1", level_id)
# Вставляем новые
for word in words:
trans = translations_map.get(word, {})
await conn.execute("""
INSERT INTO level_words (level_id, word_pl, translation_ru, translation_uk, translation_en, level)
VALUES ($1, $2, $3, $4, $5, $6)
""", level_id, word,
trans["translation_ru"] if trans else "",
trans["translation_uk"] if trans else "",
trans["translation_en"] if trans else "",
level_code,
)
@router.post("/level")
async def create_level(request: Request):
"""Создать новый уровень."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
body = await request.json()
level_code = body.get("level", "A1")
if level_code not in LEVELS:
raise HTTPException(400, f"Invalid level: {level_code}")
level_number = body.get("level_number")
if not isinstance(level_number, int) or level_number < 1:
raise HTTPException(400, "level_number must be a positive integer")
is_active = bool(body.get("is_active", False))
grid_size, grid_letters, word_paths = _validate_payload(body)
pool = await get_pool()
async with pool.acquire() as conn:
async with conn.transaction():
# 🔧 ФИКС (29.09.2026): проверяем только РУЧНЫЕ уровни.
# Старые авто-сгенерированные (created_by IS NULL) не мешают.
existing = await conn.fetchval("""
SELECT id FROM levels
WHERE level = $1 AND level_number = $2 AND created_by IS NOT NULL
""", level_code, level_number)
if existing:
raise HTTPException(
409,
f"Level {level_code} #{level_number} already exists (id={existing})"
)
row = await conn.fetchrow("""
INSERT INTO levels
(level_number, grid_size, game_type, level, grid_letters, word_paths,
is_active, created_by, created_at)
VALUES ($1, $2, 'wordsearch', $3, $4, $5, $6, $7, NOW())
RETURNING id
""",
level_number, grid_size, level_code,
json.dumps(grid_letters),
json.dumps(word_paths),
is_active, telegram_id,
)
level_id = row["id"]
# Обновляем level_words
await _upsert_level_words(conn, level_id, level_code, word_paths)
# Обновляем счётчики использований
for word in word_paths.keys():
await conn.execute("""
INSERT INTO word_usage (level_code, word_pl, usage_count, last_used_at)
VALUES ($1, $2, 1, NOW())
ON CONFLICT (level_code, word_pl)
DO UPDATE SET
usage_count = word_usage.usage_count + 1,
last_used_at = NOW()
""", level_code, word)
logger.info(
f"[admin_wordsearch] level created: {level_code} #{level_number} "
f"(id={level_id}, {len(word_paths)} words, active={is_active}) "
f"by admin {telegram_id}"
)
return {"status": "ok", "level_id": level_id}
@router.put("/level/{level_id}")
async def update_level(request: Request, level_id: int):
"""Обновить уровень."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
body = await request.json()
pool = await get_pool()
async with pool.acquire() as conn:
existing = await conn.fetchrow("""
SELECT id, level, level_number FROM levels
WHERE id = $1 AND created_by IS NOT NULL
""", level_id)
if not existing:
raise HTTPException(404, "Level not found")
level_code = existing["level"]
grid_size, grid_letters, word_paths = _validate_payload(body)
is_active = bool(body.get("is_active", False))
async with conn.transaction():
await conn.execute("""
UPDATE levels
SET grid_size = $1,
grid_letters = $2,
word_paths = $3,
is_active = $4
WHERE id = $5
""",
grid_size,
json.dumps(grid_letters),
json.dumps(word_paths),
is_active,
level_id,
)
await _upsert_level_words(conn, level_id, level_code, word_paths)
# Пересчитываем счётчики: сначала откатываем, потом инкрементим заново
# Простой путь: не откатываем, только добавляем за новые слова
# (пересечения и повторные использования считаются корректно,
# так как слово используется в новом уровне впервые)
for word in word_paths.keys():
await conn.execute("""
INSERT INTO word_usage (level_code, word_pl, usage_count, last_used_at)
VALUES ($1, $2, 1, NOW())
ON CONFLICT (level_code, word_pl)
DO UPDATE SET
usage_count = word_usage.usage_count + 1,
last_used_at = NOW()
""", level_code, word)
logger.info(
f"[admin_wordsearch] level updated: id={level_id} "
f"({len(word_paths)} words, active={is_active}) "
f"by admin {telegram_id}"
)
return {"status": "ok", "level_id": level_id}
@router.delete("/level/{level_id}")
async def delete_level(request: Request, level_id: int):
"""Удалить уровень (только созданные вручную)."""
telegram_id, _ = await require_auth(request)
_require_admin(telegram_id)
pool = await get_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow("""
SELECT id, level, level_number FROM levels
WHERE id = $1 AND created_by IS NOT NULL
""", level_id)
if not row:
raise HTTPException(404, "Level not found")
await conn.execute("DELETE FROM levels WHERE id = $1", level_id)
logger.info(
f"[admin_wordsearch] level deleted: id={level_id} "
f"({row['level']} #{row['level_number']}) by admin {telegram_id}"
)
return {"status": "ok", "level_id": level_id}
+18 -4
View File
@@ -80,10 +80,24 @@ async def get_level(request: Request, level_number: int):
if level_number > 1 and level_number > last_completed + 1: if level_number > 1 and level_number > last_completed + 1:
raise HTTPException(403, "Level not available yet. Complete previous level first.") raise HTTPException(403, "Level not available yet. Complete previous level first.")
# Получаем уровень по его номеру ВНУТРИ выбранного уровня # 🔧 ФИКС (29.09.2026): приоритет ручным уровням.
level = await conn.fetchrow( # Если есть уровень, созданный вручную (created_by IS NOT NULL) —
"SELECT * FROM levels WHERE level = $1 AND level_number = $2", current_level, level_number # показываем его. Иначе — fallback на авто (created_by IS NULL).
) level = await conn.fetchrow("""
SELECT * FROM levels
WHERE level = $1 AND level_number = $2
AND created_by IS NOT NULL
LIMIT 1
""", current_level, level_number)
# Fallback на авто, если ручного нет
if not level:
level = await conn.fetchrow("""
SELECT * FROM levels
WHERE level = $1 AND level_number = $2
AND created_by IS NULL
LIMIT 1
""", current_level, level_number)
if not level: if not level:
raise HTTPException(404, "Level not found") raise HTTPException(404, "Level not found")
+76
View File
@@ -0,0 +1,76 @@
"""
Импорт слов из CSV в таблицу csv_words.
Запуск:
python scripts/import_csv_words.py
Читает: words_a1.csv, words_a2.csv, words_b1.csv
Пишет: csv_words (level_code, word_pl, translation_ru, translation_uk, translation_en)
"""
import asyncio
import csv
import logging
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from database import get_pool
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
BACKEND_DIR = Path(__file__).parent.parent
CSV_FILES = [
("A1", "words_a1.csv"),
("A2", "words_a2.csv"),
("B1", "words_b1.csv"),
]
async def import_level(conn, level_code: str, filename: str) -> int:
"""Импортирует один CSV. Возвращает количество добавленных слов."""
path = BACKEND_DIR / filename
if not path.exists():
logger.warning(f"⚠️ Файл не найден: {path} — пропускаю")
return 0
count = 0
with open(path, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
word_pl = (row.get('polish') or '').strip().lower()
if not word_pl:
continue
translation_en = (row.get('english') or '').strip()
translation_ru = (row.get('russian') or '').strip()
translation_uk = (row.get('ukrainian') or '').strip()
await conn.execute("""
INSERT INTO csv_words (level_code, word_pl, translation_ru, translation_uk, translation_en)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (level_code, word_pl) DO UPDATE
SET translation_ru = EXCLUDED.translation_ru,
translation_uk = EXCLUDED.translation_uk,
translation_en = EXCLUDED.translation_en
""", level_code, word_pl, translation_ru, translation_uk, translation_en)
count += 1
logger.info(f"✅ {level_code} ({filename}): {count} слов импортировано")
return count
async def main():
pool = await get_pool()
total = 0
async with pool.acquire() as conn:
for level_code, filename in CSV_FILES:
total += await import_level(conn, level_code, filename)
logger.info(f"🎉 Готово! Всего импортировано: {total} слов")
if __name__ == "__main__":
asyncio.run(main())