forked from rDrama/rDrama
Add participants and field updating
parent
1a55a7670e
commit
bef0b0ff6d
|
@ -22,9 +22,17 @@ MANAGER_ACCOUNT_ID = 3
|
|||
def get_active_lottery(g):
|
||||
return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none()
|
||||
|
||||
|
||||
def get_users_participating_in_lottery(g):
|
||||
return g.db.query(User).filter(User.currently_held_lottery_tickets > 0).all()
|
||||
|
||||
|
||||
def get_active_lottery_stats(g):
|
||||
active_lottery = get_active_lottery(g)
|
||||
return None if active_lottery is None else active_lottery.stats
|
||||
participating_users = get_users_participating_in_lottery(g)
|
||||
|
||||
return None if active_lottery is None else active_lottery.stats, len(participating_users)
|
||||
|
||||
|
||||
def end_lottery_session(g):
|
||||
active_lottery = get_active_lottery(g)
|
||||
|
@ -32,27 +40,23 @@ def end_lottery_session(g):
|
|||
if (active_lottery is None):
|
||||
return False, "There is no active lottery."
|
||||
|
||||
participating_users = g.db.query(User).filter(
|
||||
User.currently_held_lottery_tickets > 0).all()
|
||||
|
||||
participating_users = get_users_participating_in_lottery(g)
|
||||
raffle = []
|
||||
for user in participating_users:
|
||||
for _ in range(user.currently_held_lottery_tickets):
|
||||
raffle.append(user.id)
|
||||
|
||||
|
||||
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)
|
||||
chance_to_win = user.currently_held_lottery_tickets / len(raffle) * 100
|
||||
notification_text = f'You won {active_lottery.prize} dramacoins in 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_repeatable_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)
|
||||
|
|
|
@ -42,4 +42,5 @@ def lottery_buy(v):
|
|||
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
||||
@auth_required
|
||||
def lottery_active(v):
|
||||
return {"message": {"user": v.lottery_stats, "lottery": get_active_lottery_stats(g)}}
|
||||
lottery, participants = get_active_lottery_stats(g)
|
||||
return {"message": "", "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}}
|
||||
|
|
|
@ -26,7 +26,10 @@
|
|||
</div>
|
||||
<div class="lottery-modal--stats">
|
||||
{% if v.admin_level > 2 %}
|
||||
<div class="lottery-modal--stat" style="position: relative; padding-top: 1rem;">
|
||||
<div
|
||||
class="lottery-modal--stat"
|
||||
style="position: relative; padding-top: 1rem"
|
||||
>
|
||||
<i
|
||||
class="fas fa-broom"
|
||||
style="
|
||||
|
@ -41,7 +44,7 @@
|
|||
type="button"
|
||||
class="btn btn-danger"
|
||||
id="endLotterySession"
|
||||
|
||||
style="display: none"
|
||||
onclick="endLotterySession()"
|
||||
>
|
||||
End Current Session
|
||||
|
@ -50,6 +53,7 @@
|
|||
type="button"
|
||||
class="btn btn-success"
|
||||
id="startLotterySession"
|
||||
style="display: none"
|
||||
onclick="startLotterySession()"
|
||||
>
|
||||
Start New Session
|
||||
|
@ -58,28 +62,42 @@
|
|||
{% endif %}
|
||||
<div class="lottery-modal--stat">
|
||||
<div class="lottery-modal--stat-keys">
|
||||
<div>Grand Prize</div>
|
||||
<div>Session Ends</div>
|
||||
<div>Tickets Sold (Session)</div>
|
||||
<div>Tickets Sold (Total)</div>
|
||||
<div>Prize</div>
|
||||
<div>Time Remaining</div>
|
||||
<div>Tickets Sold This Session</div>
|
||||
<div>Participants This Session</div>
|
||||
</div>
|
||||
<div class="lottery-modal--stat-values">
|
||||
<div>0</div>
|
||||
<div>0:00:00</div>
|
||||
<div>0</div>
|
||||
<div>0</div>
|
||||
<div>
|
||||
<img
|
||||
id="prize-image"
|
||||
alt="coins"
|
||||
class="mr-1 ml-1"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-placement="bottom"
|
||||
height="13"
|
||||
src="/assets/images/rDrama/coins.webp?v=2"
|
||||
title=""
|
||||
aria-label="coins"
|
||||
data-bs-original-title="coins"
|
||||
style="display: none; position: relative; top: -2px;"
|
||||
/> <span id="prize">-</span>
|
||||
</div>
|
||||
<div id="timeLeft">-</div>
|
||||
<div id="ticketsSoldThisSession">-</div>
|
||||
<div id="participantsThisSession">-</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lottery-modal--stat">
|
||||
<div class="lottery-modal--stat-keys">
|
||||
<div>Tickets Owned (Session)</div>
|
||||
<div>Tickets Owned (Total)</div>
|
||||
<div>Total Earnings</div>
|
||||
<div>Tickets Owned This Session</div>
|
||||
<div>Total Tickets Owned</div>
|
||||
<div>Total Winnings</div>
|
||||
</div>
|
||||
<div class="lottery-modal--stat-values">
|
||||
<div>0</div>
|
||||
<div>0</div>
|
||||
<div>0</div>
|
||||
<div id="ticketsHeldCurrent">-</div>
|
||||
<div id="ticketsHeldTotal">-</div>
|
||||
<div id="winnings">-</div>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
|
@ -87,6 +105,7 @@
|
|||
class="btn btn-success lottery-modal--action"
|
||||
id="purchaseTicket"
|
||||
onclick="purchaseLotteryTicket()"
|
||||
disabled="true"
|
||||
>
|
||||
Purchase 1 for
|
||||
<img
|
||||
|
|
Loading…
Reference in New Issue