2022-07-23 20:59:02 +02:00
|
|
|
from datetime import date
|
2022-07-03 18:35:30 +02:00
|
|
|
from typing import Union
|
2022-06-30 21:17:48 +02:00
|
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
from database.crud import users
|
|
|
|
from database.exceptions import currency as exceptions
|
2022-08-29 20:24:42 +02:00
|
|
|
from database.schemas import Bank, NightlyData
|
2022-07-11 22:23:38 +02:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"add_dinks",
|
|
|
|
"claim_nightly",
|
|
|
|
"get_bank",
|
|
|
|
"get_nightly_data",
|
|
|
|
"invest",
|
|
|
|
"NIGHTLY_AMOUNT",
|
|
|
|
]
|
2022-06-30 21:17:48 +02:00
|
|
|
|
2023-09-17 17:11:38 +02:00
|
|
|
NIGHTLY_AMOUNT = 50
|
2022-06-30 21:17:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def get_bank(session: AsyncSession, user_id: int) -> Bank:
|
|
|
|
"""Get a user's bank info"""
|
2022-08-29 20:49:29 +02:00
|
|
|
user = await users.get_or_add_user(session, user_id)
|
2022-06-30 21:17:48 +02:00
|
|
|
return user.bank
|
|
|
|
|
|
|
|
|
2022-07-03 19:26:30 +02:00
|
|
|
async def get_nightly_data(session: AsyncSession, user_id: int) -> NightlyData:
|
|
|
|
"""Get a user's nightly info"""
|
2022-08-29 20:49:29 +02:00
|
|
|
user = await users.get_or_add_user(session, user_id)
|
2022-07-03 19:26:30 +02:00
|
|
|
return user.nightly_data
|
|
|
|
|
|
|
|
|
2022-07-03 18:35:30 +02:00
|
|
|
async def invest(session: AsyncSession, user_id: int, amount: Union[str, int]) -> int:
|
|
|
|
"""Invest all your Dinks"""
|
|
|
|
bank = await get_bank(session, user_id)
|
|
|
|
if amount == "all":
|
|
|
|
amount = bank.dinks
|
|
|
|
|
2022-07-03 19:26:30 +02:00
|
|
|
# Don't allow investing more dinks than you own
|
|
|
|
amount = min(bank.dinks, int(amount))
|
2022-07-03 18:35:30 +02:00
|
|
|
|
|
|
|
bank.dinks -= amount
|
|
|
|
bank.invested += amount
|
|
|
|
|
|
|
|
session.add(bank)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
return amount
|
|
|
|
|
|
|
|
|
2022-06-30 21:17:48 +02:00
|
|
|
async def add_dinks(session: AsyncSession, user_id: int, amount: int):
|
|
|
|
"""Increase the Dinks counter for a user"""
|
|
|
|
bank = await get_bank(session, user_id)
|
|
|
|
bank.dinks += amount
|
|
|
|
session.add(bank)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
async def claim_nightly(session: AsyncSession, user_id: int):
|
|
|
|
"""Claim daily Dinks"""
|
2022-07-03 19:26:30 +02:00
|
|
|
nightly_data = await get_nightly_data(session, user_id)
|
2022-06-30 21:17:48 +02:00
|
|
|
|
2022-07-23 20:59:02 +02:00
|
|
|
now = date.today()
|
2022-06-30 21:17:48 +02:00
|
|
|
|
2022-07-23 20:59:02 +02:00
|
|
|
if nightly_data.last_nightly is not None and nightly_data.last_nightly == now:
|
2022-06-30 21:17:48 +02:00
|
|
|
raise exceptions.DoubleNightly
|
|
|
|
|
2022-07-03 19:26:30 +02:00
|
|
|
bank = await get_bank(session, user_id)
|
2022-06-30 21:17:48 +02:00
|
|
|
bank.dinks += NIGHTLY_AMOUNT
|
|
|
|
nightly_data.last_nightly = now
|
|
|
|
|
|
|
|
session.add(bank)
|
|
|
|
session.add(nightly_data)
|
|
|
|
await session.commit()
|