2022-05-29 03:33:44 +00:00
|
|
|
import time
|
2024-02-21 20:08:33 +00:00
|
|
|
import random
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from flask import g
|
|
|
|
from files.classes.lottery import Lottery
|
|
|
|
|
2022-05-29 04:23:20 +00:00
|
|
|
from files.helpers.alerts import *
|
2022-11-01 23:46:56 +00:00
|
|
|
from files.helpers.useractions import *
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-11 23:44:34 +00:00
|
|
|
from .config.const import *
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-06 04:07:38 +00:00
|
|
|
LOTTERY_WINNER_BADGE_ID = 137
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-05-30 00:49:14 +00:00
|
|
|
def get_active_lottery():
|
2023-03-16 06:27:58 +00:00
|
|
|
return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none()
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-05-29 06:01:45 +00:00
|
|
|
|
2022-05-30 00:49:14 +00:00
|
|
|
def get_users_participating_in_lottery():
|
2023-03-16 06:27:58 +00:00
|
|
|
return g.db.query(User) \
|
2022-06-13 18:33:25 +00:00
|
|
|
.filter(User.currently_held_lottery_tickets > 0) \
|
|
|
|
.order_by(User.currently_held_lottery_tickets.desc()).all()
|
2022-05-29 06:01:45 +00:00
|
|
|
|
|
|
|
|
2022-05-30 00:49:14 +00:00
|
|
|
def get_active_lottery_stats():
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery = get_active_lottery()
|
|
|
|
participating_users = get_users_participating_in_lottery()
|
2022-05-29 06:01:45 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
return None if active_lottery is None else active_lottery.stats, len(participating_users)
|
2022-05-29 06:01:45 +00:00
|
|
|
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-05-30 00:49:14 +00:00
|
|
|
def end_lottery_session():
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery = get_active_lottery()
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
if (active_lottery is None):
|
2023-01-27 11:57:29 +00:00
|
|
|
return False, "There is no active lottery!"
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
participating_users = get_users_participating_in_lottery()
|
|
|
|
raffle = []
|
|
|
|
for user in participating_users:
|
|
|
|
for _ in range(user.currently_held_lottery_tickets):
|
|
|
|
raffle.append(user.id)
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-07-21 18:28:52 +00:00
|
|
|
if len(raffle) == 0:
|
|
|
|
active_lottery.is_active = False
|
2023-01-27 11:57:29 +00:00
|
|
|
return True, "Lottery ended with no participants!"
|
2022-07-21 18:28:52 +00:00
|
|
|
|
2024-02-21 20:08:33 +00:00
|
|
|
winner = random.choice(raffle)
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery.winner_id = winner
|
|
|
|
winning_user = next(filter(lambda x: x.id == winner, participating_users))
|
2024-03-02 17:24:07 +00:00
|
|
|
winning_user.pay_account('coins', active_lottery.prize, "Lottery winnings")
|
2022-06-13 18:33:25 +00:00
|
|
|
winning_user.total_lottery_winnings += active_lottery.prize
|
2022-06-15 19:33:21 +00:00
|
|
|
badge_grant(user=winning_user, badge_id=LOTTERY_WINNER_BADGE_ID)
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
for user in participating_users:
|
|
|
|
chance_to_win = user.currently_held_lottery_tickets / len(raffle) * 100
|
2023-04-03 17:58:14 +00:00
|
|
|
chance_to_win = str(chance_to_win)[:5]
|
2022-06-13 18:33:25 +00:00
|
|
|
if user.id == winner:
|
2022-06-28 00:54:44 +00:00
|
|
|
notification_text = f'You won {active_lottery.prize} coins in the lottershe! ' \
|
2023-02-26 10:20:32 +00:00
|
|
|
+ f'Congratulations!\n\nYour odds of winning were: {chance_to_win}%'
|
2022-06-13 18:33:25 +00:00
|
|
|
else:
|
2023-02-26 10:20:32 +00:00
|
|
|
notification_text = 'You did not win the lottershe. Better luck next time!\n\n' \
|
|
|
|
+ f'Your odds of winning were: {chance_to_win}%\n\nWinner: @{winning_user.username} (won {active_lottery.prize} coins)'
|
2022-06-13 18:33:25 +00:00
|
|
|
send_repeatable_notification(user.id, notification_text)
|
|
|
|
user.currently_held_lottery_tickets = 0
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery.is_active = False
|
2022-05-29 04:23:20 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(winning_user)
|
|
|
|
g.db.add(active_lottery)
|
|
|
|
g.db.commit() # Intentionally commit early because cron runs with other tasks
|
2022-11-20 10:50:02 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
return True, f'{winning_user.username} won {active_lottery.prize} coins!'
|
2022-05-29 04:23:20 +00:00
|
|
|
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-05-30 00:49:14 +00:00
|
|
|
def start_new_lottery_session():
|
2022-06-13 18:33:25 +00:00
|
|
|
end_lottery_session()
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
lottery = Lottery()
|
|
|
|
epoch_time = int(time.time())
|
2022-06-16 02:23:21 +00:00
|
|
|
# Subtract 4 minutes from one cycle so cronjob interval doesn't cause the
|
|
|
|
# time to drift toward over multiple cycles.
|
|
|
|
one_week_from_now = epoch_time + LOTTERY_DURATION - (4 * 60)
|
2022-06-13 18:33:25 +00:00
|
|
|
lottery.ends_at = one_week_from_now
|
|
|
|
lottery.is_active = True
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(lottery)
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-07 11:36:55 +00:00
|
|
|
def check_if_end_lottery_task():
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery = get_active_lottery()
|
2022-06-07 11:36:55 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
if active_lottery is None:
|
|
|
|
return False
|
|
|
|
elif active_lottery.timeleft > 0:
|
|
|
|
return False
|
2022-06-07 11:36:55 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
start_new_lottery_session()
|
|
|
|
return True
|
2022-06-07 11:36:55 +00:00
|
|
|
|
2022-06-10 10:15:37 +00:00
|
|
|
def lottery_ticket_net_value():
|
2022-06-13 18:33:25 +00:00
|
|
|
return LOTTERY_TICKET_COST - LOTTERY_SINK_RATE
|
2022-06-07 11:36:55 +00:00
|
|
|
|
2022-06-02 23:19:49 +00:00
|
|
|
def purchase_lottery_tickets(v, quantity=1):
|
2022-06-13 18:33:25 +00:00
|
|
|
if quantity < 1:
|
2023-02-24 02:54:31 +00:00
|
|
|
return False, "Must purchase one or more lottershe tickets!"
|
2022-06-13 18:33:25 +00:00
|
|
|
elif (v.coins < LOTTERY_TICKET_COST * quantity):
|
2023-01-27 12:09:20 +00:00
|
|
|
return False, f"Lottery tickets cost {LOTTERY_TICKET_COST} coins each!"
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
most_recent_lottery = get_active_lottery()
|
|
|
|
if (most_recent_lottery is None):
|
2023-01-27 11:57:29 +00:00
|
|
|
return False, "There is no active lottery!"
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2024-03-06 01:58:39 +00:00
|
|
|
charge_reason = f'Cost of {quantity} lottery ticket'
|
|
|
|
if quantity > 1:
|
|
|
|
charge_reason += 's'
|
|
|
|
|
2024-03-09 12:19:38 +00:00
|
|
|
if not v.charge_account('coins', LOTTERY_TICKET_COST * quantity, charge_reason):
|
2022-11-01 05:25:19 +00:00
|
|
|
return False, "You don't have enough coins"
|
2023-11-27 21:13:07 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
v.currently_held_lottery_tickets += quantity
|
|
|
|
v.total_held_lottery_tickets += quantity
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
net_ticket_value = lottery_ticket_net_value() * quantity
|
|
|
|
most_recent_lottery.prize += net_ticket_value
|
|
|
|
most_recent_lottery.tickets_sold += quantity
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-28 00:54:44 +00:00
|
|
|
if quantity == 1: return True, f'Successfully purchased {quantity} lottershe ticket!'
|
|
|
|
return True, f'Successfully purchased {quantity} lottershe tickets!'
|
2022-05-30 02:59:22 +00:00
|
|
|
|
2022-06-10 10:15:37 +00:00
|
|
|
def grant_lottery_tickets_to_user(v, quantity):
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery = get_active_lottery()
|
|
|
|
prize_value = lottery_ticket_net_value() * quantity
|
2022-05-30 02:59:22 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
if active_lottery:
|
|
|
|
v.currently_held_lottery_tickets += quantity
|
|
|
|
v.total_held_lottery_tickets += quantity
|
2022-05-30 02:59:22 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
active_lottery.prize += prize_value
|
|
|
|
active_lottery.tickets_sold += quantity
|