diff --git a/files/helpers/lottery.py b/files/helpers/lottery.py index 67096fb82..01fa09f6d 100644 --- a/files/helpers/lottery.py +++ b/files/helpers/lottery.py @@ -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) diff --git a/files/routes/lottery.py b/files/routes/lottery.py index 964913076..6775143b2 100644 --- a/files/routes/lottery.py +++ b/files/routes/lottery.py @@ -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}} diff --git a/files/templates/lottery_modal.html b/files/templates/lottery_modal.html index 7337e4d69..745e3284e 100644 --- a/files/templates/lottery_modal.html +++ b/files/templates/lottery_modal.html @@ -26,7 +26,10 @@
{% if v.admin_level > 2 %} -
+
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 %}
-
Grand Prize
-
Session Ends
-
Tickets Sold (Session)
-
Tickets Sold (Total)
+
Prize
+
Time Remaining
+
Tickets Sold This Session
+
Participants This Session
-
0
-
0:00:00
-
0
-
0
+
+ - +
+
-
+
-
+
-
-
Tickets Owned (Session)
-
Tickets Owned (Total)
-
Total Earnings
+
Tickets Owned This Session
+
Total Tickets Owned
+
Total Winnings
-
0
-
0
-
0
+
-
+
-
+
-