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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue