Introduce user payment methods (#355)

* Add pay and charge methods into user class

* Replace casino charges/payments with new user methods

* Also refund wager on winning slots bets

* Unblock casino routes
remotes/1693045480750635534/spooky-22
outruncolors 2022-09-15 17:16:35 -05:00 committed by GitHub
parent 1ca88f719e
commit a0c75ab677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 43 deletions

View File

@ -173,6 +173,35 @@ class User(Base):
def __repr__(self):
return f"<User(id={self.id}, username={self.username})>"
def pay_account(self, currency, amount):
if currency == 'coins':
g.db.query(User).filter(User.id == self.id).update({ User.coins: User.coins + amount })
else:
g.db.query(User).filter(User.id == self.id).update({ User.procoins: User.procoins + amount })
g.db.flush()
def charge_account(self, currency, amount):
in_db = g.db.query(User).filter(User.id == self.id).one()
succeeded = False
if currency == 'coins':
account_balance = in_db.coins
if account_balance >= amount:
g.db.query(User).filter(User.id == self.id).update({ User.coins: User.coins - amount })
succeeded = True
elif currency == 'procoins':
account_balance = in_db.procoins
if account_balance >= amount:
g.db.query(User).filter(User.id == self.id).update({ User.procoins: User.procoins - amount })
succeeded = True
if succeeded: g.db.flush()
return succeeded
@property
@lazy

View File

@ -71,15 +71,11 @@ def get_active_roulette_games():
def charge_gambler(gambler, amount, currency):
currency_gambler_holds = getattr(gambler, currency)
can_afford = currency_gambler_holds >= amount
charged = gambler.charge_account(currency, amount)
if not can_afford:
if not charged:
raise Exception("Gambler cannot afford charge.")
setattr(gambler, currency, currency_gambler_holds - amount)
g.db.add(gambler)
def gambler_placed_roulette_bet(gambler, bet, which, amount, currency):
if not bet in (
@ -172,10 +168,8 @@ def spin_roulette_wheel():
coin_winnings = gambler_payout['coins']
procoin_winnings = gambler_payout['procoins']
setattr(gambler, 'coins', gambler.coins + coin_winnings)
setattr(gambler, 'procoins', gambler.procoins + procoin_winnings)
g.db.add(gambler)
gambler.pay_account('coins', coin_winnings)
gambler.pay_account('procoins', procoin_winnings)
# Notify the winners.
notification_text = f"Winning number: {number}\nCongratulations! One or more of your roulette bets paid off!\n"

View File

@ -18,22 +18,17 @@ payout_to_symbols = {
def casino_slot_pull(gambler, wager_value, currency):
over_min = wager_value >= minimum_bet
under_max = wager_value <= maximum_bet
currency_value = getattr(gambler, currency)
has_proper_funds = currency_value >= wager_value
if (over_min and under_max and has_proper_funds):
setattr(gambler, currency, currency_value - wager_value)
gambler.winnings -= wager_value
charged = gambler.charge_account(currency, wager_value)
if (over_min and under_max and charged):
payout = determine_payout()
reward = wager_value * payout
currency_value = getattr(gambler, currency, 0)
setattr(gambler, currency, currency_value + reward)
gambler.winnings += reward
if payout > 0:
gambler.pay_account(currency, wager_value + reward)
symbols = build_symbols(payout)
text = build_text(wager_value, payout, currency)
game_state = {
"symbols": symbols,
"text": text

View File

@ -80,15 +80,11 @@ def get_active_twentyone_game_state(gambler):
def charge_gambler(gambler, amount, currency):
currency_gambler_holds = getattr(gambler, currency)
can_afford = currency_gambler_holds >= amount
if not can_afford:
charged = gambler.charge_account(currency, amount)
if not charged:
raise Exception("Gambler cannot afford charge.")
setattr(gambler, currency, currency_gambler_holds - amount)
g.db.add(gambler)
def create_new_game(gambler, wager, currency):
existing_game = get_active_twentyone_game(gambler)
@ -245,12 +241,10 @@ def handle_payout(gambler, state, game):
else:
raise Exception("Attempted to payout a game that has not finished.")
currency_gambler_holds = getattr(gambler, game.currency)
setattr(gambler, game.currency, currency_gambler_holds + payout)
gambler.pay_account(game.currency, payout)
game.active = False
g.db.add(game)
g.db.add(gambler)
return payout

View File

@ -13,7 +13,7 @@ from files.helpers.roulette import *
@app.get("/casino")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def casino(v):
if v.rehab:
return render_template("casino/rehab.html", v=v)
@ -23,7 +23,7 @@ def casino(v):
@app.get("/casino/<game>")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def casino_game_page(v, game):
if v.rehab:
return render_template("casino/rehab.html", v=v)
@ -42,7 +42,7 @@ def casino_game_page(v, game):
@app.get("/casino/<game>/feed")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def casino_game_feed(v, game):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -54,7 +54,7 @@ def casino_game_feed(v, game):
# Lottershe
@app.get("/lottershe")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def lottershe(v):
if v.rehab:
return render_template("casino/rehab.html", v=v)
@ -65,7 +65,7 @@ def lottershe(v):
# Slots
@app.post("/casino/slots")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def pull_slots(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -94,7 +94,7 @@ def pull_slots(v):
# 21
@app.post("/casino/twentyone/deal")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def blackjack_deal_to_player(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -113,7 +113,7 @@ def blackjack_deal_to_player(v):
@app.post("/casino/twentyone/hit")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def blackjack_player_hit(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -128,7 +128,7 @@ def blackjack_player_hit(v):
@app.post("/casino/twentyone/stay")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def blackjack_player_stay(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -143,7 +143,7 @@ def blackjack_player_stay(v):
@app.post("/casino/twentyone/double-down")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def blackjack_player_doubled_down(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -158,7 +158,7 @@ def blackjack_player_doubled_down(v):
@app.post("/casino/twentyone/buy-insurance")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def blackjack_player_bought_insurance(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -173,7 +173,7 @@ def blackjack_player_bought_insurance(v):
# Roulette
@app.get("/casino/roulette/bets")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def roulette_get_bets(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400
@ -185,7 +185,7 @@ def roulette_get_bets(v):
@app.post("/casino/roulette/place-bet")
@limiter.limit("100/minute;2000/hour;12000/day")
@admin_level_required(4)
@auth_required
def roulette_player_placed_bet(v):
if v.rehab:
return {"error": "You are under Rehab award effect!"}, 400