didier/database/crud/currency.py

221 lines
5.7 KiB
Python
Raw Normal View History

2022-07-23 20:59:02 +02:00
from datetime import date
2024-02-20 18:20:41 +01:00
from typing import Optional, 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
from database.utils.math.currency import (
capacity_upgrade_price,
interest_upgrade_price,
rob_upgrade_price,
)
__all__ = [
"add_dinks",
"claim_nightly",
2024-02-20 18:20:41 +01:00
"deduct_dinks",
"gamble_dinks",
2022-07-11 22:23:38 +02:00
"get_bank",
"get_nightly_data",
"invest",
"upgrade_capacity",
"upgrade_interest",
"upgrade_rob",
"withdraw",
2022-07-11 22:23:38 +02:00
"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"""
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
2024-02-20 18:20:41 +01:00
async def invest(session: AsyncSession, user_id: int, amount: Union[str, int], *, bank: Optional[Bank] = None) -> int:
"""Invest some of your Dinks"""
2024-02-20 18:20:41 +01:00
bank = bank or await get_bank(session, user_id)
2022-07-03 18:35:30 +02:00
if amount == "all":
amount = bank.dinks
2024-02-20 16:21:51 +01:00
if bank.dinks <= 0:
return 0
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
2024-02-20 18:20:41 +01:00
async def withdraw(session: AsyncSession, user_id: int, amount: Union[str, int], *, bank: Optional[Bank] = None) -> int:
"""Withdraw your invested Dinks"""
2024-02-20 18:20:41 +01:00
bank = bank or await get_bank(session, user_id)
if amount == "all":
amount = bank.invested
# Don't allow withdrawing more dinks than you own
amount = min(bank.invested, int(amount))
bank.dinks += amount
bank.invested -= amount
2024-02-20 18:20:41 +01:00
session.add(bank)
await session.commit()
return amount
2024-02-20 18:20:41 +01:00
async def add_dinks(session: AsyncSession, user_id: int, amount: int, *, bank: Optional[Bank] = None):
2022-06-30 21:17:48 +02:00
"""Increase the Dinks counter for a user"""
2024-02-20 18:20:41 +01:00
bank = bank or await get_bank(session, user_id)
2022-06-30 21:17:48 +02:00
bank.dinks += amount
session.add(bank)
await session.commit()
2024-02-20 18:20:41 +01:00
async def deduct_dinks(session: AsyncSession, user_id: int, amount: int, *, bank: Optional[Bank] = None) -> int:
"""Decrease the Dinks counter for a user"""
bank = bank or await get_bank(session, user_id)
deducted_amount = min(amount, bank.dinks)
bank.dinks -= deducted_amount
session.add(bank)
await session.commit()
return deducted_amount
async def claim_nightly(session: AsyncSession, user_id: int, *, bank: Optional[Bank] = None):
2022-06-30 21:17:48 +02:00
"""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
2024-02-20 18:20:41 +01:00
bank = bank or 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
2024-02-20 18:20:41 +01:00
async def upgrade_capacity(session: AsyncSession, user_id: int, *, bank: Optional[Bank] = None) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade capacity level"""
2024-02-20 18:20:41 +01:00
bank = bank or 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
2024-02-20 18:20:41 +01:00
async def upgrade_interest(session: AsyncSession, user_id: int, *, bank: Optional[Bank] = None) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade interest level"""
2024-02-20 18:20:41 +01:00
bank = bank or 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
2024-02-20 18:20:41 +01:00
async def upgrade_rob(session: AsyncSession, user_id: int, *, bank: Optional[Bank] = None) -> int:
2022-07-03 17:44:16 +02:00
"""Upgrade rob level"""
2024-02-20 18:20:41 +01:00
bank = bank or 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
async def gamble_dinks(
2024-02-20 18:20:41 +01:00
session: AsyncSession,
user_id: int,
amount: Union[str, int],
payout_factor: int,
won: bool,
*,
bank: Optional[Bank] = None
) -> int:
2024-02-20 16:21:51 +01:00
"""Gamble some of your Didier Dinks"""
2024-02-20 18:20:41 +01:00
bank = bank or await get_bank(session, user_id)
if amount == "all":
amount = bank.dinks
2024-02-20 16:21:51 +01:00
if bank.dinks <= 0:
return 0
2024-02-12 20:16:52 +01:00
amount = min(int(amount), bank.dinks)
sign = 1 if won else -1
factor = (payout_factor - 1) if won else 1
2024-02-20 18:20:41 +01:00
await add_dinks(session, user_id, sign * amount * factor, bank=bank)
return amount * factor
2024-02-20 16:21:51 +01:00
2024-02-20 18:20:41 +01:00
async def rob(
session: AsyncSession,
amount: int,
robber_id: int,
robbed_id: int,
*,
robber_bank: Optional[Bank] = None,
robbed_bank: Optional[Bank] = None
):
2024-02-20 16:21:51 +01:00
"""Rob another user's Didier Dinks"""
2024-02-20 18:20:41 +01:00
robber = robber_bank or await get_bank(session, robber_id)
robbed = robbed_bank or await get_bank(session, robbed_id)
2024-02-20 16:21:51 +01:00
robber.dinks += amount
robbed.dinks -= amount
session.add(robber)
session.add(robbed)
await session.commit()