Skip to content

Commit

Permalink
Merge pull request #181 from stijndcl/return-dinks
Browse files Browse the repository at this point in the history
Re-enable some currency commands
  • Loading branch information
stijndcl authored Feb 12, 2024
2 parents f181a7a + cc75dd8 commit 35d1d87
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 21 deletions.
37 changes: 36 additions & 1 deletion database/crud/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
__all__ = [
"add_dinks",
"claim_nightly",
"gamble_dinks",
"get_bank",
"get_nightly_data",
"invest",
"upgrade_capacity",
"upgrade_interest",
"upgrade_rob",
"withdraw",
"NIGHTLY_AMOUNT",
]

Expand All @@ -40,7 +42,7 @@ async def get_nightly_data(session: AsyncSession, user_id: int) -> NightlyData:


async def invest(session: AsyncSession, user_id: int, amount: Union[str, int]) -> int:
"""Invest all your Dinks"""
"""Invest some of your Dinks"""
bank = await get_bank(session, user_id)
if amount == "all":
amount = bank.dinks
Expand All @@ -57,6 +59,22 @@ async def invest(session: AsyncSession, user_id: int, amount: Union[str, int]) -
return amount


async def withdraw(session: AsyncSession, user_id: int, amount: Union[str, int]) -> int:
"""Withdraw your invested Dinks"""
bank = 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

await session.commit()
return amount


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)
Expand Down Expand Up @@ -135,3 +153,20 @@ async def upgrade_rob(session: AsyncSession, user_id: int) -> int:
await session.commit()

return bank.rob_level


async def gamble_dinks(
session: AsyncSession, user_id: int, amount: Union[str, int], payout_factor: int, won: bool
) -> int:
"""Gamble some of your Dinks"""
bank = await get_bank(session, user_id)
if amount == "all":
amount = bank.dinks

amount = min(int(amount), bank.dinks)

sign = 1 if won else -1
factor = (payout_factor - 1) if won else 1
await add_dinks(session, user_id, sign * amount * factor)

return amount * factor
46 changes: 33 additions & 13 deletions didier/cogs/currency.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa: E800
import typing

