Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b98655716 | ||
|
|
da7182fb6b | ||
|
|
b62311d3ed | ||
|
|
6f341a7f33 | ||
|
|
981e65e57c | ||
|
|
9c975dde79 | ||
|
|
df3d32ecd5 | ||
|
|
437921d9dd | ||
|
|
bcc01a0fe8 | ||
|
|
8338d6b3c8 | ||
|
|
12c6bbca51 | ||
|
|
794daad69c | ||
|
|
5e9cd0c901 | ||
|
|
ea297b2179 | ||
|
|
791adfeee9 | ||
|
|
0a4c0c5d23 |
+11
-24
@@ -35,15 +35,6 @@ GAMES = ["wordsearch", "choose", "sentence", "bubbles"]
|
|||||||
# Множитель игры дня
|
# Множитель игры дня
|
||||||
FEATURED_MULTIPLIER = 1.5
|
FEATURED_MULTIPLIER = 1.5
|
||||||
|
|
||||||
# Базовая ставка: 1 звезда = 20 XP
|
|
||||||
POINTS_PER_STAR = 20
|
|
||||||
|
|
||||||
# Максимум звёзд за уровень
|
|
||||||
MAX_STARS = 5
|
|
||||||
|
|
||||||
# Минимум звёзд за пройденный уровень
|
|
||||||
MIN_STARS = 1
|
|
||||||
|
|
||||||
# Коэффициенты баланса (настроить после первых 1000 сессий)
|
# Коэффициенты баланса (настроить после первых 1000 сессий)
|
||||||
# Применяются к base_points ДО множителя игры дня.
|
# Применяются к base_points ДО множителя игры дня.
|
||||||
GAME_XP_COEF: dict[str, float] = {
|
GAME_XP_COEF: dict[str, float] = {
|
||||||
@@ -192,7 +183,7 @@ async def award_xp(
|
|||||||
conn,
|
conn,
|
||||||
user_id: int,
|
user_id: int,
|
||||||
game: str,
|
game: str,
|
||||||
stars: int,
|
points: int,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Единая функция начисления опыта.
|
Единая функция начисления опыта.
|
||||||
@@ -201,11 +192,10 @@ async def award_xp(
|
|||||||
conn: asyncpg connection (уже acquired)
|
conn: asyncpg connection (уже acquired)
|
||||||
user_id: telegram_id (может быть отрицательным для Pleily)
|
user_id: telegram_id (может быть отрицательным для Pleily)
|
||||||
game: 'wordsearch' | 'choose' | 'sentence' | 'bubbles'
|
game: 'wordsearch' | 'choose' | 'sentence' | 'bubbles'
|
||||||
stars: 1..5
|
points: готовые очки за уровень (20–110), формула считается до вызова
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
{
|
{
|
||||||
"stars": int,
|
|
||||||
"base_points": int,
|
"base_points": int,
|
||||||
"multiplier": float,
|
"multiplier": float,
|
||||||
"points": int,
|
"points": int,
|
||||||
@@ -219,18 +209,16 @@ async def award_xp(
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
# 1. Валидация
|
# 1. Валидация
|
||||||
stars = max(MIN_STARS, min(MAX_STARS, int(stars)))
|
|
||||||
if game not in GAME_XP_COEF:
|
if game not in GAME_XP_COEF:
|
||||||
raise ValueError(f"Unknown game: {game}")
|
raise ValueError(f"Unknown game: {game}")
|
||||||
|
|
||||||
# 2. База
|
# 2. База с коэффициентом игры (для баланса)
|
||||||
base = stars * POINTS_PER_STAR
|
base_points = int(round(points * GAME_XP_COEF.get(game, 1.0)))
|
||||||
base = int(round(base * GAME_XP_COEF.get(game, 1.0)))
|
|
||||||
|
|
||||||
# 3. Множитель игры дня
|
# 3. Множитель игры дня
|
||||||
is_featured = (game == featured_game())
|
is_featured = (game == featured_game())
|
||||||
multiplier = FEATURED_MULTIPLIER if is_featured else 1.0
|
multiplier = FEATURED_MULTIPLIER if is_featured else 1.0
|
||||||
points = int(round(base * multiplier))
|
final_points = int(round(base_points * multiplier))
|
||||||
|
|
||||||
# 4. Читаем текущий total_xp
|
# 4. Читаем текущий total_xp
|
||||||
row = await conn.fetchrow(
|
row = await conn.fetchrow(
|
||||||
@@ -259,11 +247,11 @@ async def award_xp(
|
|||||||
INSERT INTO xp_log (user_id, game, stars, base_points, multiplier, points)
|
INSERT INTO xp_log (user_id, game, stars, base_points, multiplier, points)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
""",
|
""",
|
||||||
user_id, game, stars, base, multiplier, points,
|
user_id, game, 0, base_points, multiplier, final_points,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 6. Обновляем user_profile
|
# 6. Обновляем user_profile
|
||||||
new_total = old_total + points
|
new_total = old_total + final_points
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE user_profile
|
UPDATE user_profile
|
||||||
@@ -272,7 +260,7 @@ async def award_xp(
|
|||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
""",
|
""",
|
||||||
user_id, points,
|
user_id, final_points,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 7. Считаем новый уровень
|
# 7. Считаем новый уровень
|
||||||
@@ -287,14 +275,13 @@ async def award_xp(
|
|||||||
}
|
}
|
||||||
logger.info(
|
logger.info(
|
||||||
"[xp] user=%s level_up %s → %s (%s), +%s xp",
|
"[xp] user=%s level_up %s → %s (%s), +%s xp",
|
||||||
user_id, old_level, new_level, rank_key, points,
|
user_id, old_level, new_level, rank_key, final_points,
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"stars": stars,
|
"base_points": base_points,
|
||||||
"base_points": base,
|
|
||||||
"multiplier": multiplier,
|
"multiplier": multiplier,
|
||||||
"points": points,
|
"points": final_points,
|
||||||
"featured": is_featured,
|
"featured": is_featured,
|
||||||
"level_up": level_up,
|
"level_up": level_up,
|
||||||
"total_xp": new_total,
|
"total_xp": new_total,
|
||||||
|
|||||||
Reference in New Issue
Block a user