feat: implement pivot-currency model, rbac smart tokens & fix circular imports
This commit is contained in:
@@ -1,26 +1,47 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, text
|
||||
from sqlalchemy import select
|
||||
from app.models.gamification import UserStats, PointsLedger
|
||||
from app.models.identity import User
|
||||
import math
|
||||
|
||||
class GamificationService:
|
||||
@staticmethod
|
||||
async def award_points(db: AsyncSession, user_id: int, points: int, reason: str):
|
||||
"""Pontok jóváírása (SQL szinkronizált points mezővel)."""
|
||||
new_entry = PointsLedger(
|
||||
user_id=user_id,
|
||||
points=points, # Javítva: points_change helyett points
|
||||
reason=reason
|
||||
)
|
||||
db.add(new_entry)
|
||||
|
||||
result = await db.execute(select(UserStats).where(UserStats.user_id == user_id))
|
||||
stats = result.scalar_one_or_none()
|
||||
async def process_activity(db: AsyncSession, user_id: int, xp_amount: int, social_amount: int, reason: str):
|
||||
"""
|
||||
XP növelés, Szintlépés csekk és Automata Kredit váltás.
|
||||
"""
|
||||
# 1. User statisztika lekérése
|
||||
stmt = select(UserStats).where(UserStats.user_id == user_id)
|
||||
stats = (await db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
if not stats:
|
||||
stats = UserStats(user_id=user_id, total_points=0, current_level=1)
|
||||
stats = UserStats(user_id=user_id, total_xp=0, social_points=0, current_level=1, credits=0)
|
||||
db.add(stats)
|
||||
|
||||
# 2. Részletes Logolás (PointsLedger) - A visszakövethetőség miatt
|
||||
db.add(PointsLedger(
|
||||
user_id=user_id,
|
||||
xp_gain=xp_amount,
|
||||
social_gain=social_amount,
|
||||
reason=reason
|
||||
))
|
||||
|
||||
# 3. XP és Szintlépés (Nehezedő görbe)
|
||||
stats.total_xp += xp_amount
|
||||
# Képlet: Level = (XP / 500)^(1/1.5)
|
||||
new_level = int((stats.total_xp / 500) ** (1/1.5)) + 1
|
||||
if new_level > stats.current_level:
|
||||
stats.current_level = new_level
|
||||
|
||||
# 4. Automata Kredit váltás
|
||||
# Példa: Minden 100 Social pont automatikusan 1 Kredit lesz
|
||||
stats.social_points += social_amount
|
||||
if stats.social_points >= 100:
|
||||
new_credits = stats.social_points // 100
|
||||
stats.credits += new_credits
|
||||
stats.social_points %= 100 # A maradék megmarad a következő váltáshoz
|
||||
|
||||
stats.total_points += points
|
||||
await db.flush()
|
||||
return stats.total_points
|
||||
# Külön log a váltásról
|
||||
db.add(PointsLedger(user_id=user_id, reason=f"Auto-conversion: {new_credits} Credits", credits_change=new_credits))
|
||||
|
||||
await db.commit()
|
||||
return stats
|
||||
Reference in New Issue
Block a user