import discord
Expand Down Expand Up @@ -38,7 +39,7 @@ async def award(
await crud.add_dinks(session, user.id, amount)
plural = pluralize("Didier Dink", amount)
await ctx.reply(
f"**{ctx.author.display_name}** has awarded **{user.display_name}** **{amount}** {plural}.",
f"**{ctx.author.display_name}** has awarded **{user.display_name}** with **{amount}** {plural}.",
mention_author=False,
)

Expand All @@ -48,7 +49,8 @@ async def bank(self, ctx: commands.Context):
async with self.client.postgres_session as session:
bank = await crud.get_bank(session, ctx.author.id)

embed = discord.Embed(title=f"{ctx.author.display_name}'s Bank", colour=discord.Colour.blue())
embed = discord.Embed(title="Bank of Didier", colour=discord.Colour.blue())
embed.set_author(name=ctx.author.display_name)

if ctx.author.avatar is not None:
embed.set_thumbnail(url=ctx.author.avatar.url)
Expand All @@ -59,9 +61,9 @@ async def bank(self, ctx: commands.Context):

await ctx.reply(embed=embed, mention_author=False)

@bank.group( # type: ignore[arg-type]
name="upgrade", aliases=["u", "upgrades"], case_insensitive=True, invoke_without_command=True
)
# @bank.group( # type: ignore[arg-type]
# name="upgrade", aliases=["u", "upgrades"], case_insensitive=True, invoke_without_command=True
# )
async def bank_upgrades(self, ctx: commands.Context):
"""List the upgrades you can buy & their prices."""
async with self.client.postgres_session as session:
Expand All @@ -81,7 +83,7 @@ async def bank_upgrades(self, ctx: commands.Context):

await ctx.reply(embed=embed, mention_author=False)

@bank_upgrades.command(name="capacity", aliases=["c"]) # type: ignore[arg-type]
# @bank_upgrades.command(name="capacity", aliases=["c"]) # type: ignore[arg-type]
async def bank_upgrade_capacity(self, ctx: commands.Context):
"""Upgrade the capacity level of your bank."""
async with self.client.postgres_session as session:
Expand All @@ -92,7 +94,7 @@ async def bank_upgrade_capacity(self, ctx: commands.Context):
await ctx.reply("You don't have enough Didier Dinks to do this.", mention_author=False)
await self.client.reject_message(ctx.message)

@bank_upgrades.command(name="interest", aliases=["i"]) # type: ignore[arg-type]
# @bank_upgrades.command(name="interest", aliases=["i"]) # type: ignore[arg-type]
async def bank_upgrade_interest(self, ctx: commands.Context):
"""Upgrade the interest level of your bank."""
async with self.client.postgres_session as session:
Expand All @@ -103,7 +105,7 @@ async def bank_upgrade_interest(self, ctx: commands.Context):
await ctx.reply("You don't have enough Didier Dinks to do this.", mention_author=False)
await self.client.reject_message(ctx.message)

@bank_upgrades.command(name="rob", aliases=["r"]) # type: ignore[arg-type]
# @bank_upgrades.command(name="rob", aliases=["r"]) # type: ignore[arg-type]
async def bank_upgrade_rob(self, ctx: commands.Context):
"""Upgrade the rob level of your bank."""
async with self.client.postgres_session as session:
Expand All @@ -122,12 +124,12 @@ async def dinks(self, ctx: commands.Context):
plural = pluralize("Didier Dink", bank.dinks)
await ctx.reply(f"**{ctx.author.display_name}** has **{bank.dinks}** {plural}.", mention_author=False)

@commands.command(name="invest", aliases=["deposit", "dep"]) # type: ignore[arg-type]
@commands.command(name="invest", aliases=["deposit", "dep", "i"]) # type: ignore[arg-type]
async def invest(self, ctx: commands.Context, amount: typing.Annotated[typing.Union[str, int], abbreviated_number]):
"""Invest `amount` Didier Dinks into your bank.
The `amount`-argument can take both raw numbers, and abbreviations of big numbers. Additionally, passing
`all` as the value will invest all of your Didier Dinks.
`all` or `*` as the value will invest all of your Didier Dinks.
Example usage:
```
Expand All @@ -137,16 +139,34 @@ async def invest(self, ctx: commands.Context, amount: typing.Annotated[typing.Un
didier invest 5.3b
```
"""
if isinstance(amount, int) and amount <= 0:
return await ctx.reply("Amount of Didier Dinks to invest must be a strictly positive integer.")

async with self.client.postgres_session as session:
invested = await crud.invest(session, ctx.author.id, amount)
plural = pluralize("Didier Dink", invested)

if invested == 0:
await ctx.reply("You don't have any Didier Dinks to invest.", mention_author=False)
else:
await ctx.reply(
f"**{ctx.author.display_name}** has invested **{invested}** {plural}.", mention_author=False
)
await ctx.reply(f"You have invested **{invested}** {plural}.", mention_author=False)

@commands.command(name="withdraw", aliases=["uninvest", "w"]) # type: ignore[arg-type]
async def withdraw(
self, ctx: commands.Context, amount: typing.Annotated[typing.Union[str, int], abbreviated_number]
):
"""Withdraw some of your invested Didier Dinks from your bank."""
if isinstance(amount, int) and amount <= 0:
return await ctx.reply("Amount of Didier Dinks to invest must be a strictly positive integer.")

async with self.client.postgres_session as session:
withdrawn = await crud.withdraw(session, ctx.author.id, amount)
plural = pluralize("Didier Dink", withdrawn)

if withdrawn == 0:
await ctx.reply("You don't have any Didier Dinks to withdraw.", mention_author=False)
else:
await ctx.reply(f"You have withdrawn **{withdrawn}** {plural}.", mention_author=False)

@commands.hybrid_command(name="nightly") # type: ignore[arg-type]
async def nightly(self, ctx: commands.Context):
Expand Down
73 changes: 73 additions & 0 deletions didier/cogs/gambling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import random
from typing import Annotated, Optional, Union

from discord.ext import commands

from database.crud.currency import gamble_dinks
from didier import Didier
from didier.utils.discord.converters import abbreviated_number
from didier.utils.types.string import pluralize


class Gambling(commands.Cog):
"""Cog for various games"""

client: Didier

def __init__(self, client: Didier):
self.client = client

@commands.command(name="coinflip", aliases=["cf", "flip"]) # type: ignore[arg-type]
async def coinflip(
self,
ctx: commands.Context,
amount: Optional[Annotated[Union[str, int], abbreviated_number]] = None,
guess: Optional[str] = None,
):
"""Toss a coin, optionally wagering some Didier Dinks.
Passing an argument for `amount` but not `guess` will cause the guess to be randomized.
"""
result: str = random.choice(["heads", "tails"])

# No stakes
if amount is None:
return await ctx.reply(f"{result.capitalize()}!", mention_author=False)

if guess is None:
guess = random.choice(["heads", "tails"])

guess = guess.lower()

if guess not in (
"h",
"heads",
"t",
"tails",
):
return await ctx.reply('Guess must be one of "h", "heads", "t" or "tails".', mention_author=False)

if isinstance(amount, int) and amount <= 0:
return await ctx.reply(
"Amount of Didier Dinks to wager must be a strictly positive integer.", mention_author=False
)

won = guess[0] == result[0]

async with self.client.postgres_session as session:
received = await gamble_dinks(session, ctx.author.id, amount, 2, won)

if received == 0:
return await ctx.reply("You don't have any Didier Dinks to wager.", mention_author=False)

plural = pluralize("Didier Dink", received)

if won:
await ctx.reply(f"{result.capitalize()}! You won **{received}** {plural}!", mention_author=False)
else:
await ctx.reply(f"{result.capitalize()}! You lost **{received}** {plural}!", mention_author=False)


async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Gambling(client))
45 changes: 43 additions & 2 deletions didier/cogs/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,55 @@ async def _error(self, ctx: commands.Context, *, message: str = "Debug"):
"""Raise an exception for debugging purposes"""
raise Exception(message)

