mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-10 09:05:48 +02:00
Add stats for cf
This commit is contained in:
parent
de7b5cd960
commit
a051423203
8 changed files with 230 additions and 3 deletions
34
database/crud/cf_stats.py
Normal file
34
database/crud/cf_stats.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.schemas import CFStats
|
||||
|
||||
__all__ = ["get_cf_stats", "update_cf_stats"]
|
||||
|
||||
|
||||
async def get_cf_stats(session: AsyncSession, user_id: int) -> CFStats:
|
||||
"""Get a user's coinflip stats"""
|
||||
statement = select(CFStats).where(CFStats.user_id == user_id)
|
||||
result = (await session.execute(statement)).scalar_one_or_none()
|
||||
|
||||
if result is None:
|
||||
result = CFStats(user_id=user_id)
|
||||
session.add(result)
|
||||
await session.commit()
|
||||
await session.refresh(result)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
async def update_cf_stats(session: AsyncSession, user_id: int, outcome: int):
|
||||
"""Update a user's coinflip stats"""
|
||||
stats = await get_cf_stats(session, user_id)
|
||||
if outcome < 0:
|
||||
stats.games_lost += 1
|
||||
stats.dinks_lost += abs(outcome)
|
||||
else:
|
||||
stats.games_won += 1
|
||||
stats.dinks_won += outcome
|
||||
|
||||
session.add(stats)
|
||||
await session.commit()
|
||||
|
|
@ -15,6 +15,7 @@ __all__ = [
|
|||
"BankSavings",
|
||||
"Birthday",
|
||||
"Bookmark",
|
||||
"CFStats",
|
||||
"CommandStats",
|
||||
"CustomCommand",
|
||||
"CustomCommandAlias",
|
||||
|
|
@ -106,6 +107,21 @@ class Bookmark(Base):
|
|||
user: Mapped[User] = relationship(back_populates="bookmarks", uselist=False, lazy="selectin")
|
||||
|
||||
|
||||
class CFStats(Base):
|
||||
"""A user's coinflipping stats"""
|
||||
|
||||
__tablename__ = "cf_stats"
|
||||
|
||||
cf_stats_id: Mapped[int] = mapped_column(primary_key=True)
|
||||
games_won: Mapped[int] = mapped_column(BigInteger, server_default="0", nullable=False)
|
||||
games_lost: Mapped[int] = mapped_column(BigInteger, server_default="0", nullable=False)
|
||||
dinks_won: Mapped[int] = mapped_column(BigInteger, server_default="0", nullable=False)
|
||||
dinks_lost: Mapped[int] = mapped_column(BigInteger, server_default="0", nullable=False)
|
||||
user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("users.user_id"))
|
||||
|
||||
user: Mapped[User] = relationship(back_populates="cf_stats", uselist=False, lazy="selectin")
|
||||
|
||||
|
||||
class CommandStats(Base):
|
||||
"""Metrics on how often commands are used"""
|
||||
|
||||
|
|
@ -349,6 +365,9 @@ class User(Base):
|
|||
bookmarks: Mapped[List[Bookmark]] = relationship(
|
||||
back_populates="user", uselist=True, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
cf_stats: Mapped[CFStats] = relationship(
|
||||
back_populates="user", uselist=True, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
command_stats: Mapped[List[CommandStats]] = relationship(
|
||||
back_populates="user", uselist=True, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue