didier/database/crud/currency.py

138 lines
3.4 KiB
Python
Raw Normal View History

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-07-03 19:26:30 +02:00
from database.models import Bank, NightlyData
2022-07-11 22:23:38 +02:00
from database.utils.math.currency import (
capacity_upgrade_price,
interest_upgrade_price,
rob_upgrade_price,
)
__all__ = [
"add_dinks",
"claim_nightly",
"get_bank",
"get_nightly_data",
"invest",
"upgrade_capacity",
"upgrade_interest",
"upgrade_rob",
"NIGHTLY_AMOUNT",
]
2022-06-30 21:17:48 +02:00
NIGHTLY_AMOUNT = 420
async def get_bank(session: AsyncSession, user_id: int) -> Bank:
"""Get a user's bank info"""
user = await users.get_or_add(session, user_id)
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"""
user = await users.get_or_add(session, user_id)
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()
2022-07-03 17:44:16 +02:00
2022-07-03 18:35:30 +02:00
async def upgrade_capacity(session: AsyncSession, user_id: int) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade capacity level"""
2022-07-03 18:35:30 +02:00
bank = await get_bank(session, user_id)
2022-07-03 17:44:16 +02:00
upgrade_price = capacity_upgrade_price(bank.capacity_level)
# Can't afford this upgrade
if upgrade_price > bank.dinks:
raise exceptions.NotEnoughDinks
bank.dinks -= upgrade_price
bank.capacity_level += 1
2022-07-03 18:35:30 +02:00
session.add(bank)
await session.commit()
2022-07-03 17:44:16 +02:00
return bank.capacity_level
2022-07-03 18:35:30 +02:00
async def upgrade_interest(session: AsyncSession, user_id: int) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade interest level"""
2022-07-03 18:35:30 +02:00
bank = await get_bank(session, user_id)
2022-07-03 17:44:16 +02:00
upgrade_price = interest_upgrade_price(bank.interest_level)
# Can't afford this upgrade
if upgrade_price > bank.dinks:
raise exceptions.NotEnoughDinks
bank.dinks -= upgrade_price
bank.interest_level += 1
2022-07-03 18:35:30 +02:00
session.add(bank)
await session.commit()
2022-07-03 17:44:16 +02:00
return bank.interest_level
2022-07-03 18:35:30 +02:00
async def upgrade_rob(session: AsyncSession, user_id: int) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade rob level"""
2022-07-03 18:35:30 +02:00
bank = await get_bank(session, user_id)
2022-07-03 17:44:16 +02:00
upgrade_price = rob_upgrade_price(bank.rob_level)
# Can't afford this upgrade
if upgrade_price > bank.dinks:
raise exceptions.NotEnoughDinks
bank.dinks -= upgrade_price
bank.rob_level += 1
2022-07-03 18:35:30 +02:00
session.add(bank)
await session.commit()
2022-07-03 17:44:16 +02:00
return bank.rob_level