Work on rob

This commit is contained in:
stijndcl 2024-02-20 16:21:51 +01:00
parent d46a31a7be
commit a21eb51e6d
7 changed files with 203 additions and 8 deletions

View file

@ -47,6 +47,9 @@ async def invest(session: AsyncSession, user_id: int, amount: Union[str, int]) -
if amount == "all":
amount = bank.dinks
if bank.dinks <= 0:
return 0
# Don't allow investing more dinks than you own
amount = min(bank.dinks, int(amount))
@ -158,11 +161,14 @@ async def upgrade_rob(session: AsyncSession, user_id: int) -> int:
async def gamble_dinks(
session: AsyncSession, user_id: int, amount: Union[str, int], payout_factor: int, won: bool
) -> int:
"""Gamble some of your Dinks"""
"""Gamble some of your Didier Dinks"""
bank = await get_bank(session, user_id)
if amount == "all":
amount = bank.dinks
if bank.dinks <= 0:
return 0
amount = min(int(amount), bank.dinks)
sign = 1 if won else -1
@ -170,3 +176,16 @@ async def gamble_dinks(
await add_dinks(session, user_id, sign * amount * factor)
return amount * factor
async def rob(session: AsyncSession, amount: int, robber_id: int, robbed_id: int):
"""Rob another user's Didier Dinks"""
robber = await get_bank(session, robber_id)
robbed = await get_bank(session, robbed_id)
robber.dinks += amount
robbed.dinks -= amount
session.add(robber)
session.add(robbed)
await session.commit()

28
database/crud/jail.py Normal file
View file

@ -0,0 +1,28 @@
from datetime import datetime
from typing import Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from database.schemas import Jail
__all__ = ["get_jail", "get_user_jail", "imprison"]
async def get_jail(session: AsyncSession) -> list[Jail]:
"""Get the entire Didier Jail"""
statement = select(Jail)
return list((await session.execute(statement)).scalars().all())
async def get_user_jail(session: AsyncSession, user_id: int) -> Optional[Jail]:
"""Check how long a given user is still in jail for"""
statement = select(Jail).where(Jail.user_id == user_id)
return (await session.execute(statement)).scalar_one_or_none()
async def imprison(session: AsyncSession, user_id: int, until: datetime):
"""Put a user in Didier Jail"""
jail = Jail(user_id=user_id, until=until)
session.add(jail)
await session.commit()

View file

@ -23,6 +23,7 @@ __all__ = [
"Event",
"FreeGame",
"GitHubLink",
"Jail",
"Link",
"MemeTemplate",
"NightlyData",
@ -199,6 +200,18 @@ class GitHubLink(Base):
user: Mapped[User] = relationship(back_populates="github_links", uselist=False, lazy="selectin")
class Jail(Base):
"""A user sitting in Didier Jail"""
__tablename__ = "jail"
jail_entry_i: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("users.user_id"))
until: Mapped[datetime] = mapped_column(nullable=False)
user: Mapped[User] = relationship(back_populates="jail", uselist=False, lazy="selectin")
class Link(Base):
"""Useful links that go useful places"""
@ -328,6 +341,9 @@ class User(Base):
github_links: Mapped[List[GitHubLink]] = relationship(
back_populates="user", uselist=True, lazy="selectin", cascade="all, delete-orphan"
)
jail: Mapped[Optional[Jail]] = relationship(
back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
)
nightly_data: Mapped[NightlyData] = relationship(
back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
)

View file

@ -1,6 +1,14 @@
import math
__all__ = ["capacity_upgrade_price", "interest_upgrade_price", "rob_upgrade_price"]
__all__ = [
"capacity_upgrade_price",
"interest_upgrade_price",
"rob_upgrade_price",
"jail_chance",
"jail_time",
"rob_amount",
"rob_chance",
]
def interest_upgrade_price(level: int) -> int:
@ -25,3 +33,34 @@ def rob_upgrade_price(level: int) -> int:
growth_rate = 1.9
return math.floor(base_cost * (growth_rate**level))
def jail_chance(level: int) -> float:
"""Calculate the chance that you'll end up in jail"""
base_chance = 0.35
growth_rate = 1.15
return max(0.0, base_chance - (growth_rate**level))
def jail_time(level: int) -> int:
"""Calculate the time in hours you'll have to spend in jail"""
base_hours = 2
growth_rate = 1.27
return math.floor(base_hours + growth_rate**level)
def rob_amount(level: int) -> int:
"""Calculate the maximum amount of Didier Dinks that you can rob"""
base_amount = 250
growth_rate = 1.4
return math.floor(base_amount * (growth_rate**level))
def rob_chance(level: int) -> float:
"""Calculate the chances of a robbing attempt being successful"""
base_chance = 0.25
return base_chance + 2.1 * level