@commands.command(name="unload")
async def unload(self, ctx: commands.Context, *cogs: str):
"""Unload the cogs passed as an argument"""
unloaded = []
skipped = []

for cog in cogs:
try:
await self.client.unload_extension(f"didier.cogs.{cog}")
unloaded.append(cog)
except commands.ExtensionNotLoaded:
skipped.append(cog)

await self.client.confirm_message(ctx.message)
loaded_message = ", ".join(unloaded) if unloaded else "none"
skipped_message = ", ".join(skipped) if skipped else "none"

await ctx.reply(f"Successfully unloaded {loaded_message} (skipped {skipped_message}).", mention_author=False)

@commands.command(name="load")
async def load(self, ctx: commands.Context, *cogs: str):
"""Load the cogs passed as an argument"""
loaded = []
skipped = []

for cog in cogs:
try:
await self.client.load_extension(f"didier.cogs.{cog}")
loaded.append(cog)
except commands.ExtensionAlreadyLoaded:
skipped.append(cog)

await self.client.confirm_message(ctx.message)
loaded_message = ", ".join(loaded) if loaded else "none"
skipped_message = ", ".join(skipped) if skipped else "none"

await ctx.reply(f"Successfully loaded {loaded_message} (skipped {skipped_message}).", mention_author=False)

@commands.command(name="Reload")
async def reload(self, ctx: commands.Context, *cogs: str):
"""Reload the cogs passed as an argument"""
for cog in cogs:
await self.client.reload_extension(f"didier.cogs.{cog}")
try:
await self.client.reload_extension(f"didier.cogs.{cog}")
except commands.ExtensionNotLoaded:
await self.client.load_extension(f"didier.cogs.{cog}")

await self.client.confirm_message(ctx.message)
return await ctx.reply(f"Successfully reloaded {', '.join(cogs)}.", mention_author=False)
await ctx.reply(f"Successfully reloaded {', '.join(cogs)}.", mention_author=False)

@commands.command(name="Sync")
async def sync(
Expand Down
4 changes: 0 additions & 4 deletions didier/didier.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ async def _load_directory_extensions(self, path: str):
# Allow checking against initial extensions more easily
full_name = parent_path + file

# The currency cog is disabled
if "currency" in file:
continue

# Only take Python files, and ignore the ones starting with an underscore (like __init__ and __pycache__)
# Also ignore the files that we have already loaded previously
if file.endswith(".py") and not file.startswith("_") and not full_name.startswith(self.initial_extensions):
Expand Down
2 changes: 1 addition & 1 deletion didier/utils/discord/converters/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def abbreviated_number(argument: str) -> Union[str, int]:
if not argument:
raise ValueError

if argument.lower() == "all":
if argument.lower() == "all" or argument == "*":
return "all"

if argument.isdecimal():
Expand Down

0 comments on commit 35d1d87

Please sign in to comment.