From 6d636e4e4215eac05f473b5fe096baac43abafe1 Mon Sep 17 00:00:00 2001 From: justcool393 Date: Tue, 1 Nov 2022 00:25:19 -0500 Subject: [PATCH] use charge_account everywhere --- files/helpers/lottery.py | 3 ++- files/routes/awards.py | 2 -- files/routes/polls.py | 4 ++-- files/routes/posts.py | 5 +---- files/routes/settings.py | 7 ++++--- files/routes/subs.py | 3 +-- files/routes/users.py | 7 +++++-- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/files/helpers/lottery.py b/files/helpers/lottery.py index 3281eb73a..eb8310f8c 100644 --- a/files/helpers/lottery.py +++ b/files/helpers/lottery.py @@ -103,7 +103,8 @@ def purchase_lottery_tickets(v, quantity=1): if (most_recent_lottery is None): return False, "There is no active lottery." - v.charge_account('coins', LOTTERY_TICKET_COST * quantity) + if not v.charge_account('coins', LOTTERY_TICKET_COST * quantity): + return False, "You don't have enough coins" v.currently_held_lottery_tickets += quantity v.total_held_lottery_tickets += quantity diff --git a/files/routes/awards.py b/files/routes/awards.py index c249ceb2e..060f058d1 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -43,8 +43,6 @@ def shop(v): @auth_required @feature_required('AWARDS') def buy(v, award): - - if award == 'benefactor' and not request.values.get("mb"): abort(403, "You can only buy the Benefactor award with marseybux.") diff --git a/files/routes/polls.py b/files/routes/polls.py index 387df1e08..8a9661cf5 100644 --- a/files/routes/polls.py +++ b/files/routes/polls.py @@ -21,8 +21,8 @@ def vote_option(option_id, v): abort(403, f"You need to be a member of House {sub.capitalize()} to vote on polls in /h/{sub}") if option.exclusive == 2: - if v.coins < POLL_BET_COINS: abort(400, f"You don't have {POLL_BET_COINS} coins!") - v.charge_account('coins', POLL_BET_COINS) + if not v.charge_account('coins', POLL_BET_COINS): + abort(400, f"You don't have {POLL_BET_COINS} coins!") g.db.add(v) autojanny = get_account(AUTOJANNY_ID) autojanny.coins += POLL_BET_COINS diff --git a/files/routes/posts.py b/files/routes/posts.py index 3a0b3fddc..2135a86f5 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -774,10 +774,7 @@ def submit_post(v, sub=None): if embed and len(embed) > 1500: embed = None - if request.values.get("ghost") and v.coins >= 100: - v.charge_account('coins', 100) - ghost = True - else: ghost = False + ghost = request.values.get("ghost") and v.charge_account('coins', 100) if embed: embed = embed.strip() diff --git a/files/routes/settings.py b/files/routes/settings.py index 64edac386..2289f1abb 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -274,9 +274,10 @@ def settings_profile_post(v): if v.house: cost = 2000 else: cost = 500 - if v.coins >= cost: v.charge_account('coins', cost) - elif v.procoins >= cost: v.charge_account('procoins', cost) - else: abort(403) + success = v.charge_account('coins', cost) + if not success: + success = v.charge_account('procoins', cost) + if not success: abort(403) if house == "None": house = '' v.house = house diff --git a/files/routes/subs.py b/files/routes/subs.py index 15ed3dbf6..f2204c307 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -329,10 +329,9 @@ def create_sub2(v): sub = get_sub_by_name(name, graceful=True) if not sub: - if v.coins < HOLE_COST: + if not v.charge_account('coins', HOLE_COST): return render_template("sub/create_hole.html", v=v, cost=HOLE_COST, error="You don't have enough coins!"), 403 - v.charge_account('coins', HOLE_COST) g.db.add(v) if v.shadowbanned: return {"error": "Internal Server Error"}, 500 diff --git a/files/routes/users.py b/files/routes/users.py index 1199f656f..3e6a57c94 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -272,7 +272,6 @@ def transfer_currency(v:User, username:str, currency_name:Literal['coins', 'proc amount = int(amount) if amount.isdigit() else None if amount is None or amount <= 0: abort(400, f"Invalid number of {friendly_currency_name}") - if v.coins < amount: abort(400, f"You don't have enough {friendly_currency_name}") if amount < MIN_CURRENCY_TRANSFER: abort(400, f"You have to gift at least {MIN_CURRENCY_TRANSFER} {friendly_currency_name}") tax = 0 if apply_tax and not v.patron and not receiver.patron and not v.alts_patron and not receiver.alts_patron: @@ -281,11 +280,15 @@ def transfer_currency(v:User, username:str, currency_name:Literal['coins', 'proc reason = request.values.get("reason", "").strip() log_message = f"@{v.username} has transferred {amount} {friendly_currency_name} to @{receiver.username}" notif_text = f":marseycapitalistmanlet: @{v.username} has gifted you {amount-tax} {friendly_currency_name}!" + if reason: if len(reason) > TRANSFER_MESSAGE_LENGTH_LIMIT: abort(400, f"Reason is too long, max {TRANSFER_MESSAGE_LENGTH_LIMIT} characters") notif_text += f"\n\n> {reason}" log_message += f"\n\n> {reason}" - v.charge_account(currency_name, amount) + + if not v.charge_account(currency_name, amount): + abort(400, f"You don't have enough {friendly_currency_name}") + if not v.shadowbanned: receiver.coins += amount - tax g.db.add(receiver)