forked from rDrama/rDrama
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 routesmaster
parent
1ca88f719e
commit
a0c75ab677
|
@ -173,6 +173,35 @@ class User(Base):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<User(id={self.id}, username={self.username})>"
|
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
|
@property
|
||||||
@lazy
|
@lazy
|
||||||
|
|
|
@ -71,15 +71,11 @@ def get_active_roulette_games():
|
||||||
|
|
||||||
|
|
||||||
def charge_gambler(gambler, amount, currency):
|
def charge_gambler(gambler, amount, currency):
|
||||||
currency_gambler_holds = getattr(gambler, currency)
|
charged = gambler.charge_account(currency, amount)
|
||||||
can_afford = currency_gambler_holds >= amount
|
|
||||||
|
|
||||||
if not can_afford:
|
if not charged:
|
||||||
raise Exception("Gambler cannot afford charge.")
|
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):
|
def gambler_placed_roulette_bet(gambler, bet, which, amount, currency):
|
||||||
if not bet in (
|
if not bet in (
|
||||||
|
@ -172,10 +168,8 @@ def spin_roulette_wheel():
|
||||||
coin_winnings = gambler_payout['coins']
|
coin_winnings = gambler_payout['coins']
|
||||||
procoin_winnings = gambler_payout['procoins']
|
procoin_winnings = gambler_payout['procoins']
|
||||||
|
|
||||||
setattr(gambler, 'coins', gambler.coins + coin_winnings)
|
gambler.pay_account('coins', coin_winnings)
|
||||||
setattr(gambler, 'procoins', gambler.procoins + procoin_winnings)
|
gambler.pay_account('procoins', procoin_winnings)
|
||||||
|
|
||||||
g.db.add(gambler)
|
|
||||||
|
|
||||||
# Notify the winners.
|
# Notify the winners.
|
||||||
notification_text = f"Winning number: {number}\nCongratulations! One or more of your roulette bets paid off!\n"
|
notification_text = f"Winning number: {number}\nCongratulations! One or more of your roulette bets paid off!\n"
|
||||||
|
|
|
@ -18,22 +18,17 @@ payout_to_symbols = {
|
||||||
def casino_slot_pull(gambler, wager_value, currency):
|
def casino_slot_pull(gambler, wager_value, currency):
|
||||||
over_min = wager_value >= minimum_bet
|
over_min = wager_value >= minimum_bet
|
||||||
under_max = wager_value <= maximum_bet
|
under_max = wager_value <= maximum_bet
|
||||||
currency_value = getattr(gambler, currency)
|
charged = gambler.charge_account(currency, wager_value)
|
||||||
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
|
|
||||||
|
|
||||||
|
if (over_min and under_max and charged):
|
||||||
payout = determine_payout()
|
payout = determine_payout()
|
||||||
reward = wager_value * payout
|
reward = wager_value * payout
|
||||||
|
|
||||||
currency_value = getattr(gambler, currency, 0)
|
if payout > 0:
|
||||||
setattr(gambler, currency, currency_value + reward)
|
gambler.pay_account(currency, wager_value + reward)
|
||||||
gambler.winnings += reward
|
|
||||||
symbols = build_symbols(payout)
|
symbols = build_symbols(payout)
|
||||||
text = build_text(wager_value, payout, currency)
|
text = build_text(wager_value, payout, currency)
|
||||||
|
|
||||||
game_state = {
|
game_state = {
|
||||||
"symbols": symbols,
|
"symbols": symbols,
|
||||||
"text": text
|
"text": text
|
||||||
|
|
|
@ -80,15 +80,11 @@ def get_active_twentyone_game_state(gambler):
|
||||||
|
|
||||||
|
|
||||||
def charge_gambler(gambler, amount, currency):
|
def charge_gambler(gambler, amount, currency):
|
||||||
currency_gambler_holds = getattr(gambler, currency)
|
charged = gambler.charge_account(currency, amount)
|
||||||
can_afford = currency_gambler_holds >= amount
|
|
||||||
|
if not charged:
|
||||||
if not can_afford:
|
|
||||||
raise Exception("Gambler cannot afford charge.")
|
raise Exception("Gambler cannot afford charge.")
|
||||||
|
|
||||||
setattr(gambler, currency, currency_gambler_holds - amount)
|
|
||||||
g.db.add(gambler)
|
|
||||||
|
|
||||||
|
|
||||||
def create_new_game(gambler, wager, currency):
|
def create_new_game(gambler, wager, currency):
|
||||||
existing_game = get_active_twentyone_game(gambler)
|
existing_game = get_active_twentyone_game(gambler)
|
||||||
|
@ -245,12 +241,10 @@ def handle_payout(gambler, state, game):
|
||||||
else:
|
else:
|
||||||
raise Exception("Attempted to payout a game that has not finished.")
|
raise Exception("Attempted to payout a game that has not finished.")
|
||||||
|
|
||||||
currency_gambler_holds = getattr(gambler, game.currency)
|
gambler.pay_account(game.currency, payout)
|
||||||
setattr(gambler, game.currency, currency_gambler_holds + payout)
|
|
||||||
|
|
||||||
game.active = False
|
game.active = False
|
||||||
g.db.add(game)
|
g.db.add(game)
|
||||||
g.db.add(gambler)
|
|
||||||
|
|
||||||
return payout
|
return payout
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ from files.helpers.roulette import *
|
||||||
|
|
||||||
@app.get("/casino")
|
@app.get("/casino")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def casino(v):
|
def casino(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return render_template("casino/rehab.html", v=v)
|
return render_template("casino/rehab.html", v=v)
|
||||||
|
@ -23,7 +23,7 @@ def casino(v):
|
||||||
|
|
||||||
@app.get("/casino/<game>")
|
@app.get("/casino/<game>")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def casino_game_page(v, game):
|
def casino_game_page(v, game):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return render_template("casino/rehab.html", v=v)
|
return render_template("casino/rehab.html", v=v)
|
||||||
|
@ -42,7 +42,7 @@ def casino_game_page(v, game):
|
||||||
|
|
||||||
@app.get("/casino/<game>/feed")
|
@app.get("/casino/<game>/feed")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def casino_game_feed(v, game):
|
def casino_game_feed(v, game):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -54,7 +54,7 @@ def casino_game_feed(v, game):
|
||||||
# Lottershe
|
# Lottershe
|
||||||
@app.get("/lottershe")
|
@app.get("/lottershe")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def lottershe(v):
|
def lottershe(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return render_template("casino/rehab.html", v=v)
|
return render_template("casino/rehab.html", v=v)
|
||||||
|
@ -65,7 +65,7 @@ def lottershe(v):
|
||||||
# Slots
|
# Slots
|
||||||
@app.post("/casino/slots")
|
@app.post("/casino/slots")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def pull_slots(v):
|
def pull_slots(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -94,7 +94,7 @@ def pull_slots(v):
|
||||||
# 21
|
# 21
|
||||||
@app.post("/casino/twentyone/deal")
|
@app.post("/casino/twentyone/deal")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def blackjack_deal_to_player(v):
|
def blackjack_deal_to_player(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -113,7 +113,7 @@ def blackjack_deal_to_player(v):
|
||||||
|
|
||||||
@app.post("/casino/twentyone/hit")
|
@app.post("/casino/twentyone/hit")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def blackjack_player_hit(v):
|
def blackjack_player_hit(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -128,7 +128,7 @@ def blackjack_player_hit(v):
|
||||||
|
|
||||||
@app.post("/casino/twentyone/stay")
|
@app.post("/casino/twentyone/stay")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def blackjack_player_stay(v):
|
def blackjack_player_stay(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -143,7 +143,7 @@ def blackjack_player_stay(v):
|
||||||
|
|
||||||
@app.post("/casino/twentyone/double-down")
|
@app.post("/casino/twentyone/double-down")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def blackjack_player_doubled_down(v):
|
def blackjack_player_doubled_down(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
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")
|
@app.post("/casino/twentyone/buy-insurance")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def blackjack_player_bought_insurance(v):
|
def blackjack_player_bought_insurance(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -173,7 +173,7 @@ def blackjack_player_bought_insurance(v):
|
||||||
# Roulette
|
# Roulette
|
||||||
@app.get("/casino/roulette/bets")
|
@app.get("/casino/roulette/bets")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def roulette_get_bets(v):
|
def roulette_get_bets(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
@ -185,7 +185,7 @@ def roulette_get_bets(v):
|
||||||
|
|
||||||
@app.post("/casino/roulette/place-bet")
|
@app.post("/casino/roulette/place-bet")
|
||||||
@limiter.limit("100/minute;2000/hour;12000/day")
|
@limiter.limit("100/minute;2000/hour;12000/day")
|
||||||
@admin_level_required(4)
|
@auth_required
|
||||||
def roulette_player_placed_bet(v):
|
def roulette_player_placed_bet(v):
|
||||||
if v.rehab:
|
if v.rehab:
|
||||||
return {"error": "You are under Rehab award effect!"}, 400
|
return {"error": "You are under Rehab award effect!"}, 400
|
||||||
|
|
Loading…
Reference in New Issue