Spaces:
Running on Zero
Running on Zero
CIPHΞR commited on
Update xp.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041from datetime import datetimefrom typing import Optionalfrom sqlalchemy import BigInteger, DateTime, Integer, String, UniqueConstraint, funcfrom sqlalchemy.orm import Mapped, mapped_columnfrom app.db.base import Baseclass XpEvent(Base):"""Immutable XP ledger. users.points / week_xp are only a fast cache of this table.UNIQUE(user_id, kind, ref) makes every rewarded action idempotent (no double pay)."""__tablename__ = "xp_events"__table_args__ = (UniqueConstraint("user_id", "kind", "ref", name="uq_xp_idem"),)id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)user_id: Mapped[int] = mapped_column(BigInteger, index=True)kind: Mapped[str] = mapped_column(String(24))amount: Mapped[int] = mapped_column(Integer)ref: Mapped[Optional[str]] = mapped_column(String(64))week: Mapped[str] = mapped_column(String(8), index=True)day: Mapped[str] = mapped_column(String(10))created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())class SeasonResult(Base):"""One row per winner per weekly season, plus a 'closed' marker (rank 0) per week."""__tablename__ = "season_results"__table_args__ = (UniqueConstraint("week", "kind", "rank", name="uq_season_slot"),)id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)week: Mapped[str] = mapped_column(String(8), index=True)kind: Mapped[str] = mapped_column(String(8)) # top | raffle | closedrank: Mapped[int] = mapped_column(Integer, default=0)user_id: Mapped[int] = mapped_column(BigInteger, default=0)xp: Mapped[int] = mapped_column(Integer, default=0)prize_birr: Mapped[int] = mapped_column(Integer, default=0)status: Mapped[str] = mapped_column(String(12), default="info") # info | pending | paidcreated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141from datetime import datetimefrom typing import Optionalfrom sqlalchemy import (BigInteger,DateTime,Integer,String,UniqueConstraint,func,)from sqlalchemy.orm import Mapped, mapped_columnfrom app.db.base import Baseclass XpEvent(Base):"""Immutable XP ledger.users.points / week_xp are only a fast cache of this table.UNIQUE(user_id, kind, ref) ensures every rewarded actionis idempotent and cannot be paid twice.The legacy 'week' column stores the 60-day season key."""__tablename__ = "xp_events"__table_args__ = (UniqueConstraint("user_id","kind","ref",name="uq_xp_idem",),)id: Mapped[int] = mapped_column(Integer,primary_key=True,autoincrement=True,)user_id: Mapped[int] = mapped_column(BigInteger,index=True,)kind: Mapped[str] = mapped_column(String(24),)amount: Mapped[int] = mapped_column(Integer,)ref: Mapped[Optional[str]] = mapped_column(String(64),)# 60-day season key: S60-YYYY-MM-DD (14 characters)week: Mapped[str] = mapped_column(String(16),index=True,)day: Mapped[str] = mapped_column(String(10),)created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True),server_default=func.now(),)class SeasonResult(Base):"""One row per winner per 60-day season,plus a 'closed' marker (rank 0) per season."""__tablename__ = "season_results"__table_args__ = (UniqueConstraint("week","kind","rank",name="uq_season_slot",),)id: Mapped[int] = mapped_column(Integer,primary_key=True,autoincrement=True,)# 60-day season key: S60-YYYY-MM-DD (14 characters)week: Mapped[str] = mapped_column(String(16),index=True,)kind: Mapped[str] = mapped_column(String(8),) # top | raffle | closedrank: Mapped[int] = mapped_column(Integer,default=0,)user_id: Mapped[int] = mapped_column(BigInteger,default=0,)xp: Mapped[int] = mapped_column(Integer,default=0,)prize_birr: Mapped[int] = mapped_column(Integer,default=0,)status: Mapped[str] = mapped_column(String(12),default="info",) # info | pending | paidcreated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True),server_default=func.now(),)
app/db/models/xp.py+124-24