forked from rDrama/rDrama
1
0
Fork 0

Send notifications to winners and losers

master
Outrun Colors, LLC 2022-05-28 23:23:20 -05:00
parent 469c39dca6
commit 1a55a7670e
2 changed files with 49 additions and 42 deletions

View File

@ -1,5 +1,7 @@
import time
from random import choice
from sqlalchemy import *
from files.helpers.alerts import *
from files.helpers.wrappers import *
LOTTERY_TICKET_COST = 12
@ -8,60 +10,68 @@ LOTTERY_TICKET_COST = 12
SINK_RATE = 3
# The amount of dramacoins the lottery founders receive
ROYALTY_RATE = 1
ROYALTY_RATE = 1
# The account in which royalties are to be deposited
ROYALTY_ACCOUNT_ID = 9
ROYALTY_ACCOUNT_ID = 9
# The account in which the prize is held to be accessed by anyone
MANAGER_ACCOUNT_ID = 3
def get_most_recent_lottery(g):
lotteries = g.db.query(Lottery).order_by(Lottery.id.desc())
count = 0
mostRecent = None
for lottery in lotteries:
if count == 0:
mostRecent = lottery
count += 1
return mostRecent
def get_active_lottery(g):
return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none()
def get_active_lottery_stats(g):
active_lottery = get_active_lottery(g)
return None if active_lottery is None else active_lottery.stats
def end_lottery_session(g):
# Check that a current session exists
most_recent_lottery = get_most_recent_lottery(g)
active_lottery = get_active_lottery(g)
if (most_recent_lottery is None or not most_recent_lottery.is_active):
return
if (active_lottery is None):
return False, "There is no active lottery."
# Calculate players and odds
participating_users = g.db.query(User).filter(
User.currently_held_lottery_tickets > 0).all()
# Payout to winner
raffle = []
for user in participating_users:
for _ in range(user.currently_held_lottery_tickets):
raffle.append(user.id)
# Send DM to all involved
most_recent_lottery.is_active = False
winner = choice(raffle)
winning_user = next(filter(lambda x: x.id == winner, participating_users))
winning_user.coins += active_lottery.prize
winning_user.total_lottery_winnings += active_lottery.prize
for user in participating_users:
chance_to_win = user.currently_held_lottery_tickets / raffle.count
notification_text = f'You won {active_lottery.prize} the lottery! Congratulations!\nOdds of winning: {chance_to_win}%' if user.id == winner else "You did not win the lottery. Better luck next time!\nOdds of winning: {chance_to_win}%"
send_notification(user.id, notification_text)
user.currently_held_lottery_tickets = 0
active_lottery.is_active = False
manager = g.db.query(User).get(MANAGER_ACCOUNT_ID)
manager.coins -= active_lottery.prize
# Save changes
g.db.commit()
return True, f'{winning_user.username} won {active_lottery.prize} dramacoins!'
def start_new_lottery_session(g):
# Wrap up current session
end_lottery_session(g)
# Create new session
lottery = Lottery()
epoch_time = int(time.time())
one_week_from_now = epoch_time + 60 * 60 * 24 * 7
lottery.ends_at = one_week_from_now
lottery.is_active = True
# Save changes
g.db.add(lottery)
g.db.commit()
@ -70,11 +80,10 @@ def purchase_lottery_ticket(g, v):
if (v.coins < LOTTERY_TICKET_COST):
return False, f'Lottery tickets cost {LOTTERY_TICKET_COST} dramacoins each.', None
most_recent_lottery = get_most_recent_lottery(g)
if (most_recent_lottery is None or not most_recent_lottery.is_active):
most_recent_lottery = get_active_lottery(g)
if (most_recent_lottery is None):
return False, "There is no active lottery.", None
# Charge the user and update the lottery
v.coins -= LOTTERY_TICKET_COST
v.currently_held_lottery_tickets += 1
v.total_held_lottery_tickets += 1
@ -83,15 +92,12 @@ def purchase_lottery_ticket(g, v):
most_recent_lottery.prize += net_ticket_value
most_recent_lottery.tickets_sold += 1
# Pass the holdings to the lottery manager
manager = g.db.query(User).get(MANAGER_ACCOUNT_ID)
manager.coins += net_ticket_value
# Pay royalties
beneficiary = g.db.query(User).get(ROYALTY_ACCOUNT_ID)
beneficiary.coins += ROYALTY_RATE
# Save changes
g.db.commit()
return True, 'Successfully purchased a lottery ticket!', most_recent_lottery.stats

View File

@ -9,15 +9,21 @@ from files.helpers.lottery import *
@app.post("/lottery/end")
@auth_required
def lottery_end(v):
end_lottery_session(g)
return {"message": "Lottery ended."}
if v.admin_level > 2:
success, message = end_lottery_session(g)
return {"message": message} if success else {"error": message}
else:
return {"error": "JL3+ or higher required to start and end lotteries."}, 401
@app.post("/lottery/start")
@auth_required
def lottery_start(v):
start_new_lottery_session(g)
return {"message": "Lottery started."}
if v.admin_level > 2:
start_new_lottery_session(g)
return {"message": "Lottery started."}
else:
return {"error": "JL3+ or higher required to start and end lotteries."}, 401
@app.post("/lottery/buy")
@ -36,9 +42,4 @@ def lottery_buy(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def lottery_active(v):
most_recent_lottery = get_most_recent_lottery(g)
if most_recent_lottery is None or not most_recent_lottery.is_active:
return {"message": "There is no active lottery."}
return {"message": most_recent_lottery.stats}
return {"message": {"user": v.lottery_stats, "lottery": get_active_lottery_stats(g)}}