Investing

pull/119/head
stijndcl 2022-07-03 18:35:30 +02:00
parent fff35c6c44
commit 8da0eb0b2a
3 changed files with 52 additions and 16 deletions

View File

@ -1,4 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Union
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@ -17,6 +18,23 @@ async def get_bank(session: AsyncSession, user_id: int) -> Bank:
return user.bank return user.bank
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
amount = int(amount)
bank.dinks -= amount
bank.invested += amount
session.add(bank)
await session.commit()
return amount
async def add_dinks(session: AsyncSession, user_id: int, amount: int): async def add_dinks(session: AsyncSession, user_id: int, amount: int):
"""Increase the Dinks counter for a user""" """Increase the Dinks counter for a user"""
bank = await get_bank(session, user_id) bank = await get_bank(session, user_id)
@ -44,9 +62,9 @@ async def claim_nightly(session: AsyncSession, user_id: int):
await session.commit() await session.commit()
async def upgrade_capacity(database_session: AsyncSession, user_id: int) -> int: async def upgrade_capacity(session: AsyncSession, user_id: int) -> int:
"""Upgrade capacity level""" """Upgrade capacity level"""
bank = await get_bank(database_session, user_id) bank = await get_bank(session, user_id)
upgrade_price = capacity_upgrade_price(bank.capacity_level) upgrade_price = capacity_upgrade_price(bank.capacity_level)
# Can't afford this upgrade # Can't afford this upgrade
@ -56,15 +74,15 @@ async def upgrade_capacity(database_session: AsyncSession, user_id: int) -> int:
bank.dinks -= upgrade_price bank.dinks -= upgrade_price
bank.capacity_level += 1 bank.capacity_level += 1
database_session.add(bank) session.add(bank)
await database_session.commit() await session.commit()
return bank.capacity_level return bank.capacity_level
async def upgrade_interest(database_session: AsyncSession, user_id: int) -> int: async def upgrade_interest(session: AsyncSession, user_id: int) -> int:
"""Upgrade interest level""" """Upgrade interest level"""
bank = await get_bank(database_session, user_id) bank = await get_bank(session, user_id)
upgrade_price = interest_upgrade_price(bank.interest_level) upgrade_price = interest_upgrade_price(bank.interest_level)
# Can't afford this upgrade # Can't afford this upgrade
@ -74,15 +92,15 @@ async def upgrade_interest(database_session: AsyncSession, user_id: int) -> int:
bank.dinks -= upgrade_price bank.dinks -= upgrade_price
bank.interest_level += 1 bank.interest_level += 1
database_session.add(bank) session.add(bank)
await database_session.commit() await session.commit()
return bank.interest_level return bank.interest_level
async def upgrade_rob(database_session: AsyncSession, user_id: int) -> int: async def upgrade_rob(session: AsyncSession, user_id: int) -> int:
"""Upgrade rob level""" """Upgrade rob level"""
bank = await get_bank(database_session, user_id) bank = await get_bank(session, user_id)
upgrade_price = rob_upgrade_price(bank.rob_level) upgrade_price = rob_upgrade_price(bank.rob_level)
# Can't afford this upgrade # Can't afford this upgrade
@ -92,7 +110,7 @@ async def upgrade_rob(database_session: AsyncSession, user_id: int) -> int:
bank.dinks -= upgrade_price bank.dinks -= upgrade_price
bank.rob_level += 1 bank.rob_level += 1
database_session.add(bank) session.add(bank)
await database_session.commit() await session.commit()
return bank.rob_level return bank.rob_level

View File

@ -113,9 +113,25 @@ class Currency(commands.Cog):
plural = pluralize("Didier Dink", bank.dinks) plural = pluralize("Didier Dink", bank.dinks)
await ctx.reply(f"**{ctx.author.display_name}** heeft **{bank.dinks}** {plural}.", mention_author=False) await ctx.reply(f"**{ctx.author.display_name}** heeft **{bank.dinks}** {plural}.", mention_author=False)
@commands.command(name="Invest")
async def invest(self, ctx: commands.Context, amount: abbreviated_number): # type: ignore
"""Invest a given amount of Didier Dinks"""
amount = typing.cast(typing.Union[str, int], amount)
async with self.client.db_session as session:
invested = await crud.invest(session, ctx.author.id, amount)
plural = pluralize("Didier Dink", invested)
if invested == 0:
await ctx.reply("Je hebt geen Didier Dinks om te investeren.", mention_author=False)
else:
await ctx.reply(
f"**{ctx.author.display_name}** heeft **{invested}** {plural} geïnvesteerd.", mention_author=False
)
@commands.hybrid_command(name="nightly") @commands.hybrid_command(name="nightly")
async def nightly(self, ctx: commands.Context): async def nightly(self, ctx: commands.Context):
"""Claim nightly Dinks""" """Claim nightly Didier Dinks"""
async with self.client.db_session as session: async with self.client.db_session as session:
try: try:
await crud.claim_nightly(session, ctx.author.id) await crud.claim_nightly(session, ctx.author.id)

View File

@ -1,16 +1,18 @@
import math import math
from typing import Optional from typing import Optional, Union
__all__ = ["abbreviated_number"] __all__ = ["abbreviated_number"]
def abbreviated_number(argument: str) -> int: def abbreviated_number(argument: str) -> Union[str, int]:
"""Custom converter to allow numbers to be abbreviated """Custom converter to allow numbers to be abbreviated
Examples: Examples:
515k 515k
4m 4m
""" """
if argument.lower() == "all":
return "all"
if not argument: if not argument:
raise ValueError raise ValueError