feat: XP foundation + waves + featured game + profile + leaderboard by total_xp
- services/xp.py: award_xp, level_for, RANKS (48), featured_game - services/waves.py: wave_plan, grid_size_for, wave_medal - routers/featured.py: GET /featured (игра дня ×1.5) - routers/users.py: POST /set-nickname, POST /set-avatar - routers/app_auth.py: /auth/me с XP-полями (level, rank_key, rank_name, xp) - routers/game.py: award_xp + 3 штрафа + POST /game/next (волны) - routers/wordquiz.py: award_xp + серии в choose_progress - routers/bubblewords.py: award_xp + 4 штрафа - routers/sentences.py: award_xp по длине предложения - routers/leaderboard.py: сортировка по total_xp, XP-поля в ответе - database.py: миграции user_profile, xp_log, choose_progress, user_seen_levels, user_game_progress, wave_results, bubble_levels, user_word_stats - main.py: подключение routers/featured Проверено: - /auth/me отдаёт level=1, rank_00, xp_in_level - /featured отдаёт wordsearch + seconds_left - /game/complete-level начисляет XP через award_xp - xp_log, user_profile заполняются - /game/next выдаёт уровень по волнам - /leaderboard сортирует по total_xp
This commit is contained in:
+77
-35
@@ -18,6 +18,12 @@ Telegram-юзеры сохраняют свои положительные teleg
|
||||
- DELETE /me — удаление аккаунта (Apple требует)
|
||||
|
||||
🔧 НОВОЕ (07.10.2026): pleily_auth — первая версия.
|
||||
🔧 ФИКС (07.10.2026): device_id_in_users —
|
||||
теперь device_id пишется и в users.device_id, и в auth_identities.
|
||||
Это нужно для SQL-запросов по device_id (админка, миграции).
|
||||
🔧 НОВОЕ (08.10.2026): auth_me_xp_fields —
|
||||
в /auth/me добавлены XP-поля: level, rank_key, rank_name,
|
||||
xp_in_level, xp_needed, total_xp, weekly_xp, weekly_rank.
|
||||
"""
|
||||
import logging
|
||||
import uuid
|
||||
@@ -97,11 +103,14 @@ async def _create_app_user(conn, device_id: str) -> int:
|
||||
uid_row = await conn.fetchrow("SELECT nextval('app_user_id_seq') AS uid")
|
||||
uid = uid_row["uid"]
|
||||
|
||||
# Создаём юзера в users
|
||||
# 🔧 ФИКС (07.10.2026): device_id_in_users —
|
||||
# раньше device_id сохранялся только в auth_identities,
|
||||
# а в users.device_id был NULL. Теперь — в обоих местах.
|
||||
# Это нужно для SQL-запросов по device_id (админка, миграции).
|
||||
await conn.execute("""
|
||||
INSERT INTO users (telegram_id, username, source)
|
||||
VALUES ($1, $2, 'app')
|
||||
""", uid, f"app_user_{abs(uid)}")
|
||||
INSERT INTO users (telegram_id, username, source, device_id)
|
||||
VALUES ($1, $2, 'app', $3)
|
||||
""", uid, f"app_user_{abs(uid)}", device_id)
|
||||
|
||||
# Создаём identity
|
||||
await conn.execute("""
|
||||
@@ -174,45 +183,78 @@ async def auth_device(request: Request):
|
||||
@router.get("/me")
|
||||
async def auth_me(request: Request):
|
||||
"""
|
||||
Возвращает данные текущего Pleily-юзера. Для тестов и отладки.
|
||||
|
||||
Требует Authorization: Bearer <JWT>.
|
||||
Текущий юзер + XP-поля.
|
||||
|
||||
🔧 НОВОЕ (08.10.2026): добавлены level, rank_key, rank_name,
|
||||
xp_in_level, xp_needed, total_xp, weekly_xp.
|
||||
"""
|
||||
user_id, _username = await require_auth(request)
|
||||
telegram_id, _ = await require_auth(request)
|
||||
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow("""
|
||||
SELECT telegram_id, username, source, app_language, current_level, created_at
|
||||
user = await conn.fetchrow(
|
||||
"""
|
||||
SELECT telegram_id, username, app_language, current_level,
|
||||
source, device_id, apple_id, created_at
|
||||
FROM users WHERE telegram_id = $1
|
||||
""", user_id)
|
||||
""",
|
||||
telegram_id,
|
||||
)
|
||||
if not user:
|
||||
raise HTTPException(401, "User not found")
|
||||
|
||||
if not row:
|
||||
raise HTTPException(404, "User not found")
|
||||
profile = await conn.fetchrow(
|
||||
"""
|
||||
SELECT nickname, avatar_id, total_xp, weekly_xp, week_started_at
|
||||
FROM user_profile WHERE user_id = $1
|
||||
""",
|
||||
telegram_id,
|
||||
)
|
||||
|
||||
# Получаем identities юзера (для диагностики)
|
||||
identities_rows = await conn.fetch("""
|
||||
SELECT provider, provider_uid, created_at
|
||||
FROM auth_identities WHERE user_id = $1
|
||||
ORDER BY created_at
|
||||
""", user_id)
|
||||
# Weekly rank
|
||||
weekly_rank = await conn.fetchval(
|
||||
"""
|
||||
SELECT COUNT(*) + 1
|
||||
FROM user_profile
|
||||
WHERE weekly_xp > COALESCE((
|
||||
SELECT weekly_xp FROM user_profile WHERE user_id = $1
|
||||
), 0)
|
||||
""",
|
||||
telegram_id,
|
||||
)
|
||||
|
||||
return {
|
||||
"user_id": row["telegram_id"],
|
||||
"username": row["username"],
|
||||
"source": row["source"],
|
||||
"app_language": row["app_language"],
|
||||
"current_level": row["current_level"],
|
||||
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
|
||||
"identities": [
|
||||
{
|
||||
"provider": r["provider"],
|
||||
"provider_uid": r["provider_uid"],
|
||||
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
|
||||
}
|
||||
for r in identities_rows
|
||||
],
|
||||
}
|
||||
# Считаем уровень
|
||||
from services.xp import level_for, rank_for
|
||||
|
||||
total_xp = int(profile["total_xp"]) if profile else 0
|
||||
weekly_xp = int(profile["weekly_xp"]) if profile else 0
|
||||
level, xp_in_level, xp_needed, rank_key = level_for(total_xp)
|
||||
|
||||
lang = (user["app_language"] or "ru").lower()
|
||||
if lang not in ("pl", "ru", "uk", "en"):
|
||||
lang = "ru"
|
||||
|
||||
return {
|
||||
"user_id": user["telegram_id"],
|
||||
"username": user["username"],
|
||||
"nickname": profile["nickname"] if profile else None,
|
||||
"avatar_id": int(profile["avatar_id"]) if profile and profile["avatar_id"] is not None else 0,
|
||||
"app_language": user["app_language"],
|
||||
"current_level": user["current_level"],
|
||||
"source": user["source"],
|
||||
"device_id": str(user["device_id"]) if user["device_id"] else None,
|
||||
"apple_id": user["apple_id"],
|
||||
"created_at": user["created_at"].isoformat() if user["created_at"] else None,
|
||||
# --- XP ---
|
||||
"total_xp": total_xp,
|
||||
"weekly_xp": weekly_xp,
|
||||
"level": level,
|
||||
"rank_key": rank_key,
|
||||
"rank_name": rank_for(level, lang),
|
||||
"xp_in_level": xp_in_level,
|
||||
"xp_needed": xp_needed,
|
||||
"weekly_rank": int(weekly_rank) if weekly_rank else 0,
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/me")
|
||||
|
||||
Reference in New Issue
Block a user